async function test()
{
    var urlResults = new Array;
    var statusResults = new Array;
    var headerResults = new Array;

    function finishThisTest()
    {
        for (var i = 0; i < urlResults.length; ++i) {
            log("URL " + i + " - " + urlResults[i]);
            log("Status code " + i + " - " + statusResults[i]);
            log("Source' header " + i + " - " + headerResults[i]);
        }
        finishSWTest();
    }
    
    try {
        var frame = await interceptedFrame("resources/basic-timeout-worker.js", "/workers/service/resources/");
        var fetch = frame.contentWindow.fetch;

        if (window.testRunner)
            testRunner.setServiceWorkerFetchTimeout(0);
        
        // The following two fetches should time out immediately
        fetch("timeout-fallback.html").then(function(response) {
            urlResults[0] = response.url;
            statusResults[0] = response.status;
            headerResults[0] = response.headers.get("Source");       
        }, function(error) {
            log(error);
        });

        fetch("timeout-no-fallback.html").then(function(response) {
            urlResults[1] = response.url;
            statusResults[1] = response.status;
            headerResults[1] = response.headers.get("Source");   
        }, function(error) {
            log(error);
        });

        if (window.testRunner)
            testRunner.setServiceWorkerFetchTimeout(60);

        // The service worker knows how to handle the following fetch *and* has 60 seconds to do so.
        // But will be cancelled with the above fetches since we're terminating the service worker, and 
        // therefore it will then fallback to the network.
        fetch("succeed-fallback-check.py").then(function(response) {
            urlResults[2] = response.url;
            statusResults[2] = response.status;
            headerResults[2] = response.headers.get("Source");   
            setTimeout(checkSuccessAgain, 0);
        }, function(error) {
            log(error);
            finishSWTest();
        });
        
        // Now we can fetch that same URL again, which *could* relaunch the service worker and handle it there, but for now this service worker registration is inert and fetches through it will go to the network instead.
        // I'm leaving this in to cover future cases where we do relaunch the SW to handle it.
        function checkSuccessAgain() {
            fetch("succeed-fallback-check.py").then(function(response) {
                urlResults[3] = response.url;
                statusResults[3] = response.status;
                headerResults[3] = response.headers.get("Source");   
                finishThisTest();
            }, function(error) {
                log(error);
                finishSWTest();
            });
        }
    } catch(e) {
        log("Got exception: " + e);
        finishSWTest();
    }
}

test();
