aboutsummaryrefslogtreecommitdiff
path: root/test/js/web/abort/abort.ts
blob: fb9e60627e2a23d1800e15983e5ac572ca5b0bef (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { describe, test, expect } from "bun:test";
import { heapStats } from "bun:jsc";
import { gc } from "bun";

async function expectMaxObjectTypeCount(
  expect: typeof import("bun:test").expect,
  type: string,
  count: number,
  maxWait = 1000,
) {
  gc(true);
  if (heapStats().objectTypeCounts[type] <= count) return;
  gc(true);
  for (const wait = 20; maxWait > 0; maxWait -= wait) {
    if (heapStats().objectTypeCounts[type] <= count) break;
    await new Promise(resolve => setTimeout(resolve, wait));
    gc(true);
  }
  expect(heapStats().objectTypeCounts[type]).toBeLessThanOrEqual(count);
}

describe("AbortSignal", () => {
  test("constructor", () => {
    expect(() => new AbortSignal()).toThrow(TypeError);
  });
  describe("abort()", () => {
    const reasons = [
      {
        label: "undefined",
        reason: undefined,
      },
      {
        label: "null",
        reason: null,
      },
      {
        label: "string",
        reason: "Aborted!",
      },
      {
        label: "Error",
        reason: new Error("Aborted!"),
      },
      {
        label: "object",
        reason: {
          ok: false,
          error: "Aborted!",
        },
      },
    ];
    for (const { label, reason } of reasons) {
      test(label, () => {
        const signal = AbortSignal.abort(reason);
        expect(signal instanceof AbortSignal).toBe(true);
        expect(signal).toHaveProperty("aborted", true);
        if (reason === undefined) {
          expect(signal).toHaveProperty("reason");
          expect(signal.reason instanceof DOMException).toBe(true);
        } else {
          expect(signal).toHaveProperty("reason", reason);
        }
      });
    }
  });
  describe("timeout()", () => {
    const valid = [
      {
        label: "0",
        timeout: 0,
      },
      {
        label: "1",
        timeout: 1,
      },
      {
        label: "Number.MAX_SAFE_INTEGER",
        timeout: Number.MAX_SAFE_INTEGER,
      },
    ];
    for (const { label, timeout } of valid) {
      test(label, () => {
        const signal = AbortSignal.timeout(timeout);
        expect(signal instanceof AbortSignal).toBe(true);
        expect(signal instanceof EventTarget).toBe(true);
        expect(signal).toHaveProperty("aborted", false);
        expect(signal).toHaveProperty("reason", undefined);
      });
    }
    const invalid = [
      {
        label: "-1",
        timeout: -1,
      },
      {
        label: "NaN",
        timeout: NaN,
      },
      {
        label: "Infinity",
        timeout: Infinity,
      },
      {
        label: "Number.MAX_VALUE",
        timeout: Number.MAX_VALUE,
      },
    ];
    for (const { label, timeout } of invalid) {
      test(label, () => {
        expect(() => AbortSignal.timeout(timeout)).toThrow(TypeError);
      });
    }
    // FIXME: test runner hangs when this is enabled
    test.skip("timeout works", done => {
      const abort = AbortSignal.timeout(1);
      abort.addEventListener("abort", event => {
        done();
      });
      // AbortSignal.timeout doesn't keep the event loop / process alive
      // so we set a no-op timeout
      setTimeout(() => {}, 10);
    });
  });
  describe("prototype", () => {
    test("aborted", () => {
      expect(AbortSignal.abort()).toHaveProperty("aborted", true);
      expect(AbortSignal.timeout(0)).toHaveProperty("aborted", false);
    });
    test("reason", () => {
      expect(AbortSignal.abort()).toHaveProperty("reason");
      expect(AbortSignal.timeout(0)).toHaveProperty("reason");
    });
    test("onabort", done => {
      const signal = AbortSignal.timeout(0);
      expect(signal.onabort).toBeNull();
      const onabort = (event: Event) => {
        expect(event instanceof Event).toBe(true);
        done();
      };
      expect(() => (signal.onabort = onabort)).not.toThrow();
      expect(signal.onabort).toStrictEqual(onabort);
      setTimeout(() => {}, 1);
    });
  });
});

describe("AbortController", () => {
  test("contructor", () => {
    expect(() => new AbortController()).not.toThrow();
  });
  describe("prototype", () => {
    test("signal", () => {
      const controller = new AbortController();
      expect(controller).toHaveProperty("signal");
      expect(controller.signal instanceof AbortSignal).toBe(true);
    });
    describe("abort()", () => {
      test("signal and controller are garbage collected", async () => {
        (function () {
          var last;
          class MyAbortSignalReasonGCTest {}
          for (let i = 0; i < 1e3; i++) {
            const controller = new AbortController();
            var escape;
            controller.signal.onabort = reason => {
              escape = reason;
            };
            controller.abort(new MyAbortSignalReasonGCTest());
            last = escape;
            new MyAbortSignalReasonGCTest();
          }

          return last;
        })();
        await expectMaxObjectTypeCount(expect, "AbortController", 3);
        await expectMaxObjectTypeCount(expect, "AbortSignal", 3);
      });
      const reasons = [
        {
          label: "undefined",
          reason: undefined,
        },
        {
          label: "string",
          reason: "The operation was aborted.",
        },
        {
          label: "Error",
          reason: new DOMException("The operation was aborted."),
        },
      ];
      for (const { label, reason } of reasons) {
        test(label, () => {
          const controller = new AbortController();
          let event: Event | undefined;
          expect(() => {
            controller.signal.onabort = data => {
              event = data;
            };
          }).not.toThrow();
          expect(controller).toHaveProperty("abort");
          expect(() => controller.abort()).not.toThrow();
          expect(event instanceof Event).toBe(true);
          expect(controller.signal.aborted).toBe(true);
          if (reason === undefined) {
            expect(controller.signal.reason instanceof DOMException).toBe(true);
          } else if (reason instanceof DOMException) {
            expect(controller.signal.reason).toBeInstanceOf(reason.constructor);
          } else {
            expect(controller.signal.reason.message).toStrictEqual(reason);
          }
        });
      }
    });
  });
});