-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic unit tests with Ava runner
- Loading branch information
1 parent
2c73a12
commit 9fdbdc3
Showing
3 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import net from 'net'; | ||
|
||
import test from 'ava'; | ||
|
||
import portScanner from './lib/portscanner.js' | ||
|
||
test.before('Set #1 test server', t => { | ||
const server = net.createServer(); | ||
server.listen(3000, '127.0.0.1', () => t.end()); | ||
}); | ||
|
||
test.before('Set #2 test server', t => { | ||
const server2 = net.createServer(); | ||
server2.listen(2999, '127.0.0.1', () => t.end()); | ||
}); | ||
|
||
|
||
/* checkPortStatus */ | ||
|
||
test('checkPortStatus - taken', t => { | ||
t.plan(2); | ||
|
||
portScanner.checkPortStatus(3000, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.is(port, 'open'); | ||
}); | ||
}); | ||
|
||
test('checkPortStatus - free', t => { | ||
t.plan(2); | ||
|
||
portScanner.checkPortStatus(3001, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.is(port, 'closed'); | ||
}); | ||
}); | ||
|
||
|
||
/* findPortInUse */ | ||
|
||
test('findPortInUse - taken port in range', t => { | ||
t.plan(2); | ||
|
||
portScanner.findAPortInUse(3000, 3010, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.is(port, 3000); | ||
}); | ||
}); | ||
|
||
test('findPortInUse - all ports in range free', t => { | ||
t.plan(2); | ||
|
||
portScanner.findAPortInUse(3001, 3010, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.false(port); | ||
}); | ||
}); | ||
|
||
|
||
/* findPortNotInUse */ | ||
|
||
test('findAPortNotInUse - start from free port', t => { | ||
t.plan(2); | ||
|
||
portScanner.findAPortNotInUse(3001, 3010, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.true(port >= 3001 && port <= 3010); | ||
}); | ||
}); | ||
|
||
test('findAPortNotInUse - start from taken port', t => { | ||
t.plan(2); | ||
|
||
portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.true(port >= 3001 && port <= 3010); | ||
}); | ||
}); | ||
|
||
test('findAPortNotInUse - all ports in range taken', t => { | ||
t.plan(2); | ||
|
||
portScanner.findAPortNotInUse(2999, 3000, '127.0.0.1', (error, port) => { | ||
t.is(error, null); | ||
t.false(port); | ||
}); | ||
}); |