-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAsyncTests.js
More file actions
275 lines (257 loc) · 11.1 KB
/
AsyncTests.js
File metadata and controls
275 lines (257 loc) · 11.1 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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* @format
*/
import {
TestScenario,
assert,
} from './TestCommon'
export function makeAsyncTestScenarios(pThis) {
return [
new TestScenario('Test::PauseAsync', runAsyncActionTest.bind(pThis)),
new TestScenario('Test::CountToNumberAsync', runAsyncActionWithProgressTest.bind(pThis)),
new TestScenario('Test::AddAsync', runAsyncOperationTest.bind(pThis)),
new TestScenario('Test::CountDoubleAsync', runAsyncOperationWithProgressTest.bind(pThis)),
new TestScenario('Test::ThrowAsyncException', runAsyncActionWithException.bind(pThis)),
new TestScenario('Async await', runAsyncAwaitTest.bind(pThis)),
new TestScenario('Test::GetObjectsAsync', runGetObjectsTest.bind(pThis)),
];
}
function runAsyncActionTest(scenario) {
this.runAsync(scenario, (resolve, reject) => {
Promise.all([
TestComponent.Test.pauseAsync(100),
TestComponent.Test.pauseAsync(100)
.then(() => 42),
TestComponent.Test.pauseAsync(100)
.catch(err => { throw err; })
.then(() => {}),
TestComponent.Test.pauseAsync(100)
.then(() => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.pauseAsync(100)
.finally(() => { throw 42; })
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
TestComponent.Test.pauseAsync(100)
.then(() => { throw 42; }, () => { throw new Error('Reject handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
]).then(values => {
assert.equal(42, values[1]);
// .done() does not return a Promise, so save this for last
TestComponent.Test.pauseAsync(100).done(resolve, reject);
}).catch(reject);
});
}
function runAsyncActionWithProgressTest(scenario) {
this.runAsync(scenario, (resolve, reject) => {
Promise.all([
TestComponent.Test.countToNumberAsync(10),
TestComponent.Test.countToNumberAsync(10)
.then(() => 42),
TestComponent.Test.countToNumberAsync(10)
.catch(err => { throw err; })
.then(() => {}),
TestComponent.Test.countToNumberAsync(10)
.then(() => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.countToNumberAsync(10)
.finally(() => { throw 42; })
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
TestComponent.Test.countToNumberAsync(10)
.then(() => { throw 42; }, () => { throw new Error('Reject handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
]).then(values => {
assert.equal(42, values[1]);
var lastProgress = -1;
var pass = true;
TestComponent.Test.countToNumberAsync(10).done(
() => {
try {
assert.isTrue(pass);
assert.equal(10, lastProgress); // In theory could fail, but we should have had enough time
resolve();
} catch (err) {
reject(err);
}
}, reject, val => {
// NOTE: We don't get old progress values, so we might miss a couple at the beginning
if (lastProgress != -1) {
if (val != (lastProgress + 1)) pass = false;
}
lastProgress = val;
});
}).catch(reject);
});
}
function runAsyncOperationTest(scenario) {
this.runAsync(scenario, (resolve, reject) => {
Promise.all([
TestComponent.Test.addAsync(34, 8)
.then(result => {
assert.equal(42, result);
return 42;
}),
TestComponent.Test.addAsync(34, 8)
.catch(err => { throw err; })
.then(result => assert.equal(42, result)),
TestComponent.Test.addAsync(34, 8)
.then(() => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.addAsync(34, 8)
.finally(() => { throw 42; })
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
TestComponent.Test.addAsync(34, 8)
.then(() => { throw 42; }, () => { throw new Error('Reject handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
TestComponent.Test.immediateReturnAsync(42)
.then(val => assert.equal(42, val)),
]).then(values => {
assert.equal(42, values[0]);
TestComponent.Test.addAsync(34, 8).done(
result => {
try {
assert.equal(42, result);
resolve();
} catch (err) {
reject(err);
}
}, reject);
}).catch(reject);
});
}
function runAsyncOperationWithProgressTest(scenario) {
this.runAsync(scenario, (resolve, reject) => {
Promise.all([
TestComponent.Test.countDoubleAsync(5)
.then(result => {
assert.equal(10, result);
return 42;
}),
TestComponent.Test.countDoubleAsync(5)
.catch(err => { throw err; })
.then(result => assert.equal(10, result)),
TestComponent.Test.countDoubleAsync(5)
.then(() => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.countDoubleAsync(5)
.finally(() => { throw 42; })
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
TestComponent.Test.countDoubleAsync(5)
.then(() => { throw 42; }, () => { throw new Error('Reject handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
]).then(values => {
assert.equal(42, values[0]);
var lastProgress = -1;
var pass = true;
TestComponent.Test.countDoubleAsync(5).done(
result => {
try {
assert.equal(10, result);
assert.isTrue(pass);
assert.equal(10, lastProgress); // In theory could fail, but we should have had enough time
resolve();
} catch (err) {
reject(err);
}
}, reject, val => {
// NOTE: We don't get old progress values, so we might miss a couple at the beginning
if (lastProgress != -1) {
if (val != (lastProgress + 1)) pass = false;
}
lastProgress = val;
});
}).catch(reject);
});
}
function runAsyncActionWithException(scenario) {
this.runAsync(scenario, (resolve, reject) => {
Promise.all([
TestComponent.Test.throwAsyncException()
.then(() => { throw new Error('Resolve handler ran when it should not have'); }) // This will cause assert failures below
.catch(err => {
assert.equal(-2147024809, err.number);
assert.equal("test", err.message);
return 42;
}),
TestComponent.Test.throwAsyncException()
.catch (err => {
assert.equal(-2147024809, err.number);
assert.equal("test", err.message);
return true;
})
.then(wasCaught => assert.isTrue(wasCaught)),
TestComponent.Test.throwAsyncException()
.catch(() => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.throwAsyncException()
.then(() => { throw new Error('Resolve handler ran when it should not have'); }, () => { throw 42; })
.catch(val => assert.equal(42, val)),
TestComponent.Test.throwAsyncException()
.finally(() => 42) // Should run, but do nothing
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(err => {
assert.equal(-2147024809, err.number);
assert.equal("test", err.message);
}),
TestComponent.Test.throwAsyncException()
.finally(() => { throw 42; }) // Throwing, on the other hand, should overwrite the failure
.then(() => { throw new Error('Resolve handler ran when it should not have'); })
.catch(val => assert.equal(42, val)),
]).then(values => {
assert.equal(42, values[0]);
TestComponent.Test.throwAsyncException().done(
() => reject(new Error('Expected an exception')),
err => {
try {
assert.equal(-2147024809, err.number);
assert.equal("test", err.message);
resolve();
} catch (e) {
reject(e);
}
});
}).catch(reject);
});
}
function runAsyncAwaitTest(scenario) {
this.runAsync(scenario, async (resolve, reject) => {
try
{
await TestComponent.Test.pauseAsync(100);
await TestComponent.Test.countToNumberAsync(10);
var result = await TestComponent.Test.addAsync(34, 8);
assert.equal(42, result);
result = await TestComponent.Test.countDoubleAsync(5);
assert.equal(10, result);
assert.equal(42, await TestComponent.Test.immediateReturnAsync(42));
try
{
await TestComponent.Test.throwAsyncException();
throw new Error('Expected an exception');
} catch (err) {
assert.equal(-2147024809, err.number);
assert.equal("test", err.message);
}
resolve();
} catch (err) {
reject(err);
}
});
}
function runGetObjectsTest(scenario) {
this.runAsync(scenario, async (resolve, reject) => {
try
{
var result = await TestComponent.Test.getObjectsAsync();
assert.equal(42, result[0].magicValue);
resolve();
} catch (err) {
reject(err);
}
});
}