dom/indexedDB/test/unit/test_writer_starvation.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial