/
all.js
164 lines (131 loc) · 4.08 KB
/
all.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
var waitForPort = require('../index'),
net = require('net'),
sinon = require('sinon'),
should = require('should'),
helpers = require('./helpers'),
assert = require('assert');
describe('waitForPort', function() {
var clock;
before(function() { clock = sinon.useFakeTimers(); });
after(function() { clock.restore(); });
function tryToConnect(host, port, cb) {
var client = net.createConnection(port, host, function() {
client.end();
cb(null);
});
}
describe('when port is not open', function() {
var host = 'localhost', port;
var server = null;
before(function(done) {
helpers.nextRandomPort(function(err, next) {
port = next;
done(err);
});
});
beforeEach(function() {
server = net.createServer();
});
afterEach(function(done) {
server.close(done);
});
it('calls us back', function(done) {
var portIsReady = false;
waitForPort(host, port, function(err) {
if (err) return done(err);
portIsReady.should.be.true;
done(); // called more than once?
});
server.listen(port, function() {
portIsReady = true;
});
clock.tick(10); // TODO: magic number
});
});
describe('when port is open already', function() {
var host = 'localhost', port;
var server;
before(function(done) {
server = net.createServer();
server.listen(0 /* any port */, function() {
var address = server.address();
port = address.port;
// console.log('listening on port ' + port);
done();
});
});
after(function(done) {
server.close(function() {
// console.log('server closed');
done();
});
});
it('calls us back', function(done) {
waitForPort(host, port, function(err) {
// no error
(err === null).should.be.true;
// can we connect?
tryToConnect(host, port, done);
});
});
});
describe('retries', function() {
var host = 'localhost', port;
beforeEach(function(done) {
helpers.nextRandomPort(function(err, next) {
port = next;
done(err);
});
});
it('fails if no server and we run out of retries', function(done) {
waitForPort(host, port, function(err) {
err.should.match(/out of retries/);
done();
});
clock.tick(50000000); // TODO: magic number
})
});
describe('retries', function() {
var host = 'localhost', port = 0;
beforeEach(function(done) {
helpers.nextRandomPort(function(err, next) {
if (err) throw new Error(err);
port = next;
done();
});
});
describe('invalid options', function() {
it('fails for invalid options', function() {
(function() {
waitForPort(host, port, { numRetries: -1 }, function() {});
}).should.throw(/invalid value for option/);
(function() {
waitForPort(host, port, { retryInterval: -1 }, function() {});
}).should.throw(/invalid value for option/);
});
});
var variations = [
{ numRetries: 1, retryInterval: 1 },
{ numRetries: 1, retryInterval: 10000 },
{ numRetries: 7, retryInterval: 1 },
{ numRetries: 7, retryInterval: 6577 }
];
variations.forEach(function(options) {
it('does callback with error when run out of retries, retries=' + options.numRetries + ', interval=' + options.retryInterval, function(done) {
waitForPort(host, port, options, function(err) {
err.should.match(/out of retries/);
done();
});
clock.tick(options.numRetries * options.retryInterval + 1);
});
it('does not call back if numRetries and/or retryInterval large enough, retries=' + options.numRetries + ', interval=' + options.retryInterval, function(done) {
var err = null;
waitForPort(host, port, options, function() {
err = new Error('should not be called');
});
clock.tick(options.numRetries * options.retryInterval - 1);
done(err);
});
});
});
});