<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script>
    const RESOURCES_DIR = "/WebKit/beacon/resources/";

    // We should expect 64KiB of rolling quota for any type of keep-alive request sent.
    var expectedQuota = 65536;

    function waitForBeaconCompletion()
    {
        return new Promise(function(resolve, reject) {
            if (!internals.inflightBeaconsCount) {
                resolve();
                return;
            }
            setTimeout(function() {
                waitForBeaconCompletion().then(resolve, reject);
            }, 10);
        });
    }

    function createPayload(payloadSize)
    {
        return new Blob(["*".repeat(payloadSize)]);
    }

    test(function() {
        assert_false(navigator.sendBeacon("/", createPayload(expectedQuota + 1)));
    }, "Beacon with a body above the Quota Limit should fail.");

    promise_test(function() {
        var id = self.token();
        var target = RESOURCES_DIR + "beacon-preflight.py?allowCors=1&cmd=put&id=" + id;

        assert_true(navigator.sendBeacon(target, createPayload(expectedQuota)), "Beacon with a body at the Quota Limit should succeed.");
        assert_equals(internals.inflightBeaconsCount, 1);
        assert_false(navigator.sendBeacon("/", createPayload(1)), "Second beacon should not be sent because we reached the quota");
        return waitForBeaconCompletion().then(function() {
            assert_true(navigator.sendBeacon(target, createPayload(1)), "Allocated quota should be returned once the beacon is no longer in flight");
        });
    }, "Multiple Beacons Quota Limit");
</script>
