From 3eced74a88de43ab9afe542fcce20a8db8e3fe60 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 22 Feb 2021 14:27:54 +0100 Subject: more tests, fix event ordering issue --- .../idb-bridge/src/idb-wpt-ported/wptsupport.ts | 64 +++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) (limited to 'packages/idb-bridge/src/idb-wpt-ported/wptsupport.ts') diff --git a/packages/idb-bridge/src/idb-wpt-ported/wptsupport.ts b/packages/idb-bridge/src/idb-wpt-ported/wptsupport.ts index 6777dc122..9ec46c765 100644 --- a/packages/idb-bridge/src/idb-wpt-ported/wptsupport.ts +++ b/packages/idb-bridge/src/idb-wpt-ported/wptsupport.ts @@ -1,5 +1,5 @@ import test, { ExecutionContext } from "ava"; -import { BridgeIDBFactory } from ".."; +import { BridgeIDBFactory, BridgeIDBRequest } from ".."; import { IDBDatabase, IDBIndex, @@ -480,3 +480,65 @@ export function indexeddb_test( } }); } + +/** + * Keeps the passed transaction alive indefinitely (by making requests + * against the named store). Returns a function that asserts that the + * transaction has not already completed and then ends the request loop so that + * the transaction may autocommit and complete. + */ +export function keep_alive( + t: ExecutionContext, + tx: IDBTransaction, + store_name: string, +) { + let completed = false; + tx.addEventListener("complete", () => { + completed = true; + }); + + let keepSpinning = true; + let spinCount = 0; + + function spin() { + console.log("spinning"); + if (!keepSpinning) return; + const request = tx.objectStore(store_name).get(0); + (request as BridgeIDBRequest)._debugName = `req-spin-${spinCount}`; + spinCount++; + request.onsuccess = spin; + } + spin(); + + return () => { + t.log("stopping spin"); + t.false(completed, "Transaction completed while kept alive"); + keepSpinning = false; + }; +} + +// Checks to see if the passed transaction is active (by making +// requests against the named store). +export function is_transaction_active( + t: ExecutionContext, + tx: IDBTransaction, + store_name: string, +) { + try { + const request = tx.objectStore(store_name).get(0); + request.onerror = (e) => { + e.preventDefault(); + e.stopPropagation(); + }; + return true; + } catch (ex) { + console.log(ex.stack); + t.deepEqual( + ex.name, + "TransactionInactiveError", + "Active check should either not throw anything, or throw " + + "TransactionInactiveError", + ); + return false; + } +} -- cgit v1.2.3