83789cbeac0d1fdcd49dcca7c8d4e109634073fe
[WebKit-https.git] / LayoutTests / storage / indexeddb / modern / resources / transaction-scheduler-2.js
1 description("This test makes sure that two read-only transactions to two different object stores are active at the same time.");
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("TransactionScheduler2Database");
15
16 createRequest.onupgradeneeded = function(event) {
17     debug("ALERT: " + "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("OS1");
22     var request = objectStore.put("foo1", "bar1");
23
24     request.onerror = function(event) {
25         debug("ALERT: " + "put1 FAILED - " + event);
26         done();
27     }
28     
29     objectStore = database.createObjectStore("OS2");
30     request = objectStore.put("foo2", "bar2");
31
32     request.onerror = function(event) {
33         debug("ALERT: " + "put2 FAILED - " + event);
34         done();
35     }
36
37     versionTransaction.onabort = function(event) {
38         debug("ALERT: " + "versionchange transaction aborted");
39         done();
40     }
41
42     versionTransaction.oncomplete = function(event) {
43         debug("ALERT: " + "versionchange transaction completed");
44         continueTest();
45         database.close();
46     }
47
48     versionTransaction.onerror = function(event) {
49         debug("ALERT: " + "versionchange transaction error'ed - " + event);
50         done();
51     }
52 }
53
54 function continueTest()
55 {
56     var request1 = window.indexedDB.open("TransactionScheduler2Database", 1);
57     var request2 = window.indexedDB.open("TransactionScheduler2Database", 1);
58
59     setupRequest(request1, "OS1");
60     setupRequest(request2, "OS2");
61 }
62
63 function setupRequest(request, osname)
64 {
65     request.onsuccess = function(event) {
66         debug("ALERT: " + "Success opening database connection - Starting transaction to ObjectStore " + osname);
67         startTransactionLoop(event.target.result.transaction(osname, "readonly"), osname, true);
68     }
69     request.onerror = function(event) {
70         debug("ALERT: " + "Unexpected error - " + osname + " - " + event);
71         done();
72     }
73     request.onblocked = function(event) {
74         debug("ALERT: " + "Unexpected blocked - " + osname + " - " + event);
75         done();
76     }
77     request.onupgradeneeded = function(event) {
78         debug("ALERT: " + "Unexpected upgradeneeded - " + osname + " - " + event);
79         done();
80     } 
81 }
82
83 var numberOfOpenTransactions = 0;
84
85 function startTransactionLoop(transaction, osname, isFirstTime)
86 {
87     var objectStore = transaction.objectStore(osname);
88     var request = objectStore.get("bar");
89     
90     request.onsuccess = function(event) {
91         if (isFirstTime)
92             numberOfOpenTransactions++;
93         
94         if (numberOfOpenTransactions == 2) {
95             debug("ALERT: " + "Two transactions open at once. Yay.");
96             done();
97         }
98         startTransactionLoop(event.target.transaction, osname, false);
99     }
100
101     request.onerror = function(event) {
102         debug("ALERT: " + "Unexpected request error - " + event);
103         done();
104     }
105
106     transaction.onerror = function(event) {
107         debug("ALERT: " + "Unexpected transaction error - " + event);
108         done();
109     }
110
111     transaction.onabort = function(event) {
112         --numberOfOpenTransactions;
113         debug("ALERT: " + "Unexpected transaction abort - " + event);
114         done();
115     }
116
117     transaction.oncomplete = function(event) {
118         --numberOfOpenTransactions;
119         debug("ALERT: " + "Unexpected transaction complete - " + event);
120         done();
121     }
122 }
123
124