|
| 1 | +import path from 'path'; |
| 2 | +import fs from 'fs'; |
| 3 | +import net from 'net'; |
| 4 | + |
| 5 | +import test from 'ava'; |
| 6 | + |
| 7 | +import portScanner from './lib/portscanner.js' |
| 8 | + |
| 9 | +test.before('Set #1 test server', t => { |
| 10 | + const server = net.createServer(); |
| 11 | + server.listen(3000, '127.0.0.1', () => t.end()); |
| 12 | +}); |
| 13 | + |
| 14 | +test.before('Set #2 test server', t => { |
| 15 | + const server2 = net.createServer(); |
| 16 | + server2.listen(2999, '127.0.0.1', () => t.end()); |
| 17 | +}); |
| 18 | + |
| 19 | + |
| 20 | +/* checkPortStatus */ |
| 21 | + |
| 22 | +test('checkPortStatus - taken', t => { |
| 23 | + t.plan(2); |
| 24 | + |
| 25 | + portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => { |
| 26 | + t.is(error, null); |
| 27 | + t.is(port, 'open'); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +test('checkPortStatus - free', t => { |
| 32 | + t.plan(2); |
| 33 | + |
| 34 | + portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => { |
| 35 | + t.is(error, null); |
| 36 | + t.is(port, 'closed'); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | + |
| 41 | +/* findPortInUse */ |
| 42 | + |
| 43 | +test('findPortInUse - taken port in range', t => { |
| 44 | + t.plan(2); |
| 45 | + |
| 46 | + portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => { |
| 47 | + t.is(error, null); |
| 48 | + t.is(port, 3000); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +test('findPortInUse - all ports in range free', t => { |
| 53 | + t.plan(2); |
| 54 | + |
| 55 | + portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => { |
| 56 | + t.is(error, null); |
| 57 | + t.false(port); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | + |
| 62 | +/* findPortNotInUse */ |
| 63 | + |
| 64 | +test('findAPortNotInUse - start from free port', t => { |
| 65 | + t.plan(2); |
| 66 | + |
| 67 | + portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => { |
| 68 | + t.is(error, null); |
| 69 | + t.true(port >= 3001 && port <= 3010); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +test('findAPortNotInUse - start from taken port', t => { |
| 74 | + t.plan(2); |
| 75 | + |
| 76 | + portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => { |
| 77 | + t.is(error, null); |
| 78 | + t.true(port >= 3001 && port <= 3010); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +test('findAPortNotInUse - all ports in range taken', t => { |
| 83 | + t.plan(2); |
| 84 | + |
| 85 | + portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => { |
| 86 | + t.is(error, null); |
| 87 | + t.false(port); |
| 88 | + }); |
| 89 | +}); |
0 commit comments