15af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkefunction terminateTest()
25af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke{
35af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    if (window.layoutTestController)
45af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        layoutTestController.notifyDone();
55af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke}
65af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
75af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkefunction openTestDatabase()
85af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke{
95af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    return openDatabaseWithSuffix("ReadAndWriteTransactionsDontRunTogetherTest",
105af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                                  "1.0",
115af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                                  "Test to make sure that read and write transactions on different DB handles to the same DB don't run at the same time.",
125af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                                  32768);
135af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke}
145af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
155af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkevar readTransactionsInProgress = 0;
165af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkevar writeTransactionsInProgress = 0;
175af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkevar totalTransactions = 0;
185af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkevar finishedTransactions = 0;
195af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
205af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkefunction runTransaction(db, readOnly)
215af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke{
225af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    var transactionFunction = (readOnly ? db.readTransaction : db.transaction);
235af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    transactionFunction.call(db, function(tx) {
245af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            if (readOnly) {
255af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                if (writeTransactionsInProgress != 0) {
265af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                    log("Read transaction starting while write transaction in progress.");
275af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                    terminateTest();
285af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                }
295af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                readTransactionsInProgress++;
305af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            } else {
315af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                if ((readTransactionsInProgress != 0) || (writeTransactionsInProgress != 0)) {
325af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                    log("Write transaction starting while another transaction in progress.");
335af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                    terminateTest();
345af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                }
355af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                writeTransactionsInProgress++;
365af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            }
375af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            tx.executeSql("SELECT * FROM Test;");
385af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        }, function(error) {
395af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            log((readOnly ? "Read" : "Write") + " transaction failed: " + error.message);
405af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            terminateTest();
415af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        }, function() {
425af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke             finishedTransactions++;
435af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke             if (readOnly)
445af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                 readTransactionsInProgress--;
455af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke             else
465af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                 writeTransactionsInProgress--;
475af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke             log("Transaction successful.");
485af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke             if ((finishedTransactions == totalTransactions) && (readTransactionsInProgress == 0) && (writeTransactionsInProgress == 0))
495af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke                 terminateTest();
505af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        });
515af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke}
525af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
535af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkefunction runReadAndWriteTransactions(db1, db2, db3)
545af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke{
555af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    totalTransactions = 10;
565af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    finishedTransactions = 0;
575af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db1, true);
585af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db2, true);
595af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db1, false);
605af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db1, true);
615af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db2, true);
625af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db3, true);
635af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db1, false);
645af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db2, false);
655af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db1, true);
665af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    runTransaction(db3, true);
675af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke}
685af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke
695af96e2c7b73ebc627c6894727826a7576d31758Leon Clarkefunction runTest() {
705af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    var db1 = openTestDatabase();
715af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    var db2 = openTestDatabase();
725af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    var db3 = openTestDatabase();
735af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke    db1.transaction(function(tx) {
745af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            tx.executeSql("CREATE TABLE IF NOT EXISTS Test (Foo int);");
755af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        }, function(error) {
765af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            log("Cannot create the Test table: " + error.message);
775af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            terminateTest();
785af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        }, function() {
795af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke            runReadAndWriteTransactions(db1, db2, db3);
805af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke        });
815af96e2c7b73ebc627c6894727826a7576d31758Leon Clarke}
82