<html>
<head>
<script>

function log(message)
{
    document.body.innerHTML += message + "<br>";
}

function terminateTest()
{
    if (window.testRunner)
        testRunner.notifyDone();
}

function openTestDatabase()
{
    return openDatabase("ReadTransactionsRunningConcurrentlyTest",
                        "1.0",
                        "Test to make sure that multiple read transactions on different DB handles to the same DB run concurrently.",
                        32768);
}

var readTransactionsInProgress = 0;

function runReadTransaction(db)
{
    db.readTransaction(function(tx) {
            readTransactionsInProgress++;
        }, function(error) {
            log("Read transaction failed: " + error.message);
            terminateTest();
        }, function() {
            if (readTransactionsInProgress == 2)
                log("Read transactions running concurrently.");
            readTransactionsInProgress--;
            if (readTransactionsInProgress == 0)
                terminateTest();
        });
}

function runTest() {
    if (window.testRunner) {
        testRunner.clearAllDatabases();
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
    }

    try {
        var db1 = openTestDatabase();
        var db2 = 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() {
                runReadTransaction(db1);
                runReadTransaction(db2);
            });
     } catch(err) { log(err); }
}
</script>
</head>
<body onload="runTest();">
This test tests that two read-only transactions on different handles to the same database run concurrently.<br>
</body>
</html>
