Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /**
2 * Any copyright is dedicated to the Public Domain.
3 * http://creativecommons.org/publicdomain/zero/1.0/
4 */
6 if (!this.window) {
7 this.runTest = function() {
8 todo(false, "Test disabled in xpcshell test suite for now");
9 finishTest();
10 }
11 }
13 var testGenerator = testSteps();
15 function testSteps()
16 {
17 const name = this.window ? window.location.pathname : "Splendid Test";
19 // Needs to be enough to saturate the thread pool.
20 const SYNC_REQUEST_COUNT = 25;
22 let request = indexedDB.open(name, 1);
23 request.onerror = errorHandler;
24 request.onupgradeneeded = grabEventAndContinueHandler;
25 request.onsuccess = grabEventAndContinueHandler;
26 let event = yield undefined;
28 let db = event.target.result;
29 db.onerror = errorHandler;
31 is(event.target.transaction.mode, "versionchange", "Correct mode");
33 let objectStore = db.createObjectStore("foo", { autoIncrement: true });
35 request = objectStore.add({});
36 request.onerror = errorHandler;
37 request.onsuccess = grabEventAndContinueHandler;
38 event = yield undefined;
40 let key = event.target.result;
41 ok(key, "Got a key");
43 yield undefined;
45 let continueReading = true;
46 let readerCount = 0;
47 let writerCount = 0;
48 let callbackCount = 0;
50 // Generate a bunch of reads right away without returning to the event
51 // loop.
52 info("Generating " + SYNC_REQUEST_COUNT + " readonly requests");
54 for (let i = 0; i < SYNC_REQUEST_COUNT; i++) {
55 readerCount++;
56 let request = db.transaction("foo").objectStore("foo").get(key);
57 request.onsuccess = function(event) {
58 is(event.target.transaction.mode, "readonly", "Correct mode");
59 callbackCount++;
60 };
61 }
63 while (continueReading) {
64 readerCount++;
65 info("Generating additional readonly request (" + readerCount + ")");
66 let request = db.transaction("foo").objectStore("foo").get(key);
67 request.onsuccess = function(event) {
68 callbackCount++;
69 info("Received readonly request callback (" + callbackCount + ")");
70 is(event.target.transaction.mode, "readonly", "Correct mode");
71 if (callbackCount == SYNC_REQUEST_COUNT) {
72 writerCount++;
73 info("Generating 1 readwrite request with " + readerCount +
74 " previous readonly requests");
75 let request = db.transaction("foo", "readwrite")
76 .objectStore("foo")
77 .add({}, readerCount);
78 request.onsuccess = function(event) {
79 callbackCount++;
80 info("Received readwrite request callback (" + callbackCount + ")");
81 is(event.target.transaction.mode, "readwrite", "Correct mode");
82 is(event.target.result, callbackCount,
83 "write callback came before later reads");
84 }
85 }
86 else if (callbackCount == SYNC_REQUEST_COUNT + 5) {
87 continueReading = false;
88 }
89 };
91 setTimeout(function() { testGenerator.next(); }, writerCount ? 1000 : 100);
92 yield undefined;
93 }
95 while (callbackCount < (readerCount + writerCount)) {
96 executeSoon(function() { testGenerator.next(); });
97 yield undefined;
98 }
100 is(callbackCount, readerCount + writerCount, "All requests accounted for");
102 finishTest();
103 yield undefined;
104 }