-
Notifications
You must be signed in to change notification settings - Fork 225
/
callback-coordination.test.js
119 lines (101 loc) · 3.01 KB
/
callback-coordination.test.js
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
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
const tape = require('tape');
const logging = require('../../lib/logging');
const logger = logging.createLogger('off');
const {
CallbackCoordination,
} = require('../../lib/cloud-metadata/callback-coordination');
tape.test('fetch coordination: all successful', function (t) {
const fetcher = new CallbackCoordination(-1, logger);
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(null, 'pass1');
}, 10);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(null, 'pass2');
}, 20);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(null, 'pass3');
}, 30);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 200);
});
fetcher.on('result', function (result) {
t.equals('pass1', result, "first callback's value is the result");
t.end();
});
fetcher.on('error', function () {
t.fail('no errors expected');
});
fetcher.start();
});
tape.test('fetch coordination: all errors', function (t) {
const fetcher = new CallbackCoordination(-1, logger);
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 10);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 20);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 30);
});
fetcher.on('result', function (result) {
t.fail('no results');
});
fetcher.on('error', function (error) {
t.ok(error);
t.equals(error.allErrors.length, 3, 'all three errors captured');
t.end();
});
fetcher.start();
});
tape.test(
'fetch coordination: fails to invoke callback (timeout)',
function (t) {
const fetcher = new CallbackCoordination(100, logger);
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 10);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
fetcher.recordResult(new Error('an error'));
}, 20);
});
fetcher.schedule(function (fetcher) {
setTimeout(function () {
// Deliberately empty — simulates forgetting to call fetcher.recordResult
}, 30);
});
fetcher.on('result', function (result) {
t.error('do no expect to reach');
});
fetcher.on('error', function (error) {
t.ok(error);
t.equals(error.allErrors.length, 2, 'two of three errors captured');
t.equals(error.message, 'callback coordination reached timeout');
t.end();
});
fetcher.start();
},
);