forked from ni/VireoSDK
/
Timeout.Test.js
228 lines (182 loc) · 8.92 KB
/
Timeout.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
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
describe('Timeout test suite #Slow', function () {
'use strict';
// Reference aliases
var vireoHelpers = window.vireoHelpers;
var vireoRunner = window.testHelpers.vireoRunner;
var fixtures = window.testHelpers.fixtures;
var httpBinHelpers = window.testHelpers.httpBinHelpers;
var httpParser = window.testHelpers.httpParser;
var TIMEOUT_CODE = 56;
var ABORT_CODE = 363508;
var vireo;
var httpGetMethodViaUrl = fixtures.convertToAbsoluteFromFixturesDir('http/GetMethod.via');
beforeAll(function (done) {
fixtures.preloadAbsoluteUrls([
httpGetMethodViaUrl
], done);
});
beforeAll(function (done) {
httpBinHelpers.queryHttpBinStatus(done);
});
beforeAll(async function () {
vireo = await vireoHelpers.createInstance();
});
afterAll(function () {
vireo = undefined;
});
beforeEach(function () {
httpBinHelpers.makeTestPendingIfHttpBinOffline();
});
beforeEach(function (done) {
// The timeout tests sometimes need a recovery period so perform a full get request and wait for it before continuing
httpBinHelpers.forceHttpBinQuery('get?show_env=1', done);
});
it('GET method with timeout 0s times out with httpbin delay of 10s', function (done) {
var timeout = 0;
var timeoutEpsilon = 1000;
var runSlicesAsync = vireoRunner.rebootAndLoadVia(vireo, httpGetMethodViaUrl);
var viPathParser = vireoRunner.createVIPathParser(vireo, 'MyVI');
var viPathWriter = vireoRunner.createVIPathWriter(vireo, 'MyVI');
var url = httpBinHelpers.convertToAbsoluteUrl('delay/10');
viPathWriter('url', url);
viPathWriter('timeout', timeout);
var startTime = performance.now();
runSlicesAsync(function (rawPrint, rawPrintError) {
var endTime = performance.now();
var runTime = endTime - startTime;
expect(runTime).toBeNear(timeout, timeoutEpsilon);
expect(rawPrint).toBeEmptyString();
expect(rawPrintError).toBeEmptyString();
expect(viPathParser('handle')).toBe(0);
expect(viPathParser('headers')).toBeEmptyString();
expect(viPathParser('body')).toBeEmptyString();
expect(viPathParser('statusCode')).toBe(0);
expect(viPathParser('error.status')).toBeTrue();
expect(viPathParser('error.code')).toBe(TIMEOUT_CODE);
expect(viPathParser('error.source')).toMatch(/HttpClientGet in MyVI/);
expect(viPathParser('error.source')).toMatch(/time limit/);
done();
});
});
it('GET method with timeout 1s times out with httpbin delay of 10s', function (done) {
var timeout = 1000;
var runSlicesAsync = vireoRunner.rebootAndLoadVia(vireo, httpGetMethodViaUrl);
var viPathParser = vireoRunner.createVIPathParser(vireo, 'MyVI');
var viPathWriter = vireoRunner.createVIPathWriter(vireo, 'MyVI');
var url = httpBinHelpers.convertToAbsoluteUrl('delay/10');
viPathWriter('url', url);
viPathWriter('timeout', timeout);
var startTime = performance.now();
runSlicesAsync(function (rawPrint, rawPrintError) {
var endTime = performance.now();
var runTime = endTime - startTime;
expect(runTime).toBeNear(timeout, timeout * 0.5);
expect(rawPrint).toBeEmptyString();
expect(rawPrintError).toBeEmptyString();
expect(viPathParser('handle')).toBe(0);
expect(viPathParser('headers')).toBeEmptyString();
expect(viPathParser('body')).toBeEmptyString();
expect(viPathParser('statusCode')).toBe(0);
expect(viPathParser('error.status')).toBeTrue();
expect(viPathParser('error.code')).toBe(TIMEOUT_CODE);
expect(viPathParser('error.source')).toMatch(/HttpClientGet in MyVI/);
expect(viPathParser('error.source')).toMatch(/time limit/);
done();
});
});
it('GET method with timeout of 5 seconds times out with httpbin delay of 10s', function (done) {
var timeout = 5000;
var runSlicesAsync = vireoRunner.rebootAndLoadVia(vireo, httpGetMethodViaUrl);
var viPathParser = vireoRunner.createVIPathParser(vireo, 'MyVI');
var viPathWriter = vireoRunner.createVIPathWriter(vireo, 'MyVI');
var url = httpBinHelpers.convertToAbsoluteUrl('delay/10');
viPathWriter('url', url);
viPathWriter('timeout', timeout);
var startTime = performance.now();
runSlicesAsync(function (rawPrint, rawPrintError) {
var endTime = performance.now();
var runTime = endTime - startTime;
expect(runTime).toBeNear(timeout, timeout * 0.5);
expect(rawPrint).toBeEmptyString();
expect(rawPrintError).toBeEmptyString();
expect(viPathParser('handle')).toBe(0);
expect(viPathParser('headers')).toBeEmptyString();
expect(viPathParser('body')).toBeEmptyString();
expect(viPathParser('statusCode')).toBe(0);
expect(viPathParser('error.status')).toBeTrue();
expect(viPathParser('error.code')).toBe(TIMEOUT_CODE);
expect(viPathParser('error.source')).toMatch(/HttpClientGet in MyVI/);
expect(viPathParser('error.source')).toMatch(/time limit/);
done();
});
});
it('GET method with timeout of 5 seconds times and httpbin delay of 10s is aborted after 2s', function (done) {
var timeout = 5000;
var abortTimeout = 2000;
var runSlicesAsync = vireoRunner.rebootAndLoadVia(vireo, httpGetMethodViaUrl);
var viPathParser = vireoRunner.createVIPathParser(vireo, 'MyVI');
var viPathWriter = vireoRunner.createVIPathWriter(vireo, 'MyVI');
var url = httpBinHelpers.convertToAbsoluteUrl('delay/10');
viPathWriter('url', url);
viPathWriter('timeout', timeout);
var startTime = performance.now();
setTimeout(function () {
vireo.httpClient.abortAllRunningRequests();
}, abortTimeout);
runSlicesAsync(function (rawPrint, rawPrintError) {
var endTime = performance.now();
var runTime = endTime - startTime;
expect(runTime).toBeNear(abortTimeout, abortTimeout * 0.5);
expect(rawPrint).toBeEmptyString();
expect(rawPrintError).toBeEmptyString();
expect(viPathParser('handle')).toBe(0);
expect(viPathParser('headers')).toBeEmptyString();
expect(viPathParser('body')).toBeEmptyString();
expect(viPathParser('statusCode')).toBe(0);
expect(viPathParser('error.status')).toBeTrue();
expect(viPathParser('error.code')).toBe(ABORT_CODE);
expect(viPathParser('error.source')).toMatch(/HttpClientGet in MyVI/);
expect(viPathParser('error.source')).toMatch(/aborted/);
done();
});
});
it('GET method with timeout -1 succeeds with httpbin delay of 10s', function (done) {
var timeout = -1;
var httpBinDelay = 10000;
var runSlicesAsync = vireoRunner.rebootAndLoadVia(vireo, httpGetMethodViaUrl);
var viPathParser = vireoRunner.createVIPathParser(vireo, 'MyVI');
var viPathWriter = vireoRunner.createVIPathWriter(vireo, 'MyVI');
var url = httpBinHelpers.convertToAbsoluteUrl('delay/10');
viPathWriter('url', url);
viPathWriter('timeout', timeout);
var startTime = performance.now();
runSlicesAsync(function (rawPrint, rawPrintError) {
var endTime = performance.now();
var runTime = endTime - startTime;
expect(runTime).toBeNear(httpBinDelay, httpBinDelay * 2);
expect(rawPrint).toBeEmptyString();
expect(rawPrintError).toBeEmptyString();
// handle
expect(viPathParser('handle')).toBe(0);
// header
var responseHeader = httpParser.parseResponseHeader(viPathParser('headers'));
expect(responseHeader.httpVersion).toBe('HTTP/1.1');
expect(responseHeader.statusCode).toBe(200);
expect(responseHeader.reasonPhrase).toBe('OK');
expect(responseHeader.headers).toBeNonEmptyObject();
// body
var httpBinBody = httpBinHelpers.parseBody(viPathParser('body'));
var requestUrl = httpParser.parseUrl(httpBinBody.url);
expect(httpBinBody.args).toBeEmptyObject();
expect(httpBinBody.headers).toBeNonEmptyObject();
expect(requestUrl.pathname).toBe('/delay/10');
// status code
expect(viPathParser('statusCode')).toBe(200);
// error
expect(viPathParser('error.status')).toBeFalse();
expect(viPathParser('error.code')).toBe(0);
expect(viPathParser('error.source')).toBeEmptyString();
done();
});
});
});