<html>
<head>
<script src="../../resources/js-test.js"></script>
<script src="resources/shared.js"></script>
</head>
<body>
<script>
description("Explicitly terminating worker with blocked call should allow later open calls to proceed");

function test()
{
    removeVendorPrefixes();
    setDBNameFromPath();
    if (self.location.search !== "?second") {
        firstOpen();
    } else {
        secondOpen();
    }
}

function firstOpen() {
    request = evalAndLog("indexedDB.deleteDatabase(dbname)");
    request.onblocked = unexpectedBlockedCallback;
    request.onerror = unexpectedErrorCallback;
    request.onsuccess = function() {
        request = evalAndLog("indexedDB.open(dbname)");
        request.onblocked = unexpectedBlockedCallback;
        request.onerror = unexpectedErrorCallback;
        request.onsuccess = startTheWorker;
    };
}

function secondOpen() {
    request = evalAndLog("indexedDB.open(dbname)");
    request.onblocked = unexpectedBlockedCallback;
    request.onerror = unexpectedErrorCallback;
    request.onupgradeneeded = unexpectedUpgradeNeededCallback;
    request.onsuccess = function() {
        testPassed("Open worked after page reload.");
        finishJSTest();
    };
}

function startTheWorker()
{
    worker = startWorker("resources/pending-version-change-stuck.js?" + encodeURIComponent(dbname));
    worker.onerror = function(e) {
        testFailed(e.message);
        finishJSTest();
    };
    originalOnMessage = worker.onmessage;
    worker.onmessage = function(event) {
        if (event.data == "gotblocked") {
            worker.terminate();
            self.location = self.location + "?second";
            return;
        }

        originalOnMessage(event);
    };
}

test();

</script>
</body>
</html>
