summaryrefslogtreecommitdiffstats
path: root/LayoutTests/storage/read-and-write-transactions-dont-run-together.js
blob: 5b817bad3307371d7ad199b40bf119b5b05637d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function terminateTest()
{
    if (window.layoutTestController)
        layoutTestController.notifyDone();
}

function openTestDatabase()
{
    return openDatabaseWithSuffix("ReadAndWriteTransactionsDontRunTogetherTest",
                                  "1.0",
                                  "Test to make sure that read and write transactions on different DB handles to the same DB don't run at the same time.",
                                  32768);
}

var readTransactionsInProgress = 0;
var writeTransactionsInProgress = 0;
var totalTransactions = 0;
var finishedTransactions = 0;

function runTransaction(db, readOnly)
{
    var transactionFunction = (readOnly ? db.readTransaction : db.transaction);
    transactionFunction.call(db, function(tx) {
            if (readOnly) {
                if (writeTransactionsInProgress != 0) {
                    log("Read transaction starting while write transaction in progress.");
                    terminateTest();
                }
                readTransactionsInProgress++;
            } else {
                if ((readTransactionsInProgress != 0) || (writeTransactionsInProgress != 0)) {
                    log("Write transaction starting while another transaction in progress.");
                    terminateTest();
                }
                writeTransactionsInProgress++;
            }
            tx.executeSql("SELECT * FROM Test;");
        }, function(error) {
            log((readOnly ? "Read" : "Write") + " transaction failed: " + error.message);
            terminateTest();
        }, function() {
             finishedTransactions++;
             if (readOnly)
                 readTransactionsInProgress--;
             else
                 writeTransactionsInProgress--;
             log("Transaction successful.");
             if ((finishedTransactions == totalTransactions) && (readTransactionsInProgress == 0) && (writeTransactionsInProgress == 0))
                 terminateTest();
        });
}

function runReadAndWriteTransactions(db1, db2, db3)
{
    totalTransactions = 10;
    finishedTransactions = 0;
    runTransaction(db1, true);
    runTransaction(db2, true);
    runTransaction(db1, false);
    runTransaction(db1, true);
    runTransaction(db2, true);
    runTransaction(db3, true);
    runTransaction(db1, false);
    runTransaction(db2, false);
    runTransaction(db1, true);
    runTransaction(db3, true);
}

function runTest() {
    var db1 = openTestDatabase();
    var db2 = openTestDatabase();
    var db3 = openTestDatabase();
    db1.transaction(function(tx) {
            tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo int);");
        }, function(error) {
            log("Cannot create the Test table: " + error.message);
            terminateTest();
        }, function() {
            runReadAndWriteTransactions(db1, db2, db3);
        });
}