Skip to content

Commit

Permalink
Added more code coverage for the repro of 2058
Browse files Browse the repository at this point in the history
  • Loading branch information
David Fahlander committed Oct 14, 2024
1 parent 9b43356 commit d1d6a09
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions test/tests-live-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Signal {
promise = new Promise(resolve => this.resolve = resolve);
}

function liveQueryUnitTester(lq) {
function liveQueryUnitTester(lq, {graceTime}={graceTime: 0}) {
let currentVal;
let currentError = null;
let signal = ()=>{};
Expand All @@ -52,6 +52,7 @@ function liveQueryUnitTester(lq) {
++querying;
try {
currentVal = await lq();
console.log("Emitted", currentVal);
currentError = null;
} catch (error) {
currentError = error;
Expand All @@ -63,7 +64,7 @@ function liveQueryUnitTester(lq) {
else
signal(currentVal);
}
}, 0);
}, graceTime);
}).subscribe(x => x);
return {
waitNextValue(timeout=500) {
Expand Down Expand Up @@ -832,7 +833,9 @@ promisedTest("Issue 2067: useLiveQuery does not update when multiple items are d
});

promisedTest("Issue 2058: Cache error after bulkPut failures", async () => {
const tester = liveQueryUnitTester(()=>db.items.toArray());
const tester = liveQueryUnitTester(
()=>db.items.toArray()
);
let items = await tester.waitNextValue();
deepEqual(items, [{id:1},{id:2},{id:3}], "Initial items are correct");
let promise = tester.waitNextValue();
Expand All @@ -844,5 +847,35 @@ promisedTest("Issue 2058: Cache error after bulkPut failures", async () => {
});
items = await promise;
deepEqual(items, [{id:1},{id:2},{id:3},{id:88}], "The livequery emitted correct result after bulk operation");
// Now making sure we go through a different code path (where the number of items > 50 in cache-middleware.ts)
const itemsToAdd = new Array(51)
.fill({id: 1}, 0, 40) // Positions 0..40 is constraint violations agains existing data + themselves
.fill({id:99}, 40, 49) // Positions 40 is new value but 41...49 is constraint violation of pos 40.
.fill({id:100}, 49, 50) // Position 50 is ok.
.fill({id:101}, 50) // Position 51 is ok.

await db.items.bulkAdd(itemsToAdd).catch(error => {
deepEqual(Object.keys(error.failuresByPos).map(Number),[
0,1,2,3,4,5,6,7,8,9,
10,11,12,13,14,15,16,17,18,19,
20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,
41,42,43,44,45,46,47,48
]
, "We get errors on the expected positions");
});

console.log("Before await promise", performance.now());
items = await tester.waitNextValue();
console.log("After await promise", performance.now());
deepEqual(items, [
{id:1},
{id:2},
{id:3},
{id:88},
{id:99},
{id:100},
{id:101}
], "Correct state after trying to add these half baked entries");
tester.subscription.unsubscribe();
});

0 comments on commit d1d6a09

Please sign in to comment.