This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
tracker.test.ts
152 lines (115 loc) · 4.35 KB
/
tracker.test.ts
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
import { assert } from "chai";
import delay from "delay";
import {
tracked,
waitForOutstandingPromises,
getOutstandingPromises
} from "../lib";
describe("promise-tracker", () => {
describe("tracked decorator", () => {
it("tracks outstanding promises for a tracked class", async () => {
const target = new TrackedObject();
const p = target.trackedMethod();
let promises = getOutstandingPromises({ target });
assert.lengthOf(promises, 1);
assert.strictEqual(promises[0], p);
await p;
});
it("cleans up terminated promises", async () => {
const target = new TrackedObject();
await target.trackedMethod();
let promises = getOutstandingPromises({ target });
assert.lengthOf(promises, 0);
});
it("doesn't track untracked async methods", async () => {
const target = new TrackedObject();
const p = target.untrackedMethod();
let promises = getOutstandingPromises({ target });
assert.lengthOf(promises, 0);
await p;
});
it("handles functions that return null", async () => {
const target = new TrackedObject();
target.returnsNullTrackedMethod();
let promises = getOutstandingPromises({ target });
assert.lengthOf(promises, 0);
assert.strictEqual(target.executedReturnsNullTrackedMethodCount, 1);
});
});
describe("waitForOutstandingPromises", () => {
it("waits on a single promise from a single instance", async () => {
const target = new TrackedObject();
target.trackedMethod();
assert.strictEqual(target.executedTrackedMethodCount, 0);
await waitForOutstandingPromises();
assert.strictEqual(target.executedTrackedMethodCount, 1);
});
it("waits on multiple promises from a single instance", async () => {
const target = new TrackedObject();
target.trackedMethod();
target.trackedMethod();
assert.strictEqual(target.executedTrackedMethodCount, 0);
await waitForOutstandingPromises();
assert.strictEqual(target.executedTrackedMethodCount, 2);
});
it("waits on promises for a specific instance", async () => {
const target1 = new TrackedObject();
const target2 = new TrackedObject();
target1.trackedMethod();
const p = target2.trackedMethod(50);
assert.strictEqual(target1.executedTrackedMethodCount, 0);
assert.strictEqual(target2.executedTrackedMethodCount, 0);
await waitForOutstandingPromises({ target: target1 });
assert.strictEqual(target1.executedTrackedMethodCount, 1);
assert.strictEqual(target2.executedTrackedMethodCount, 0);
await p;
assert.strictEqual(target2.executedTrackedMethodCount, 1);
});
it("waits for all promises, even when some throw", async () => {
const target = new TrackedObject();
// delay for longer to ensure this finishes after the one that throws
target.trackedMethod(50);
// throws after 25ms
target.throwingTrackedMethod();
assert.strictEqual(target.executedTrackedMethodCount, 0);
assert.strictEqual(target.executedThrowingTrackedMethodCount, 0);
await waitForOutstandingPromises();
assert.strictEqual(target.executedTrackedMethodCount, 1);
assert.strictEqual(target.executedThrowingTrackedMethodCount, 1);
});
it("allows caller to pass in custom catch handler logic", async () => {
const target = new TrackedObject();
let throwCount = 0;
// throws after 25ms
target.throwingTrackedMethod();
target.throwingTrackedMethod();
assert.strictEqual(throwCount, 0);
await waitForOutstandingPromises({ catchHandler: () => throwCount++ });
assert.strictEqual(throwCount, 2);
});
});
});
class TrackedObject {
executedTrackedMethodCount: number = 0;
executedThrowingTrackedMethodCount: number = 0;
executedReturnsNullTrackedMethodCount: number = 0;
@tracked
async trackedMethod(duration: number = 25): Promise<void> {
await delay(duration);
this.executedTrackedMethodCount++;
}
@tracked
async throwingTrackedMethod(): Promise<void> {
await delay(25);
this.executedThrowingTrackedMethodCount++;
throw new Error("expected");
}
@tracked
returnsNullTrackedMethod(): null {
this.executedReturnsNullTrackedMethodCount++;
return null;
}
async untrackedMethod(): Promise<void> {
await delay(25);
}
}