UNPKG

3.83 kBJavaScriptView Raw
1'use strict';
2
3var net = require('net');
4var op = require('../');
5
6module.exports = {
7 'find a single open port': function (test) {
8 op.find(
9 {
10 ports: [ 1024 ]
11 },
12 function (err, port) {
13 test.ok(!err);
14 test.equals(1024, port);
15 test.done();
16 });
17 },
18
19 'find a port not in the avoid list': function (test) {
20 op.find(
21 {
22 ports: [ 1024, 1025, 1026, 1027 ],
23 avoid: [ 1025, 1026 ],
24 count: 2
25 },
26 function (err, ports) {
27 test.ok(!err);
28 test.deepEqual([1024, 1027], ports);
29 test.done();
30 });
31 },
32
33 'find a port not in the avoid list outer case': function (test) {
34 op.find(
35 {
36 ports: [ 1024, 1025, 1026, 1027 ],
37 avoid: [ '1024', '1027' ],
38 count: 2
39 },
40 function (err, ports) {
41 test.ok(!err);
42 test.deepEqual([1025, 1026], ports);
43 test.done();
44 });
45 },
46
47 'find a port not in the avoid list starting port': function (test) {
48 op.find(
49 {
50 startingPort: 1024,
51 avoid: [ 1024, 1026 ],
52 count: 2
53 },
54 function (err, ports) {
55 test.ok(!err);
56 test.deepEqual([1025, 1027], ports);
57 test.done();
58 });
59 },
60
61 'port that requires sudo': function (test) {
62 op.find(
63 {
64 ports: [ 1023 ]
65 },
66 function (err, port) {
67 test.equals('no ports found', err.message);
68 test.ok(!port);
69 test.done();
70 });
71 },
72
73 'find a port after a closed port': function (test) {
74 var server = net.createServer();
75 server.listen(1024, function () {
76 op.find(
77 {
78 ports: [ 1024, 1025 ]
79 },
80 function (err, port) {
81 test.ok(!err);
82 test.equals(1025, port);
83 server.close();
84 test.done();
85 });
86 });
87 },
88
89 'find multiple open ports': function (test) {
90 op.find(
91 {
92 ports: [ 1024, 1025 ],
93 count: 2
94 },
95 function (err, ports) {
96 test.ok(!err);
97 test.equals(2, ports.length);
98 test.equals(1024, ports[0]);
99 test.equals(1025, ports[1]);
100 test.done();
101 });
102 },
103
104 'find open defaults': function (test) {
105 op.find(
106 function (err, port) {
107 test.ok(!err);
108 test.equals(1024, port);
109 test.done();
110 });
111 },
112
113 'find open with starting port': function (test) {
114 op.find(
115 {
116 startingPort: 10000
117 },
118 function (err, port) {
119 test.ok(!err);
120 test.equals(10000, port);
121 test.done();
122 });
123 },
124
125 'bad starting and ending port': function (test) {
126 try {
127 op.find(
128 {
129 startingPort: 1025,
130 endingPort: 1024
131 },
132 function (err, port) {
133 test.fail("this shouldn't happen");
134 });
135 } catch (ex) {
136 test.done();
137 }
138 },
139
140 'bad starting port': function (test) {
141 try {
142 op.find(
143 {
144 startingPort: 0
145 },
146 function (err, port) {
147 test.fail("this shouldn't happen");
148 });
149 } catch (ex) {
150 test.done();
151 }
152 },
153
154 'bad ending port': function (test) {
155 try {
156 op.find(
157 {
158 startingPort: op.maxPort + 1
159 },
160 function (err, port) {
161 test.fail("this shouldn't happen");
162 });
163 } catch (ex) {
164 test.done();
165 }
166 },
167
168 'find no open port hitting ending port': function (test) {
169 var server = net.createServer();
170 server.listen(1024, function () {
171 op.find(
172 {
173 startingPort: 1024,
174 endingPort: 1024
175 },
176 function (err, port) {
177 test.equals('no ports found', err.message);
178 test.ok(!port);
179 server.close();
180 test.done();
181 });
182 });
183 }
184};