-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathclock-epoch.int.test.ts
More file actions
89 lines (76 loc) · 2.81 KB
/
clock-epoch.int.test.ts
File metadata and controls
89 lines (76 loc) · 2.81 KB
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
import process from 'node:process';
import { threadId } from 'node:worker_threads';
import { describe, expect, it } from 'vitest';
import { defaultClock, epochClock } from './clock-epoch.js';
describe('epochClock', () => {
it('should create epoch clock with defaults', () => {
const c = epochClock();
expect(c).toStrictEqual(
expect.objectContaining({
tid: threadId,
pid: process.pid,
timeOriginMs: performance.timeOrigin,
}),
);
expect(c.fromEpochMs).toBeFunction();
expect(c.fromEpochUs).toBeFunction();
expect(c.fromPerfMs).toBeFunction();
expect(c.fromEntryStartTimeMs).toBeFunction();
expect(c.fromDateNowMs).toBeFunction();
});
it('should convert epoch milliseconds to microseconds correctly', () => {
const c = epochClock();
const epochMs = Date.now();
const result = c.fromEpochMs(epochMs);
expect(result).toBeInteger();
expect(result).toBe(Math.round(epochMs * 1000));
});
it('should convert epoch microseconds to microseconds correctly', () => {
const c = epochClock();
const epochUs = Date.now() * 1000;
const expectedUs = Math.round(epochUs);
const result = c.fromEpochUs(epochUs);
expect(result).toBe(expectedUs);
expect(result).toBe(Math.round(result));
});
it('should convert performance milliseconds to epoch microseconds correctly', () => {
const c = epochClock();
const perfMs = performance.now();
const expectedUs = Math.round((c.timeOriginMs + perfMs) * 1000);
const result = c.fromPerfMs(perfMs);
expect(result).toBe(expectedUs);
expect(result).toBeInteger();
});
it('should convert entry start time milliseconds to epoch microseconds correctly', () => {
const c = epochClock();
const entryStartMs = performance.mark('fromPerfMs').startTime;
const expectedUs = Math.round((c.timeOriginMs + entryStartMs) * 1000);
const result = c.fromEntryStartTimeMs(entryStartMs);
expect(result).toBe(expectedUs);
expect(result).toBe(Math.round(result));
});
it('should convert Date.now milliseconds to epoch microseconds correctly', () => {
const c = epochClock();
const dateNowMs = Date.now();
const result = c.fromDateNowMs(dateNowMs);
expect(result).toBe(Math.round(dateNowMs * 1000));
expect(result).toBe(Math.round(result));
});
});
describe('defaultClock', () => {
it('should have valid defaultClock export', () => {
const c = defaultClock;
expect(c).toStrictEqual(
expect.objectContaining({
tid: threadId,
pid: process.pid,
timeOriginMs: performance.timeOrigin,
}),
);
expect(c.fromEpochMs).toBeFunction();
expect(c.fromEpochUs).toBeFunction();
expect(c.fromPerfMs).toBeFunction();
expect(c.fromEntryStartTimeMs).toBeFunction();
expect(c.fromDateNowMs).toBeFunction();
});
});