UNPKG

12.4 kBJavaScriptView Raw
1'use strict';
2
3var waitOn = require('../');
4var fs = require('fs');
5var http = require('http');
6var path = require('path');
7var temp = require('temp');
8
9var mocha = require('mocha');
10var describe = mocha.describe;
11var it = mocha.it;
12var afterEach = mocha.afterEach;
13var expect = require('expect');
14
15temp.track(); // cleanup files on exit
16
17describe('api', function () {
18 this.timeout(3000);
19 var httpServer = null;
20
21 afterEach(function (done) {
22 if (httpServer) {
23 httpServer.close();
24 httpServer = null;
25 }
26 done();
27 });
28
29 it('should succeed when file resources are available', function (done) {
30 temp.mkdir({}, function (err, dirPath) {
31 var opts = {
32 resources: [
33 path.resolve(dirPath, 'foo'),
34 path.resolve(dirPath, 'bar')
35 ]
36 };
37 fs.writeFileSync(opts.resources[0], 'data1');
38 fs.writeFileSync(opts.resources[1], 'data2');
39 waitOn(opts, function (err) {
40 expect(err).toNotExist();
41 done();
42 });
43 });
44 });
45
46 it('should succeed when file resources are become available later', function (done) {
47 temp.mkdir({}, function (err, dirPath) {
48 var opts = {
49 resources: [
50 path.resolve(dirPath, 'foo'),
51 path.resolve(dirPath, 'bar')
52 ]
53 };
54
55 setTimeout(function () {
56 fs.writeFile(opts.resources[0], 'data1', function () {});
57 fs.writeFile(opts.resources[1], 'data2', function () {});
58 }, 300);
59
60 waitOn(opts, function (err) {
61 expect(err).toNotExist();
62 done();
63 });
64 });
65 });
66
67 it('should succeed when http resources are become available later', function (done) {
68 var opts = {
69 resources: [
70 'http://localhost:3000',
71 'http://localhost:3000/foo'
72 ]
73 };
74
75 setTimeout(function () {
76 httpServer = http.createServer()
77 .on('request', function (req, res) {
78 res.end('data');
79 });
80 httpServer.listen(3000, 'localhost');
81 }, 300);
82
83 waitOn(opts, function (err) {
84 expect(err).toNotExist();
85 done();
86 });
87 });
88
89 it('should succeed when http GET resources become available later', function (done) {
90 var opts = {
91 resources: [
92 'http-get://localhost:3011',
93 'http-get://localhost:3011/foo'
94 ]
95 };
96
97 setTimeout(function () {
98 httpServer = http.createServer()
99 .on('request', function (req, res) {
100 res.end('data');
101 });
102 httpServer.listen(3011, 'localhost');
103 }, 300);
104
105 waitOn(opts, function (err) {
106 expect(err).toNotExist();
107 done();
108 });
109 });
110
111 /*
112 it('should succeed when an https resource is available', function (done) {
113 var opts = {
114 resources: [
115 'https://www.google.com'
116 ]
117 };
118
119 waitOn(opts, function (err) {
120 expect(err).toNotExist();
121 done();
122 });
123 });
124
125 it('should succeed when an https GET resource is available', function (done) {
126 var opts = {
127 resources: [
128 'https-get://www.google.com'
129 ]
130 };
131
132 waitOn(opts, function (err) {
133 expect(err).toNotExist();
134 done();
135 });
136 });
137 */
138
139
140 it('should succeed when a service is listening to tcp port', function (done) {
141 var opts = {
142 resources: [
143 'tcp:localhost:3001',
144 'tcp:3001'
145 ]
146 };
147
148 setTimeout(function () {
149 httpServer = http.createServer()
150 .on('request', function (req, res) {
151 res.end('data');
152 });
153 httpServer.listen(3001, 'localhost');
154 }, 300);
155
156 waitOn(opts, function (err) {
157 expect(err).toNotExist();
158 done();
159 });
160 });
161
162 it('should succeed when a service is listening to a socket', function (done) {
163 var socketPath;
164 temp.mkdir({}, function (err, dirPath) {
165 socketPath = path.resolve(dirPath, 'sock');
166 var opts = {
167 resources: [
168 'socket:'+socketPath
169 ]
170 };
171
172 setTimeout(function () {
173 httpServer = http.createServer();
174 httpServer.listen(socketPath);
175 }, 300);
176
177 waitOn(opts, function (err) {
178 expect(err).toNotExist();
179 done();
180 });
181 });
182 });
183
184 it('should succeed when a http service is listening to a socket', function (done) {
185 var socketPath;
186 temp.mkdir({}, function (err, dirPath) {
187 socketPath = path.resolve(dirPath, 'sock');
188 var opts = {
189 resources: [
190 'http://unix:' + socketPath + ':/',
191 'http://unix:' + socketPath + ':/foo'
192 ]
193 };
194
195 setTimeout(function () {
196 httpServer = http.createServer()
197 .on('request', function (req, res) {
198 res.end('data');
199 });
200 httpServer.listen(socketPath);
201 }, 300);
202
203 waitOn(opts, function (err) {
204 expect(err).toNotExist();
205 done();
206 });
207 });
208 });
209
210 it('should succeed when a http GET service is listening to a socket', function (done) {
211 var socketPath;
212 temp.mkdir({}, function (err, dirPath) {
213 socketPath = path.resolve(dirPath, 'sock');
214 var opts = {
215 resources: [
216 'http-get://unix:' + socketPath + ':/',
217 'http-get://unix:' + socketPath + ':/foo'
218 ]
219 };
220
221 setTimeout(function () {
222 httpServer = http.createServer()
223 .on('request', function (req, res) {
224 res.end('data');
225 });
226 httpServer.listen(socketPath);
227 }, 300);
228
229 waitOn(opts, function (err) {
230 expect(err).toNotExist();
231 done();
232 });
233 });
234 });
235
236 // Error situations
237
238 it('should timeout when all resources are not available and timout option is specified', function (done) {
239 temp.mkdir({}, function (err, dirPath) {
240 var opts = {
241 resources: [ path.resolve(dirPath, 'foo') ],
242 timeout: 1000
243 };
244 waitOn(opts, function (err) {
245 expect(err).toExist();
246 done();
247 });
248 });
249 });
250
251 it('should timeout when some resources are not available and timout option is specified', function (done) {
252 temp.mkdir({}, function (err, dirPath) {
253 var opts = {
254 resources: [
255 path.resolve(dirPath, 'foo'),
256 path.resolve(dirPath, 'bar')
257 ],
258 timeout: 1000
259 };
260 fs.writeFile(opts.resources[0], 'data', function () {});
261 waitOn(opts, function (err) {
262 expect(err).toExist();
263 done();
264 });
265 });
266 });
267
268 it('should timeout when an http resource returns 404', function (done) {
269 var opts = {
270 resources: [
271 'http://localhost:3002'
272 ],
273 timeout: 1000,
274 interval: 100,
275 window: 100
276 };
277
278 setTimeout(function () {
279 httpServer = http.createServer()
280 .on('request', function (req, res) {
281 res.statusCode = 404;
282 res.end('data');
283 });
284 httpServer.listen(3002, 'localhost');
285 }, 300);
286
287 waitOn(opts, function (err) {
288 expect(err).toExist();
289 done();
290 });
291 });
292
293 it('should timeout when an http resource is not available', function (done) {
294 var opts = {
295 resources: [
296 'http://localhost:3010'
297 ],
298 timeout: 1000,
299 interval: 100,
300 window: 100
301 };
302
303 waitOn(opts, function (err) {
304 expect(err).toExist();
305 done();
306 });
307 });
308
309 it('should timeout when an http GET resource is not available', function (done) {
310 var opts = {
311 resources: [
312 'http-get://localhost:3010'
313 ],
314 timeout: 1000,
315 interval: 100,
316 window: 100
317 };
318
319 waitOn(opts, function (err) {
320 expect(err).toExist();
321 done();
322 });
323 });
324
325 it('should timeout when an https resource is not available', function (done) {
326 var opts = {
327 resources: [
328 'https://localhost:3010/foo/bar'
329 ],
330 timeout: 1000,
331 interval: 100,
332 window: 100
333 };
334
335 waitOn(opts, function (err) {
336 expect(err).toExist();
337 done();
338 });
339 });
340
341 it('should timeout when an https GET resource is not available', function (done) {
342 var opts = {
343 resources: [
344 'https-get://localhost:3010/foo/bar'
345 ],
346 timeout: 1000,
347 interval: 100,
348 window: 100
349 };
350
351 waitOn(opts, function (err) {
352 expect(err).toExist();
353 done();
354 });
355 });
356
357 it('should timeout when a service is not listening to tcp port', function (done) {
358 var opts = {
359 resources: [
360 'tcp:localhost:3010'
361 ],
362 timeout: 1000
363 };
364
365 waitOn(opts, function (err) {
366 expect(err).toExist();
367 done();
368 });
369 });
370
371 it('should timeout when a service is not listening to a socket', function (done) {
372 var socketPath;
373 temp.mkdir({}, function (err, dirPath) {
374 socketPath = path.resolve(dirPath, 'sock');
375 var opts = {
376 resources: [
377 'socket:'+socketPath
378 ],
379 timeout: 1000,
380 interval: 100,
381 window: 100
382 };
383
384 waitOn(opts, function (err) {
385 expect(err).toExist();
386 done();
387 });
388 });
389 });
390
391 it('should timeout when an http service listening to a socket returns 404', function (done) {
392 var socketPath;
393 temp.mkdir({}, function (err, dirPath) {
394 socketPath = path.resolve(dirPath, 'sock');
395 var opts = {
396 resources: [
397 'http://unix:' + socketPath + ':/',
398 'http://unix:' + socketPath + ':/foo'
399 ],
400 timeout: 1000,
401 interval: 100,
402 window: 100
403 };
404
405 setTimeout(function () {
406 httpServer = http.createServer()
407 .on('request', function (req, res) {
408 res.statusCode = 404;
409 res.end('data');
410 });
411 httpServer.listen(socketPath);
412 }, 300);
413
414 waitOn(opts, function (err) {
415 expect(err).toExist();
416 done();
417 });
418 });
419 });
420
421 it('should timeout when an http service listening to a socket is too slow', function (done) {
422 var socketPath;
423 temp.mkdir({}, function (err, dirPath) {
424 socketPath = path.resolve(dirPath, 'sock');
425 var opts = {
426 resources: [
427 'package.json',
428 'http://unix:' + socketPath + ':/',
429 'http://unix:' + socketPath + ':/foo'
430 ],
431 timeout: 1000,
432 interval: 100,
433 window: 100
434 };
435
436 httpServer = http.createServer()
437 .on('request', function (req, res) {
438 setTimeout(function () {
439 // res.statusCode = 404;
440 res.end('data');
441 }, 1100);
442 });
443 httpServer.listen(socketPath);
444
445 waitOn(opts, function (err) {
446 expect(err).toExist();
447 done();
448 });
449 });
450 });
451
452 it('should succeed when file resources are not available in reverse mode', function (done) {
453 temp.mkdir({}, function (err, dirPath) {
454 var opts = {
455 resources: [
456 path.resolve(dirPath, 'foo'),
457 path.resolve(dirPath, 'bar')
458 ],
459 reverse: true
460 };
461 waitOn(opts, function (err) {
462 expect(err).toNotExist();
463 done();
464 });
465 });
466 });
467
468 it('should succeed when file resources are not available later in reverse mode', function (done) {
469 temp.mkdir({}, function (err, dirPath) {
470 var opts = {
471 resources: [
472 path.resolve(dirPath, 'foo'),
473 path.resolve(dirPath, 'bar')
474 ],
475 reverse: true
476 };
477 fs.writeFileSync(opts.resources[0], 'data1');
478 fs.writeFileSync(opts.resources[1], 'data2');
479 setTimeout(function () {
480 fs.unlinkSync(opts.resources[0]);
481 fs.unlinkSync(opts.resources[1]);
482 }, 300);
483 waitOn(opts, function (err) {
484 expect(err).toNotExist();
485 done();
486 });
487 });
488 });
489
490 it('should timeout when file resources are available in reverse mode', function (done) {
491 temp.mkdir({}, function (err, dirPath) {
492 var opts = {
493 resources: [
494 path.resolve(dirPath, 'foo'),
495 path.resolve(dirPath, 'bar')
496 ],
497 reverse: true,
498 timeout: 1000
499 };
500 fs.writeFileSync(opts.resources[0], 'data1');
501 fs.writeFileSync(opts.resources[1], 'data2');
502 waitOn(opts, function (err) {
503 expect(err).toExist();
504 done();
505 });
506 });
507 });
508
509
510
511
512});