1function terminateTest()
2{
3    if (window.layoutTestController)
4        layoutTestController.notifyDone();
5}
6
7function openTestDatabase()
8{
9    return openDatabaseWithSuffix("ReadAndWriteTransactionsDontRunTogetherTest",
10                                  "1.0",
11                                  "Test to make sure that read and write transactions on different DB handles to the same DB don't run at the same time.",
12                                  32768);
13}
14
15var readTransactionsInProgress = 0;
16var writeTransactionsInProgress = 0;
17var totalTransactions = 0;
18var finishedTransactions = 0;
19
20function runTransaction(db, readOnly)
21{
22    var transactionFunction = (readOnly ? db.readTransaction : db.transaction);
23    transactionFunction.call(db, function(tx) {
24            if (readOnly) {
25                if (writeTransactionsInProgress != 0) {
26                    log("Read transaction starting while write transaction in progress.");
27                    terminateTest();
28                }
29                readTransactionsInProgress++;
30            } else {
31                if ((readTransactionsInProgress != 0) || (writeTransactionsInProgress != 0)) {
32                    log("Write transaction starting while another transaction in progress.");
33                    terminateTest();
34                }
35                writeTransactionsInProgress++;
36            }
37            tx.executeSql("SELECT * FROM Test;");
38        }, function(error) {
39            log((readOnly ? "Read" : "Write") + " transaction failed: " + error.message);
40            terminateTest();
41        }, function() {
42             finishedTransactions++;
43             if (readOnly)
44                 readTransactionsInProgress--;
45             else
46                 writeTransactionsInProgress--;
47             log("Transaction successful.");
48             if ((finishedTransactions == totalTransactions) && (readTransactionsInProgress == 0) && (writeTransactionsInProgress == 0))
49                 terminateTest();
50        });
51}
52
53function runReadAndWriteTransactions(db1, db2, db3)
54{
55    totalTransactions = 10;
56    finishedTransactions = 0;
57    runTransaction(db1, true);
58    runTransaction(db2, true);
59    runTransaction(db1, false);
60    runTransaction(db1, true);
61    runTransaction(db2, true);
62    runTransaction(db3, true);
63    runTransaction(db1, false);
64    runTransaction(db2, false);
65    runTransaction(db1, true);
66    runTransaction(db3, true);
67}
68
69function runTest() {
70    var db1 = openTestDatabase();
71    var db2 = openTestDatabase();
72    var db3 = openTestDatabase();
73    db1.transaction(function(tx) {
74            tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo int);");
75        }, function(error) {
76            log("Cannot create the Test table: " + error.message);
77            terminateTest();
78        }, function() {
79            runReadAndWriteTransactions(db1, db2, db3);
80        });
81}
82