summaryrefslogtreecommitdiffstats
path: root/dom/cache/test/mochitest/test_cache_worker_gc.html
blob: 798bafaf9bd114452d6fa4a5ee18ed0db262b99c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Test CacheStorage does not prevent worker GC</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="text/javascript" src="driver.js"></script>
</head>
<body>
<iframe id="frame"></iframe>
<script class="testbody" type="text/javascript">
function startWorker() {
  return new Promise(resolve => {
    var w = new Worker("idle_worker.js");
    w.addEventListener("message", function onMessage(evt) {
      if (evt.data === "LOADED") {
        w.removeEventListener("message", onMessage);
        resolve(w);
      }
    });
  });
}

function gc() {
  return new Promise(resolve => {
    SpecialPowers.exactGC(resolve);
  });
}

SimpleTest.waitForExplicitFinish();

async function test() {
  let w = await startWorker();
  let weakWorker = SpecialPowers.Cu.getWeakReference(w);
  ok(weakWorker, "worker supports weak reference");
  w = null;
  await gc();
  await gc();
  ok(!weakWorker.get(), "worker weak reference should be garbage collected");
  SimpleTest.finish();
}
// Bug 1746646: Make mochitests work with TCP enabled (cookieBehavior = 5)
// Acquire storage access permission here so that the Cache API is avaialable
SpecialPowers.wrap(document).notifyUserGestureActivation();
SpecialPowers.addPermission("storageAccessAPI", true, window.location.href).then(() => {
  SpecialPowers.wrap(document).requestStorageAccess().then(() => {
    SpecialPowers.pushPrefEnv({
      set: [["privacy.partition.always_partition_third_party_non_cookie_storage", false]],
    }).then(() => {
      test();
    });
  });
});
</script>
</body>
</html>