summaryrefslogtreecommitdiffstats
path: root/dom/indexedDB/test/unit/test_table_locks.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /dom/indexedDB/test/unit/test_table_locks.js
parentInitial commit. (diff)
downloadfirefox-upstream/124.0.1.tar.xz
firefox-upstream/124.0.1.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/indexedDB/test/unit/test_table_locks.js')
-rw-r--r--dom/indexedDB/test/unit/test_table_locks.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/dom/indexedDB/test/unit/test_table_locks.js b/dom/indexedDB/test/unit/test_table_locks.js
new file mode 100644
index 0000000000..e1f3b5e7d6
--- /dev/null
+++ b/dom/indexedDB/test/unit/test_table_locks.js
@@ -0,0 +1,123 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+const dbName = "window" in this ? window.location.pathname : "test";
+const dbVersion = 1;
+const objName1 = "o1";
+const objName2 = "o2";
+const idxName1 = "i1";
+const idxName2 = "i2";
+const idxKeyPathProp = "idx";
+const objDataProp = "data";
+const objData = "1234567890";
+const objDataCount = 5;
+const loopCount = 100;
+
+/* exported testGenerator */
+var testGenerator = testSteps();
+
+function* testSteps() {
+ let req = indexedDB.open(dbName, dbVersion);
+ req.onerror = errorHandler;
+ req.onupgradeneeded = grabEventAndContinueHandler;
+ req.onsuccess = grabEventAndContinueHandler;
+
+ let event = yield undefined;
+
+ is(event.type, "upgradeneeded", "Got upgradeneeded event");
+
+ let db = event.target.result;
+
+ let objectStore1 = db.createObjectStore(objName1);
+ objectStore1.createIndex(idxName1, idxKeyPathProp);
+
+ let objectStore2 = db.createObjectStore(objName2);
+ objectStore2.createIndex(idxName2, idxKeyPathProp);
+
+ for (let i = 0; i < objDataCount; i++) {
+ var data = {};
+ data[objDataProp] = objData;
+ data[idxKeyPathProp] = objDataCount - i - 1;
+
+ objectStore1.add(data, i);
+ objectStore2.add(data, i);
+ }
+
+ event = yield undefined;
+
+ is(event.type, "success", "Got success event");
+
+ doReadOnlyTransaction(db, 0, loopCount);
+ doReadWriteTransaction(db, 0, loopCount);
+
+ // Wait for readonly and readwrite transaction loops to complete.
+ yield undefined;
+ yield undefined;
+
+ finishTest();
+}
+
+function doReadOnlyTransaction(db, key, remaining) {
+ if (!remaining) {
+ info("Finished all readonly transactions");
+ continueToNextStep();
+ return;
+ }
+
+ info(
+ "Starting readonly transaction for key " +
+ key +
+ ", " +
+ remaining +
+ " loops left"
+ );
+
+ let objectStore = db.transaction(objName1, "readonly").objectStore(objName1);
+ let index = objectStore.index(idxName1);
+
+ index.openKeyCursor(key, "prev").onsuccess = function (event) {
+ let cursor = event.target.result;
+ ok(cursor, "Got readonly cursor");
+
+ objectStore.get(cursor.primaryKey).onsuccess = function (event) {
+ if (++key == objDataCount) {
+ key = 0;
+ }
+ doReadOnlyTransaction(db, key, remaining - 1);
+ };
+ };
+}
+
+function doReadWriteTransaction(db, key, remaining) {
+ if (!remaining) {
+ info("Finished all readwrite transactions");
+ continueToNextStep();
+ return;
+ }
+
+ info(
+ "Starting readwrite transaction for key " +
+ key +
+ ", " +
+ remaining +
+ " loops left"
+ );
+
+ let objectStore = db.transaction(objName2, "readwrite").objectStore(objName2);
+ objectStore.openCursor(key).onsuccess = function (event) {
+ let cursor = event.target.result;
+ ok(cursor, "Got readwrite cursor");
+
+ let value = cursor.value;
+ value[idxKeyPathProp]++;
+
+ cursor.update(value).onsuccess = function (event) {
+ if (++key == objDataCount) {
+ key = 0;
+ }
+ doReadWriteTransaction(db, key, remaining - 1);
+ };
+ };
+}