arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/www/node_modules/portfinder/test/port-finder-test.js
www/node_modules/portfinder/test/port-finder-test.jsRaw
 1/*
 2 * portfinder-test.js: Tests for the `portfinder` module.
 3 *
 4 * (C) 2011, Charlie Robbins
 5 *
 6 */
 7
 8var vows = require('vows'),
 9    assert = require('assert'),
10    async = require('async'),
11    http = require('http'),
12    portfinder = require('../lib/portfinder');
13
14var servers = [];
15
16function createServers (callback) {
17  var base = 8000;
18  
19  async.whilst(
20    function () { return base < 8005 },
21    function (next) {
22      var server = http.createServer(function () { });
23      server.listen(base, next);
24      base++;
25      servers.push(server);
26    }, callback);
27}
28
29vows.describe('portfinder').addBatch({
30  "When using portfinder module": {
31    "with 5 existing servers": {
32      topic: function () {
33        createServers(this.callback);
34      },
35      "the getPort() method": {
36        topic: function () {
37          portfinder.getPort(this.callback);
38        },
39        "should respond with the first free port (8005)": function (err, port) {
40          assert.isTrue(!err);
41          assert.equal(port, 8005);
42        }
43      }
44    }
45  }
46}).addBatch({
47  "When using portfinder module": {
48    "with no existing servers": {
49      topic: function () {
50        servers.forEach(function (server) {
51          server.close();
52        });
53        
54        return null;
55      },
56      "the getPort() method": {
57        topic: function () {
58          portfinder.getPort(this.callback);
59        },
60        "should respond with the first free port (8000)": function (err, port) {
61          assert.isTrue(!err);
62          assert.equal(port, 8000);
63        }
64      }
65    }
66  }
67}).export(module);