@boly38/check-port 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ [ < Back](../README.md)
2
+
3
+ # Github users HowTos
4
+
5
+ ## HowTo contribute
6
+
7
+ Please create an issue describing your goal / idea / question / bug description...
8
+
9
+ If you want to push some code create a pull request that link your issue using `#<issue_id>`.
10
+
11
+ ### HowTo test
12
+ * launch tests using `npm run test`.
13
+
14
+
15
+ # Maintainers HowTos
16
+
17
+ ## HowTo release
18
+
19
+ - verify actions secrets : need `NPM_DEPLOY_TOKEN` (from npmjs user profile)
20
+ - push `npmjs` branch according.
@@ -0,0 +1,53 @@
1
+ name: ci
2
+
3
+ ## REQUIREMENTS
4
+ # publish to npmjs : NPM_DEPLOY_TOKEN : https://www.npmjs.com/settings/<user>/tokens
5
+ on:
6
+ push:
7
+ branches:
8
+ - 'npmjs'
9
+ - 'main'
10
+ pull_request:
11
+ # run if and only if update on sub-part or repository matching paths
12
+ paths:
13
+ - 'src/**'
14
+ - 'tests/**'
15
+ - 'package.json'
16
+ - 'package-lock.json'
17
+ workflow_dispatch:
18
+
19
+ # A workflow run is made up of one or more jobs that can run sequentially or in parallel
20
+ jobs:
21
+ build:
22
+ runs-on: ubuntu-latest
23
+
24
+ strategy:
25
+ matrix:
26
+ # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
27
+ node-version: [ 22.x ]
28
+
29
+ steps:
30
+ - uses: actions/checkout@v4
31
+
32
+ - name: Use Node.js ${{ matrix.node-version }}
33
+ uses: actions/setup-node@v4
34
+ with:
35
+ node-version: ${{ matrix.node-version }}
36
+
37
+ - name: Install dependencies
38
+ # npx force-resolutions : in case of Permission denied: run it locally to fix package-lock.json
39
+ run: |
40
+ echo "install"
41
+ npm install
42
+ echo "show outdated (if any)"
43
+ npm outdated --depth=3 || echo "you must think about update your dependencies :)"
44
+
45
+ - name: Run tests
46
+ run: npm run test
47
+
48
+ - name: Publish NpmJS package
49
+ if: github.ref == 'refs/heads/npmjs'
50
+ run: |
51
+ echo "//registry.npmjs.org/:_authToken=${{ secrets.NPM_DEPLOY_TOKEN }}" > .npmrc
52
+ npm whoami # rely on .npmrc
53
+ npm publish
package/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # check-port
2
+
3
+ This project is inspired from famous repository
4
+ - [stdarg/tcp-port-used-check-port](https://github.com/stdarg/tcp-port-used)
5
+
6
+ # usage to check a port
7
+
8
+ ## setup
9
+ (need clone)
10
+ ````bash
11
+ # setup
12
+ npm install
13
+ ````
14
+
15
+ ## run sample
16
+
17
+ ````bash
18
+ # launch sample
19
+ node samples/basic.js
20
+ ````
21
+
22
+ to use this library
23
+ ````javascript
24
+ import checkPort from "@boly38/check-port"
25
+ ````
26
+
27
+ ## accept port on localhost:44204
28
+
29
+ Example to set up a port listener
30
+
31
+ ````bash
32
+ npm i http
33
+ node.exe -e "require('http').createServer((req, res) => res.end('Hello World')).listen(44204, () => console.log('Listening on port 44204'))"
34
+ ````
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@boly38/check-port",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "a tool that check a given TCP port is accepting connection",
6
+ "private": false,
7
+ "keywords": [
8
+ "tcp",
9
+ "port",
10
+ "check"
11
+ ],
12
+ "author": "Edmond Meinfelder, Boly38",
13
+ "fork-from": "https://github.com/stdarg/tcp-port-used",
14
+ "license": "MIT",
15
+ "bugs": {
16
+ "url": "https://github.com/boly38/check-port"
17
+ },
18
+ "homepage": "https://github.com/boly38/check-port",
19
+ "main": "src/index.js",
20
+ "scripts": {
21
+ "test": "mocha --trace-warnings --exit --timeout 180000 --unhandled-rejections=strict tests/*.test.js"
22
+ },
23
+ "dependencies": {
24
+ "debug": "^4.3.7",
25
+ "is2": "^2.0.9",
26
+ "net": "^1.0.2",
27
+ "util": "^0.12.5"
28
+ },
29
+ "devDependencies": {
30
+ "mocha": "^10.8.2"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
34
+ }
35
+ }
@@ -0,0 +1,15 @@
1
+ // users: import checkPort from "@boly38/check-port"
2
+ import checkPort from "../src/index.js";
3
+
4
+ const host = 'localhost';
5
+ const port = 44204;
6
+ checkPort.showTime();
7
+ console.log(`check ${host}:${port}...`);
8
+ checkPort.waitUntilUsedOnHost(port, host, 500, 4000)
9
+ .then(() => {
10
+ checkPort.showTime();
11
+ console.log(`Port ${host}:${port} is now in use.`);
12
+ }, err => {
13
+ checkPort.showTime();
14
+ console.log('Error:', err.message);
15
+ });
package/src/index.js ADDED
@@ -0,0 +1,230 @@
1
+ import is from "is2"
2
+ import util from "util"
3
+ import net from "net"
4
+ import debugLib from "debug"
5
+
6
+ const debug = debugLib('check-port');
7
+
8
+ // Global Values
9
+ const TIMEOUT = 2000;
10
+ const RETRY_TIME = 250;
11
+
12
+ const makeOptionsObj = (port, host, inUse, retryTimeMs, timeOutMs) => {
13
+ return {port, host, inUse, retryTimeMs, timeOutMs}
14
+ };
15
+
16
+ export default class checkPort {
17
+
18
+ static check(port, host) {
19
+ return new Promise((resolve, reject) => {
20
+ let inUse = true;
21
+ let client;
22
+ let opts;
23
+ if (!is.obj(port)) {
24
+ opts = makeOptionsObj(port, host);
25
+ } else {
26
+ opts = port;
27
+ }
28
+
29
+ if (!is.port(opts.port)) {
30
+ debug('Error invalid port: ' + util.inspect(opts.port));
31
+ reject(new Error('invalid port: ' + util.inspect(opts.port)));
32
+ return;
33
+ }
34
+
35
+ if (is.nullOrUndefined(opts.host)) {
36
+ debug('set host address to default 127.0.0.1');
37
+ opts.host = '127.0.0.1';
38
+ }
39
+
40
+ if (!is.positiveInt(opts.timeOutMs)) {
41
+ opts.timeOutMs = null;
42
+ }
43
+
44
+ function cleanUp() {
45
+ if (client) {
46
+ client.removeAllListeners('connect');
47
+ client.removeAllListeners('error');
48
+ client.end();
49
+ client.destroy();
50
+ client.unref();
51
+ }
52
+ //debug('listeners removed from client socket');
53
+ }
54
+
55
+ function onConnectCb() {
56
+ //debug('check - promise resolved - in use');
57
+ cleanUp();
58
+ resolve(inUse);
59
+ }
60
+
61
+ function onErrorCb(err) {
62
+ if (err.code !== 'ECONNREFUSED') {
63
+ cleanUp();
64
+ reject(err);
65
+ } else {
66
+ //debug('ECONNREFUSED');
67
+ inUse = false;
68
+ cleanUp();
69
+ resolve(inUse);
70
+ }
71
+ }
72
+
73
+ client = new net.Socket();
74
+ if (is.positiveInt(opts.timeOutMs)) {
75
+ client.setTimeout(opts.timeOutMs);
76
+ client.on('timeout', () => {
77
+ debug('check timeout');
78
+ inUse = false;
79
+ cleanUp();
80
+ resolve(inUse);
81
+ });
82
+ }
83
+ client.once('connect', onConnectCb);
84
+ client.once('error', onErrorCb);
85
+ client.connect({port: opts.port, host: opts.host}, () => {
86
+ });
87
+ });
88
+ }
89
+
90
+ static waitForStatus(port, host, inUse, retryTimeMs, timeOutMs) {
91
+ const lib = this;
92
+ return new Promise((resolve, reject) => {
93
+ let timeoutId;
94
+ let timedout = false;
95
+ let retryId;
96
+
97
+ // the first argument may be an object, if it is not, make an object
98
+ let opts;
99
+ if (is.obj(port)) {
100
+ opts = port;
101
+ } else {
102
+ opts = makeOptionsObj(port, host, inUse, retryTimeMs, timeOutMs);
103
+ }
104
+
105
+ if (!is.bool(opts.inUse)) {
106
+ reject(new Error('inUse must be a boolean'));
107
+ return;
108
+ }
109
+ if (!is.positiveInt(opts.retryTimeMs)) {
110
+ opts.retryTimeMs = RETRY_TIME;
111
+ debug('set retryTime to default ' + RETRY_TIME + 'ms');
112
+ }
113
+ if (!is.positiveInt(opts.timeOutMs)) {
114
+ opts.timeOutMs = TIMEOUT;
115
+ debug('set timeOutMs to default ' + TIMEOUT + 'ms');
116
+ }
117
+
118
+ function cleanUp() {
119
+ if (timeoutId) {
120
+ clearTimeout(timeoutId);
121
+ }
122
+ if (retryId) {
123
+ clearTimeout(retryId);
124
+ }
125
+ }
126
+
127
+ function timeoutFunc() {
128
+ timedout = true;
129
+ reject(new Error('timeout'));
130
+ cleanUp();
131
+ }
132
+
133
+ timeoutId = setTimeout(timeoutFunc, opts.timeOutMs);
134
+
135
+ function doCheck() {
136
+ lib.check(opts)// .port, opts.host)
137
+ .then(function (inUse) {
138
+ if (timedout) {
139
+ return;
140
+ }
141
+ //debug('doCheck inUse: '+inUse);
142
+ //debug('doCheck opts.inUse: '+opts.inUse);
143
+ if (inUse === opts.inUse) {
144
+ cleanUp();
145
+ resolve();
146
+ } else {
147
+ retryId = setTimeout(function () {
148
+ doCheck();
149
+ }, opts.retryTimeMs);
150
+ }
151
+ }, function (err) {
152
+ if (timedout) {
153
+ return;
154
+ }
155
+ cleanUp();
156
+ reject(err);
157
+ });
158
+ }
159
+
160
+ doCheck();
161
+ });
162
+ }
163
+
164
+ /**
165
+ * Creates a promise and fulfills it only when the socket is used.
166
+ * Will retry on an interval specified in retryTimeMs.
167
+ * Note: you have to be super-user to correctly test system ports (0-1023).
168
+ * @return {Object} A deferred promise from the q library.
169
+ * @param {Number|Object} port a valid TCP port number. If an object, must contain all the parameters as properties.
170
+ * @param port
171
+ * @param host
172
+ * @param retryTimeMs
173
+ * @param {Number} [retryTimeMs] the retry interval in milliseconds - default is is 500ms
174
+ * @param {Number} [timeOutMs] the amount of time to wait until port is free
175
+ * @param timeOutMs
176
+ * @returns {Promise<unknown>}
177
+ */
178
+ static waitUntilUsedOnHost = (port, host, retryTimeMs, timeOutMs) => {
179
+ // the first argument may be an object, if it is not, make an object
180
+ let opts;
181
+ if (is.obj(port)) {
182
+ opts = port;
183
+ opts.inUse = true;
184
+ } else {
185
+ opts = makeOptionsObj(port, host, true, retryTimeMs, timeOutMs);
186
+ }
187
+ return this.waitForStatus(opts);
188
+ }
189
+
190
+ /**
191
+ * For compatibility to previous version of module which did not have support
192
+ * for host addresses. This function works only for localhost.
193
+ * @param {Number} port a valid TCP port number. If an Object, must contain all the parameters as properties.
194
+ * @param {Number} [retryTimeMs] the retry interval in milliseconds - defaultis is 500ms
195
+ * @param {Number} [timeOutMs] the amount of time to wait until port is free
196
+ * @return {Object} A deferred promise from the q library.
197
+ *
198
+ * Example usage:
199
+ *
200
+ * var tcpPortUsed = require('tcp-port-used');
201
+ * tcpPortUsed.waitUntilUsed(44204, 500, 4000)
202
+ * .then(function() {
203
+ * console.log('Port 44204 is now in use.');
204
+ * }, function(err) {
205
+ * console.log('Error: ', error.message);
206
+ * });
207
+ */
208
+ static waitUntilUsed = (port, retryTimeMs = RETRY_TIME, timeOutMs = TIMEOUT) => {
209
+ // the first argument may be an object, if it is not, make an object
210
+ let opts;
211
+ if (is.obj(port)) {
212
+ opts = port;
213
+ opts.host = '127.0.0.1';
214
+ opts.inUse = true;
215
+ } else {
216
+ opts = makeOptionsObj(port, '127.0.0.1', true, retryTimeMs, timeOutMs);
217
+ }
218
+
219
+ return this.waitUntilUsedOnHost(opts);
220
+ }
221
+
222
+ static showTime = (instant = new Date()) => console.log(
223
+ `${instant.toLocaleTimeString('fr-FR', {
224
+ hour: '2-digit',
225
+ minute: '2-digit',
226
+ second: '2-digit'
227
+ })}:${instant.getMilliseconds()}`
228
+ );
229
+
230
+ }
@@ -0,0 +1,440 @@
1
+ 'use strict';
2
+ import assert from "assert";
3
+ import checkPort from "../src/index.js";
4
+ import net from "net";
5
+ import {after, before, describe, it} from "mocha";
6
+
7
+ let server;
8
+
9
+ function freePort(cb) {
10
+ if (!server) {
11
+ return cb(new Error('Port not in use'));
12
+ }
13
+
14
+ server.close();
15
+ server.unref();
16
+ server = undefined;
17
+ cb();
18
+ }
19
+
20
+ function bindPort(port, cb) {
21
+ if (server) {
22
+ return cb(new Error('Free the server port, first.'));
23
+ }
24
+
25
+ server = net.createServer();
26
+ server.listen(port);
27
+
28
+ function errEventCb(err) {
29
+ server.close();
30
+ if (cb) {
31
+ rmListeners();
32
+ cb(err);
33
+ }
34
+ server = undefined;
35
+ }
36
+
37
+ function listenEventCb() {
38
+ if (cb) {
39
+ rmListeners();
40
+ cb();
41
+ }
42
+ }
43
+
44
+ function rmListeners() {
45
+ server.removeListener('error', errEventCb);
46
+ server.removeListener('listening', listenEventCb);
47
+ }
48
+
49
+ server.on('error', errEventCb);
50
+ server.on('listening', listenEventCb);
51
+ }
52
+
53
+ describe('check arguments', function () {
54
+ it('should not accept negative port numbers in an obj', function (done) {
55
+ checkPort.check({port: -20, host: '127.0.0.1'})
56
+ .then(function () {
57
+ done(new Error('check unexpectedly succeeded'));
58
+ }, function (err) {
59
+ assert.ok(err && err.message === 'invalid port: -20');
60
+ done();
61
+ });
62
+ });
63
+
64
+ it('should not accept negative port numbers', function (done) {
65
+ checkPort.check(-20, '127.0.0.1')
66
+ .then(function () {
67
+ done(new Error('check unexpectedly succeeded'));
68
+ }, function (err) {
69
+ assert.ok(err && err.message === 'invalid port: -20');
70
+ done();
71
+ });
72
+ });
73
+
74
+ it('should not accept invalid types for port numbers in an obj', function (done) {
75
+ checkPort.check({port: 'hello', host: '127.0.0.1'})
76
+ .then(function () {
77
+ done(new Error('check unexpectedly succeeded'));
78
+ }, function (err) {
79
+ assert.ok(err && err.message === 'invalid port: \'hello\'');
80
+ done();
81
+ });
82
+ });
83
+
84
+ it('should not accept invalid types for port numbers', function (done) {
85
+ checkPort.check('hello', '127.0.0.1')
86
+ .then(function () {
87
+ done(new Error('check unexpectedly succeeded'));
88
+ }, function (err) {
89
+ assert.ok(err && err.message === 'invalid port: \'hello\'');
90
+ done();
91
+ });
92
+ });
93
+
94
+ it('should require an argument for a port number in an obj', function (done) {
95
+ checkPort.check({})
96
+ .then(function () {
97
+ done(new Error('check unexpectedly succeeded'));
98
+ }, function (err) {
99
+ assert.ok(err && err.message === 'invalid port: undefined');
100
+ done();
101
+ });
102
+ });
103
+
104
+ it('should require an argument for a port number', function (done) {
105
+ checkPort.check()
106
+ .then(function () {
107
+ done(new Error('check unexpectedly succeeded'));
108
+ }, function (err) {
109
+ assert.ok(err && err.message === 'invalid port: undefined');
110
+ done();
111
+ });
112
+ });
113
+
114
+ it('should not accept port number > 65535 in an obj', function (done) {
115
+ checkPort.check({port: 65536})
116
+ .then(function () {
117
+ done(new Error('check unexpectedly succeeded'));
118
+ }, function (err) {
119
+ assert.ok(err && err.message === 'invalid port: 65536');
120
+ done();
121
+ });
122
+ });
123
+
124
+
125
+ it('should not accept port number > 65535', function (done) {
126
+ checkPort.check(65536)
127
+ .then(function () {
128
+ done(new Error('check unexpectedly succeeded'));
129
+ }, function (err) {
130
+ assert.ok(err && err.message === 'invalid port: 65536');
131
+ done();
132
+ });
133
+ });
134
+
135
+ it('should not accept port number < 0 in an obj', function (done) {
136
+ checkPort.check({port: -1})
137
+ .then(function () {
138
+ done(new Error('check unexpectedly succeeded'));
139
+ }, function (err) {
140
+ assert.ok(err && err.message === 'invalid port: -1');
141
+ done();
142
+ });
143
+ });
144
+
145
+ it('should not accept port number < 0', function (done) {
146
+ checkPort.check(-1)
147
+ .then(function () {
148
+ done(new Error('check unexpectedly succeeded'));
149
+ }, function (err) {
150
+ assert.ok(err && err.message === 'invalid port: -1');
151
+ done();
152
+ });
153
+ });
154
+ });
155
+
156
+ describe('check functionality for unused port', function () {
157
+ before(function (done) {
158
+ bindPort(44202, function (err) {
159
+ done(err);
160
+ });
161
+ });
162
+
163
+ it('should return true for a used port with default host value in an obj', function (done) {
164
+ checkPort.check({port: 44202})
165
+ .then(function (inUse) {
166
+ assert.ok(inUse === true);
167
+ done();
168
+ }, function (err) {
169
+ done(err);
170
+ });
171
+ });
172
+
173
+
174
+ it('should return true for a used port with default host value', function (done) {
175
+ checkPort.check(44202)
176
+ .then(function (inUse) {
177
+ assert.ok(inUse === true);
178
+ done();
179
+ }, function (err) {
180
+ done(err);
181
+ });
182
+ });
183
+
184
+ it('should return true for a used port with default host value using arg obj', function (done) {
185
+ checkPort.check({port: 44202})
186
+ .then(function (inUse) {
187
+ assert.ok(inUse === true);
188
+ done();
189
+ }, function (err) {
190
+ done(err);
191
+ });
192
+ });
193
+
194
+ it('should return true for a used port with given host value using arg obj', function (done) {
195
+ checkPort.check({port: 44202, host: '127.0.0.1'})
196
+ .then(function (inUse) {
197
+ assert.ok(inUse === true);
198
+ done();
199
+ }, function (err) {
200
+ assert.ok(false);
201
+ done(err);
202
+ });
203
+ });
204
+
205
+
206
+ it('should return true for a used port with given host value', function (done) {
207
+ checkPort.check(44202, '127.0.0.1')
208
+ .then(function (inUse) {
209
+ assert.ok(inUse === true);
210
+ done();
211
+ }, function (err) {
212
+ assert.ok(false);
213
+ done(err);
214
+ });
215
+ });
216
+
217
+ it('should return false for an unused port and default host using arg object', function (done) {
218
+ checkPort.check({port: 44201})
219
+ .then(function (inUse) {
220
+ assert.ok(inUse === false);
221
+ done();
222
+ }, function (err) {
223
+ done(err);
224
+ });
225
+ });
226
+
227
+
228
+ it('should return false for an unused port and default host', function (done) {
229
+ checkPort.check(44201)
230
+ .then(function (inUse) {
231
+ assert.ok(inUse === false);
232
+ done();
233
+ }, function (err) {
234
+ done(err);
235
+ });
236
+ });
237
+
238
+ it('should return false for an unused port and given default host using arg object', function (done) {
239
+ checkPort.check({port: 44201, host: '127.0.0.1'})
240
+ .then(function (inUse) {
241
+ assert.ok(inUse === false);
242
+ done();
243
+ }, function (err) {
244
+ done(err);
245
+ });
246
+ });
247
+
248
+ it('should return false for an unused port and given default host', function (done) {
249
+ checkPort.check(44201, '127.0.0.1')
250
+ .then(function (inUse) {
251
+ assert.ok(inUse === false);
252
+ done();
253
+ }, function (err) {
254
+ done(err);
255
+ });
256
+ });
257
+
258
+ after(function (cb) {
259
+ freePort(function (err) {
260
+ cb(err);
261
+ });
262
+ });
263
+ });
264
+
265
+
266
+ describe('waitUntilUsedOnHost', function () {
267
+ this.timeout(5000);
268
+ this.slow(5000);
269
+
270
+ before(function () {
271
+ setTimeout(function () {
272
+ bindPort(44204);
273
+ }, 2000);
274
+ });
275
+
276
+ it('should wait until the port is listening using an arg object', function (done) {
277
+ checkPort.waitUntilUsedOnHost({port: 44204, host: '127.0.0.1', retryTimeMs: 500, timeOutMs: 4000})
278
+ .then(function () {
279
+ done();
280
+ }, function (err) {
281
+ done(err);
282
+ });
283
+ });
284
+
285
+ it('should wait until the port is listening', function (done) {
286
+ checkPort.waitUntilUsedOnHost(44204, '127.0.0.1', 500, 4000)
287
+ .then(function () {
288
+ done();
289
+ }, function (err) {
290
+ done(err);
291
+ });
292
+ });
293
+
294
+ it('should reject promise when given an invalid port using an arg object', function (done) {
295
+ checkPort.waitUntilUsedOnHost({port: 'hello', host: '127.0.0.1', retryTimeMs: 500, timeOutMs: 2000})
296
+ .then(function () {
297
+ done(new Error('waitUntil used unexpectedly successful.'));
298
+ }, function (err) {
299
+ if (err.message === 'invalid port: \'hello\'') {
300
+ done();
301
+ } else {
302
+ done(err);
303
+ }
304
+ });
305
+ });
306
+
307
+ it('should reject promise when given an invalid port', function (done) {
308
+ checkPort.waitUntilUsedOnHost('hello', '127.0.0.1', 500, 2000)
309
+ .then(function () {
310
+ done(new Error('waitUntil used unexpectedly successful.'));
311
+ }, function (err) {
312
+ if (err.message === 'invalid port: \'hello\'') {
313
+ done();
314
+ } else {
315
+ done(err);
316
+ }
317
+ });
318
+ });
319
+
320
+ it('should timeout when no port is listening using an arg obj', function (done) {
321
+ checkPort.waitUntilUsedOnHost({port: 44205, host: '127.0.0.1', retryTimeMs: 500, tmieOutMs: 2000})
322
+ .then(function () {
323
+ done(new Error('waitUntil used unexpectedly successful.'));
324
+ }, function (err) {
325
+ if (err.message === 'timeout') {
326
+ done();
327
+ } else {
328
+ done(err);
329
+ }
330
+ });
331
+ });
332
+
333
+
334
+ it('should timeout when no port is listening', function (done) {
335
+ checkPort.waitUntilUsedOnHost(44205, '127.0.0.1', 500, 2000)
336
+ .then(function () {
337
+ done(new Error('waitUntil used unexpectedly successful.'));
338
+ }, function (err) {
339
+ if (err.message === 'timeout') {
340
+ done();
341
+ } else {
342
+ done(err);
343
+ }
344
+ });
345
+ });
346
+
347
+ after(function (cb) {
348
+ freePort(function (err) {
349
+ cb(err);
350
+ });
351
+ });
352
+ });
353
+
354
+ describe('waitForStatus', function () {
355
+ this.timeout(5000);
356
+ this.slow(5000);
357
+
358
+ before(function () {
359
+ setTimeout(function () {
360
+ bindPort(44204);
361
+ }, 2000);
362
+ });
363
+
364
+ it('should wait until the port is listening using arg obj', function (done) {
365
+ checkPort.waitForStatus({port: 44204, host: '127.0.0.1', inUse: true, retryTimeMs: 500, timeOutMs: 4000})
366
+ .then(function () {
367
+ done();
368
+ }, function (err) {
369
+ done(err);
370
+ });
371
+ });
372
+
373
+ it('should wait until the port is listening', function (done) {
374
+ checkPort.waitForStatus(44204, '127.0.0.1', true, 500, 4000)
375
+ .then(function () {
376
+ done();
377
+ }, function (err) {
378
+ done(err);
379
+ });
380
+ });
381
+
382
+ it('should reject promise when given an invalid port using arg object', function (done) {
383
+ checkPort.waitForStatus({port: 'hello', host: '127.0.0.1', inUse: false, retryTimeMs: 500, timeOutMs: 2000})
384
+ .then(function () {
385
+ done(new Error('waitUntil used unexpectedly successful.'));
386
+ }, function (err) {
387
+ if (err.message === 'invalid port: \'hello\'') {
388
+ done();
389
+ } else {
390
+ done(err);
391
+ }
392
+ });
393
+ });
394
+
395
+ it('should reject promise when given an invalid port', function (done) {
396
+ checkPort.waitForStatus('hello', '127.0.0.1', false, 500, 2000)
397
+ .then(function () {
398
+ done(new Error('waitUntil used unexpectedly successful.'));
399
+ }, function (err) {
400
+ if (err.message === 'invalid port: \'hello\'') {
401
+ done();
402
+ } else {
403
+ done(err);
404
+ }
405
+ });
406
+ });
407
+
408
+ it('should timeout when no port is listening using arg obj', function (done) {
409
+ checkPort.waitUntilUsed({port: 44205, host: '127.0.0.1', inUse: true, retryTimeMs: 500, timeOutMs: 2000})
410
+ .then(function () {
411
+ done(new Error('waitUntil used unexpectedly successful.'));
412
+ }, function (err) {
413
+ if (err.message === 'timeout') {
414
+ done();
415
+ } else {
416
+ done(err);
417
+ }
418
+ });
419
+ });
420
+
421
+ it('should timeout when no port is listening', function (done) {
422
+ checkPort.waitUntilUsed(44205, 500, 2000)
423
+ .then(function () {
424
+ done(new Error('waitUntil used unexpectedly successful.'));
425
+ }, function (err) {
426
+ if (err.message === 'timeout') {
427
+ done();
428
+ } else {
429
+ done(err);
430
+ }
431
+ });
432
+ });
433
+
434
+ after(function (cb) {
435
+ freePort(function (err) {
436
+ cb(err);
437
+ });
438
+ });
439
+ });
440
+