summaryrefslogtreecommitdiff
path: root/tests/unit_node/timers_test.ts
blob: c7dc30bbb7054afd4972b08013e8b377103f6c75 (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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

import { assert, fail } from "@std/assert";
import * as timers from "node:timers";
import * as timersPromises from "node:timers/promises";
import { assertEquals } from "@std/assert";

Deno.test("[node/timers setTimeout]", () => {
  {
    const { clearTimeout, setTimeout } = timers;
    const id = setTimeout(() => {});
    clearTimeout(id);
  }

  {
    const id = timers.setTimeout(() => {});
    timers.clearTimeout(id);
  }
});

Deno.test("[node/timers setInterval]", () => {
  {
    const { clearInterval, setInterval } = timers;
    const id = setInterval(() => {});
    clearInterval(id);
  }

  {
    const id = timers.setInterval(() => {});
    timers.clearInterval(id);
  }
});

Deno.test("[node/timers setImmediate]", async () => {
  {
    const { clearImmediate, setImmediate } = timers;
    const imm = setImmediate(() => {});
    clearImmediate(imm);
  }

  {
    const imm = timers.setImmediate(() => {});
    timers.clearImmediate(imm);
  }

  {
    const deffered = Promise.withResolvers<void>();
    const imm = timers.setImmediate(
      (a, b) => {
        assert(a === 1);
        assert(b === 2);
        deffered.resolve();
      },
      1,
      2,
    );
    await deffered;
    timers.clearImmediate(imm);
  }
});

Deno.test("[node/timers/promises setTimeout]", () => {
  const { setTimeout } = timersPromises;
  const p = setTimeout(0);

  assert(p instanceof Promise);
  return p;
});

Deno.test("[node/timers/promises scheduler.wait]", async () => {
  const { scheduler } = timersPromises;
  let resolved = false;
  timers.setTimeout(() => (resolved = true), 20);
  const p = scheduler.wait(20);

  assert(p instanceof Promise);
  await p;
  assert(resolved);
});

Deno.test("[node/timers/promises scheduler.yield]", async () => {
  const { scheduler } = timersPromises;
  let resolved = false;
  timers.setImmediate(() => resolved = true);

  const p = scheduler.yield();
  assert(p instanceof Promise);
  await p;

  assert(resolved);
});

// Regression test for https://github.com/denoland/deno/issues/17981
Deno.test("[node/timers refresh cancelled timer]", () => {
  const { setTimeout, clearTimeout } = timers;
  const p = setTimeout(() => {
    fail();
  }, 1);
  clearTimeout(p);
  p.refresh();
});

Deno.test("[node/timers setImmediate returns Immediate object]", () => {
  const { clearImmediate, setImmediate } = timers;

  const imm = setImmediate(() => {});
  imm.unref();
  imm.ref();
  imm.hasRef();
  clearImmediate(imm);
});

Deno.test({
  name: "setInterval yields correct values at expected intervals",
  async fn() {
    // Test configuration
    const CONFIG = {
      expectedValue: 42,
      intervalMs: 100,
      iterations: 3,
      tolerancePercent: 10,
    };

    const { setInterval } = timersPromises;
    const results: Array<{ value: number; timestamp: number }> = [];
    const startTime = Date.now();

    const iterator = setInterval(CONFIG.intervalMs, CONFIG.expectedValue);

    for await (const value of iterator) {
      results.push({
        value,
        timestamp: Date.now(),
      });
      if (results.length === CONFIG.iterations) {
        break;
      }
    }

    const values = results.map((r) => r.value);
    assertEquals(
      values,
      Array(CONFIG.iterations).fill(CONFIG.expectedValue),
      `Each iteration should yield ${CONFIG.expectedValue}`,
    );

    const intervals = results.slice(1).map((result, index) => ({
      interval: result.timestamp - results[index].timestamp,
      iterationNumber: index + 1,
    }));

    const toleranceMs = (CONFIG.tolerancePercent / 100) * CONFIG.intervalMs;
    const expectedRange = {
      min: CONFIG.intervalMs - toleranceMs,
      max: CONFIG.intervalMs + toleranceMs,
    };

    intervals.forEach(({ interval, iterationNumber }) => {
      const isWithinTolerance = interval >= expectedRange.min &&
        interval <= expectedRange.max;

      assertEquals(
        isWithinTolerance,
        true,
        `Iteration ${iterationNumber}: Interval ${interval}ms should be within ` +
          `${expectedRange.min}ms and ${expectedRange.max}ms ` +
          `(${CONFIG.tolerancePercent}% tolerance of ${CONFIG.intervalMs}ms)`,
      );
    });

    const totalDuration = results[results.length - 1].timestamp - startTime;
    const expectedDuration = CONFIG.intervalMs * CONFIG.iterations;
    const isDurationReasonable =
      totalDuration >= (expectedDuration - toleranceMs) &&
      totalDuration <= (expectedDuration + toleranceMs);

    assertEquals(
      isDurationReasonable,
      true,
      `Total duration ${totalDuration}ms should be close to ${expectedDuration}ms ` +
        `(within ${toleranceMs}ms tolerance)`,
    );

    const timestamps = results.map((r) => r.timestamp);
    const areTimestampsOrdered = timestamps.every((timestamp, i) =>
      i === 0 || timestamp > timestamps[i - 1]
    );

    assertEquals(
      areTimestampsOrdered,
      true,
      "Timestamps should be strictly increasing",
    );
  },
});

Deno.test({
  name: "setInterval with AbortSignal stops after expected duration",
  async fn() {
    const INTERVAL_MS = 500;
    const TOTAL_DURATION_MS = 3000;
    const TOLERANCE_MS = 500;

    const abortController = new AbortController();
    const { setInterval } = timersPromises;

    // Set up abort after specified duration
    const abortTimeout = timers.setTimeout(() => {
      abortController.abort();
    }, TOTAL_DURATION_MS);

    // Track iterations and timing
    const startTime = Date.now();
    const iterations: number[] = [];

    try {
      for await (
        const _timestamp of setInterval(INTERVAL_MS, undefined, {
          signal: abortController.signal,
        })
      ) {
        iterations.push(Date.now() - startTime);
      }
    } catch (error) {
      if (error instanceof Error && error.name !== "AbortError") {
        throw error;
      }
    } finally {
      timers.clearTimeout(abortTimeout);
    }

    // Validate timing
    const totalDuration = iterations[iterations.length - 1];
    const isWithinTolerance =
      totalDuration >= (TOTAL_DURATION_MS - TOLERANCE_MS) &&
      totalDuration <= (TOTAL_DURATION_MS + TOLERANCE_MS);

    assertEquals(
      isWithinTolerance,
      true,
      `Total duration ${totalDuration}ms should be within ±${TOLERANCE_MS}ms of ${TOTAL_DURATION_MS}ms`,
    );

    // Validate interval consistency
    const intervalDeltas = iterations.slice(1).map((time, i) =>
      time - iterations[i]
    );

    intervalDeltas.forEach((delta, i) => {
      const isIntervalValid = delta >= (INTERVAL_MS - 50) &&
        delta <= (INTERVAL_MS + 50);
      assertEquals(
        isIntervalValid,
        true,
        `Interval ${
          i + 1
        } duration (${delta}ms) should be within ±50ms of ${INTERVAL_MS}ms`,
      );
    });
  },
});