dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
562 stars 69 forks source link

setTimeout problems / questions #69

Closed joshkel closed 2 years ago

joshkel commented 2 years ago

I'm having some problems since the upgrade to 3.1.4; Jest tests that previously worked fine are now timing out. I did some digging, and if I understand correctly:

I experimented with using Jest's fake timers to speed up my tests, but they don't appear to be a great solution: because IndexedDB operations result in fake-indexeddb timers which resolve promises which may then trigger additional IndexedDB operations (etc.), there's an awkward dance of trying to figure out which timers and promises are outstanding after each operation so that they can all be properly resolved. (See the linked test case for an example of what this looks like - this gets even harder when using third-party code that may do multiple IndexedDB operations in a row. Maybe there's an easier way to do this in Jest? If so please let me know.)

We ran into this using the idb-keyval package, which, in order to keep a simple API, relies on lots of small operations.

Test case: https://github.com/joshkel/fake-indexeddb-settimeout-test-case

Timings for the "create and read a bunch of values" from that project in fake-indexeddb 3.1.4:

In fake-indexeddb 3.1.3:

ath0mas commented 2 years ago

Same issue for me updating from 3.1.3 to 3.1.4 as I told in #67. I did not had much time to work on having a repro, just found that translating ph-fritsche/repro-jest-jsdom-indexeddb from jest to mocha is still running without such problem. So I would have to start from my own code to try and reduce it to a minimal repro.

My setup:

jleider commented 2 years ago

We just ran into issues here as well trying to get a simple test working on a standard factory open call. The setTimeout in the mocked open function was never invoked. Rolling back to 3.1.3 solved the issue for us.

Node v12.14.1

dumbmatter commented 2 years ago

Thanks for looking into this. I'm really not sure what the best solution is here, but let me try to summarize the problem as I understand it:

68 by @ph-fritsche would make it configurable whether to use setTimeout or setImmediate. But that would still not help for people facing both of the problems above - they could only fix one of those problems, not both.

There must be a better solution. Off the top of my head, I'm not sure what that is. But if someone could figure out how to either:

then that would allow me to fix this issue for all cases.

dumbmatter commented 2 years ago

Fixed in v3.1.6, thank you @joshkel and everyone else who helped debug this!