Modern IDB: Remove unneeded "Alert: " string from storage/indexeddb/modern tests.
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / resources / transaction-scheduler-3.js
1 description("This test makes sure that a write transaction is blocked by a read transaction with overlapping scope. It also makes sure the write transaction starts after the read transaction completes.");
2
3
4 if (window.testRunner) {
5     testRunner.waitUntilDone();
6     testRunner.dumpAsText();
7 }
8
9 function done()
10 {
11     finishJSTest();
12 }
13
14 var createRequest = window.indexedDB.open("TransactionScheduler3Database");
15
16 createRequest.onupgradeneeded = function(event) {
17     debug("Upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
18
19     var versionTransaction = createRequest.transaction;
20     var database = event.target.result;
21     var objectStore = database.createObjectStore("OS");
22     var request = objectStore.put("bar", "foo");
23
24     request.onerror = function(event) {
25         debug("put FAILED - " + event);
26         done();
27     }
28
29     versionTransaction.onabort = function(event) {
30         debug("versionchange transaction aborted");
31         done();
32     }
33
34     versionTransaction.oncomplete = function(event) {
35         debug("versionchange transaction completed");
36         continueTest();
37         database.close();
38     }
39
40     versionTransaction.onerror = function(event) {
41         debug("versionchange transaction error'ed - " + event);
42         done();
43     }
44 }
45
46 var secondDatabaseConnection;
47 function continueTest()
48 {
49     var longRunningReadRequest = window.indexedDB.open("TransactionScheduler3Database", 1);
50     longRunningReadRequest.onsuccess = function(event) {
51         debug("Success opening database connection - Starting readonly transaction");
52         secondDatabaseConnection = event.target.result;
53         readTransactionLoop(secondDatabaseConnection.transaction("OS", "readonly"), true);
54     }
55     longRunningReadRequest.onerror = function(event) {
56         debug("Long running read request unexpected error - " + event);
57         done();
58     }
59     longRunningReadRequest.onblocked = function(event) {
60         debug("Long running read request unexpected blocked - " + event);
61         done();
62     }
63     longRunningReadRequest.onupgradeneeded = function(event) {
64         debug("Long running read request unexpected upgradeneeded - " + event);
65         done();
66     } 
67 }
68
69 var shouldEndReadTransaction = false;
70
71 function readTransactionLoop(transaction, isFirstTime)
72 {
73     var objectStore = transaction.objectStore("OS");
74     var request = objectStore.get("foo");
75     
76     request.onsuccess = function(event) {
77         if (!shouldEndReadTransaction)
78             readTransactionLoop(event.target.transaction, false);
79         
80         // Now that the read transaction is open, the write transaction can be started, but it will be blocked to start with.
81         if (isFirstTime)
82              startWriteTransaction()
83     }
84
85     request.onerror = function(event) {
86         debug("Unexpected request error - " + event);
87         done();
88     }
89
90     transaction.onerror = function(event) {
91         debug("Unexpected transaction error - " + event);
92         done();
93     }
94
95     transaction.onabort = function(event) {
96         debug("Unexpected transaction abort - " + event);
97         done();
98     }
99
100     transaction.oncomplete = function(event) {
101         debug("Read transaction complete - " + event);
102     }
103 }
104
105 function startWriteTransaction()
106 {
107     debug("Creating write transaction");
108     var transaction = secondDatabaseConnection.transaction("OS", "readwrite");
109     var objectStore = transaction.objectStore("OS");
110     var request = objectStore.put("baz", "foo");
111
112     setTimeout("shouldEndReadTransaction = true;", 0);
113
114     request.onsuccess = function(event) {
115         debug("Write transaction put success");
116     }
117
118     request.onerror = function(event) {
119         debug("Write transaction put unexpected error - " + event);
120         done();
121     }
122
123     transaction.onerror = function(event) {
124         debug("Write transaction unexpected error - " + event);
125         done();
126     }
127
128     transaction.onabort = function(event) {
129         debug("Write transaction unexpected abort - " + event);
130         done();
131     }
132
133     transaction.oncomplete = function(event) {
134         debug("Write transaction complete - " + event);
135         done();
136     }
137 }
138
139