0e6efd74f6e841896f9e80e7908c72de752771aa
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / resources / transaction-scheduler-5.js
1 description("This test creates two object stores. \
2 It then opens two long running read-only transactions, one per object store. \
3 It then opens a read-write transaction to both object stores. \
4 The read-only transactions both need to finish before the read-write transaction starts.");
5
6
7 if (window.testRunner) {
8     testRunner.waitUntilDone();
9     testRunner.dumpAsText();
10 }
11
12 function done()
13 {
14     finishJSTest();
15 }
16
17 var createRequest = window.indexedDB.open("TransactionScheduler5Database");
18
19 createRequest.onupgradeneeded = function(event) {
20     debug("ALERT: " + "Upgrade needed: Old version - " + event.oldVersion + " New version - " + event.newVersion);
21
22     var versionTransaction = createRequest.transaction;
23     var database = event.target.result;
24     var objectStore = database.createObjectStore("OS1");
25     var request = objectStore.put("bar", "foo");
26
27     request.onerror = function(event) {
28         debug("ALERT: " + "first put FAILED - " + event);
29         done();
30     }
31
32     objectStore = database.createObjectStore("OS2");
33     request = objectStore.put("bar", "foo");
34     
35     request.onerror = function(event) {
36         debug("ALERT: " + "second put FAILED - " + event);
37         done();
38     }
39     
40     versionTransaction.onabort = function(event) {
41         debug("ALERT: " + "versionchange transaction aborted");
42         done();
43     }
44
45     versionTransaction.oncomplete = function(event) {
46         debug("ALERT: " + "versionchange transaction completed");
47         continueTest();
48         database.close();
49     }
50
51     versionTransaction.onerror = function(event) {
52         debug("ALERT: " + "versionchange transaction error'ed - " + event);
53         done();
54     }
55 }
56
57 var secondDatabaseConnection;
58 var readWriteTransaction;
59
60 function setupReadTransactionCallbacks(transaction)
61 {
62     transaction.onerror = function(event) {
63         debug("ALERT: " + "Unexpected transaction error - " + event);
64         done();
65     }
66
67     transaction.onabort = function(event) {
68         debug("ALERT: " + "Unexpected transaction abort - " + event);
69         done();
70     }
71
72     transaction.oncomplete = function(event) {
73         debug("ALERT: " + "Read transaction complete - " + event);
74     }
75
76     transaction.hasDoneFirstRead = false;
77 }
78
79 function continueTest()
80 {
81     var openDBRequest = window.indexedDB.open("TransactionScheduler5Database", 1);
82     openDBRequest.onsuccess = function(event) {
83         debug("ALERT: " + "Success opening database connection - Starting transactions");
84         secondDatabaseConnection = event.target.result;
85         
86         var transaction1 = secondDatabaseConnection.transaction("OS1", "readonly");
87         setupReadTransactionCallbacks(transaction1);
88         var transaction2 = secondDatabaseConnection.transaction("OS2", "readonly");
89         setupReadTransactionCallbacks(transaction2);
90
91         readTransactionLoop(transaction1, "OS1", false);
92         readTransactionLoop(transaction2, "OS2", true);
93     }
94     openDBRequest.onerror = function(event) {
95         debug("ALERT: " + "Long running read request unexpected error - " + event);
96         done();
97     }
98     openDBRequest.onblocked = function(event) {
99         debug("ALERT: " + "Long running read request unexpected blocked - " + event);
100         done();
101     }
102     openDBRequest.onupgradeneeded = function(event) {
103         debug("ALERT: " + "Long running read request unexpected upgradeneeded - " + event);
104         done();
105     } 
106 }
107
108 var shouldEndReadTransaction = false;
109
110 function readTransactionLoop(transaction, osname, shouldStartWrite)
111 {
112     var objectStore = transaction.objectStore(osname);
113     var request = objectStore.get("foo");
114     
115     request.onsuccess = function(event) {
116         if (shouldEndReadTransaction && transaction.hasDoneFirstRead)
117             return;
118
119         transaction.hasDoneFirstRead = true;
120         readTransactionLoop(event.target.transaction, osname, false);
121         
122         if (shouldStartWrite)
123              startWriteTransaction();
124     }
125
126     request.onerror = function(event) {
127         debug("ALERT: " + "Unexpected request error - " + event);
128         done();
129     }
130 }
131
132 function startWriteTransaction()
133 {
134     debug("ALERT: " + "Starting write transaction");
135     var transaction = secondDatabaseConnection.transaction(["OS1", "OS2"], "readwrite");
136     var objectStore = transaction.objectStore("OS1");
137     var request = objectStore.put("baz", "foo");
138
139     shouldEndReadTransaction = true;
140
141     request.onsuccess = function(event) {
142         debug("ALERT: " + "Write to OS1 successful");
143     }
144     
145     request.onerror = function(event) {
146         debug("ALERT: " + "Write transaction put unexpected error - " + event);
147         done();
148     }
149
150     objectStore = transaction.objectStore("OS2");
151     request = objectStore.put("baz", "foo");
152
153     request.onsuccess = function(event) {
154         debug("ALERT: " + "Write to OS2 successful");
155         done();
156     }
157     
158     request.onerror = function(event) {
159         debug("ALERT: " + "Write transaction put unexpected error - " + event);
160         done();
161     }
162 }
163
164