aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/fetch/fetch-leak-test-fixture.js
blob: 07275a42512f993bb1351914cd4d7d970dc80d55 (plain) (blame)
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
import { heapStats } from "bun:jsc";

const { SERVER } = process.env;

if (typeof SERVER === "undefined" || !SERVER?.length) {
  throw new Error("SERVER environment variable is not set");
}

const COUNT = parseInt(process.env.COUNT || "50", 10);
await (async function runAll() {
  var fetches = new Array(COUNT);
  let i = 0;
  while (i < Math.max(COUNT - 32, 0)) {
    for (let j = 0; j < 32; j++) {
      fetches.push(fetch(SERVER));
    }
    await Promise.all(fetches.slice(i, i + 32));
    i += 32;
  }

  while (i++ < COUNT) {
    fetches.push(fetch(SERVER));
  }

  await Promise.all(fetches);
  fetches.length = 0;
  fetches = [];
})();
await Bun.sleep(10);
Bun.gc(true);

if ((heapStats().objectTypeCounts.Response ?? 0) > 10) {
  throw new Error("Too many Response objects: " + heapStats().objectTypeCounts.Response);
}