Skip to content

Commit

Permalink
Made the test even more naughty by spawning async function.
Browse files Browse the repository at this point in the history
Removed the old test that never reproduced anything.
  • Loading branch information
dfahlander committed Nov 29, 2023
1 parent d3d28a0 commit 200bf86
Showing 1 changed file with 12 additions and 57 deletions.
69 changes: 12 additions & 57 deletions test/tests-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,17 @@ promisedTest("Issue - ReadonlyError thrown in liveQuery despite user did not do
equal(Dexie.Promise.PSD.id, 'global', "PSD is the global PSD");
const observable = liveQuery(async () => {
console.debug("liveQuery executing");
const result = db.metrics.toArray();
console.debug("physicalTick executed by toArray");
return await result;
const result = await db.metrics.toArray();
//await 3;
async function foo() {
console.log("qm PSD.id = " + Dexie.Promise.PSD?.id);
await db.metrics.toArray();
console.log("qm PSD.id = " + Dexie.Promise.PSD?.id);
}
foo(); // Be naughty and spawn promises that we don't await.
// Verify that we handle this situation and escape from zone echoing before
// we return the result.
return result;
});
equal(Dexie.Promise.PSD.id, 'global', "PSD is the global PSD");
Expand All @@ -446,6 +454,7 @@ promisedTest("Issue - ReadonlyError thrown in liveQuery despite user did not do
await new Promise(resolve => {
const o = observable.subscribe(val => {
o.unsubscribe();
console.log("PSD.id = " + Dexie.Promise.PSD?.id);
resolve(val);
});
});
Expand All @@ -457,57 +466,3 @@ promisedTest("Issue - ReadonlyError thrown in liveQuery despite user did not do
`);
return F(ok, equal, Dexie, db, liveQuery).catch(err => ok(false, 'final catch: '+err));
});

/** Try to reproduce customer issue where ReadonlyError was thrown when using liveQuery.
* This repro is not good enough though as it doesn't fail in [email protected].
* Probably need to reproduce it in a more complex scenario with
* multiple liveQueries and transactions running in parallel.
* However, the issue is fixed with this same commit which will be
* [email protected]. It is verified with customer application.
*
* Keeping the test in case there will be time to try to improve it later.
*/
promisedTest("Issue - ReadonlyError thrown in liveQuery despite user did not do write transactions", async () => {
// Encapsulating the code in a string to avoid transpilation. We need native await here to trigger bug.
ok(!Promise.PSD, "Must not be within async context when starting");
ok(db.isOpen(), "DB must be open when starting");
await new Promise(resolve => setTimeout(resolve, 10));
const F = new Function('ok', 'equal', 'Dexie', 'db', 'liveQuery', `
ok(true, "Got here");
return (async ()=>{
let physicalTickScheduled = false;
const observable = liveQuery(async () => {
console.debug("liveQuery executing");
//debugger;
const result = db.transaction('r', 'metrics', async () => db.metrics.toArray());
physicalTickScheduled = true;
console.debug("physicalTick executed by toArray");
return await result;
});
const subscription = observable.subscribe({
next: function (result) {
ok(true, "Got next result from observable");
}
});
ok(!!physicalTickScheduled, "physicalTick is scheduled at this point");
console.debug("liveQuery subscribed. Now doing transaction immediately - it will push to microtickQueue");
try {
//debugger;
//db.transaction('rw', db.metrics, () => {
// const x = Promise.PSD;
await db.metrics.add({ id: "id1", name: "a", time: 1 }).then(() => {
//debugger;
return db.metrics.update("id1", {name: "b"});
});
//});
console.debug("Transaction succeeded");
ok(true, "Successfully executed transaction");
} catch (error) {
console.debug("Transaction failed");
ok(false, 'Failed to execute transaction due to ' + error);
}
subscription.unsubscribe();
})();
`);
return F(ok, equal, Dexie, db, liveQuery).catch(err => ok(false, 'final catch: '+err));
});

0 comments on commit 200bf86

Please sign in to comment.