-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathpoll-action.function.spec.ts
142 lines (121 loc) · 4.17 KB
/
poll-action.function.spec.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
import {timeout} from "./poll-action.function";
describe("poll-action", () => {
it("should timeout after maxDuration if action rejects", async () => {
// GIVEN
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(() => {
return Promise.reject(false);
});
// WHEN
const start = Date.now();
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toBe(`Action timed out after ${maxDuration} ms`);
}
const end = Date.now();
// THEN
expect((end - start)).toBeGreaterThanOrEqual(maxDuration);
});
it("should timeout after maxDuration if action resolve != true", async () => {
// GIVEN
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(async () => {
return false;
});
// WHEN
const start = Date.now();
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
}
const end = Date.now();
// THEN
expect((end - start)).toBeGreaterThanOrEqual(maxDuration);
});
it("should resolve after updateInterval if action resolves", async () => {
// GIVEN
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(() => {
return Promise.resolve(true);
});
// WHEN
const start = Date.now();
await timeout(updateInterval, maxDuration, action);
const end = Date.now();
// THEN
expect((end - start)).toBeLessThan(updateInterval);
expect(action).toBeCalledTimes(1);
});
it("should resolve after updateInterval if action resolves != true", async () => {
// GIVEN
const updateInterval = 200;
const maxDuration = 1000;
const action = jest.fn(async () => {
return true;
});
// WHEN
const start = Date.now();
await timeout(updateInterval, maxDuration, action);
const end = Date.now();
// THEN
expect((end - start)).toBeLessThan(updateInterval);
expect(action).toBeCalledTimes(1);
});
it("should retry until action succeeds", async () => {
// GIVEN
const updateInterval = 200;
const maxDuration = 1000;
const delay = 2.5 * updateInterval;
const start = Date.now();
const action = jest.fn(() => {
const interval = (Date.now() - start);
return new Promise<boolean>((resolve, reject) => (interval > delay) ? resolve(true) : reject());
});
// WHEN
const result = await timeout(updateInterval, maxDuration, action);
const end = Date.now();
// THEN
expect((end - start)).toBeGreaterThanOrEqual(delay);
expect(result).toBeTruthy();
});
it("should fail after timeout if timeout < retry interval", async () => {
// GIVEN
const updateInterval = 1000;
const maxDuration = 200;
const action = jest.fn(() => Promise.resolve(false));
// WHEN
const start = Date.now();
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
}
const end = Date.now();
// THEN
expect(action).toBeCalledTimes(1);
expect((end - start)).toBeLessThan(updateInterval);
});
it("should fail if action does not resolve within timeout", async () => {
// GIVEN
const updateInterval = 100;
const maxDuration = 200;
const action = jest.fn(() => {
return new Promise((_, reject) => {
setTimeout(() => reject(), 300);
})
});
// WHEN
try {
await timeout(updateInterval, maxDuration, action);
} catch (e) {
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
}
// THEN
expect(action).toBeCalledTimes(1);
});
});