arnaucube
arnaucube/socketioMEANseedPublic Self-hosted repository
socketioMEANseed/www/node_modules/portfinder/test/port-finder-socket-test.js
www/node_modules/portfinder/test/port-finder-socket-test.jsRaw
 1/*
 2 * portfinder-test.js: Tests for the `portfinder` module.
 3 *
 4 * (C) 2011, Charlie Robbins
 5 *
 6 */
 7
 8var assert = require('assert'),
 9    exec = require('child_process').exec,
10    net = require('net'),
11    path = require('path'),
12    async = require('async'),
13    vows = require('vows'),
14    portfinder = require('../lib/portfinder');
15
16var servers = [],
17    socketDir = path.join(__dirname, 'fixtures'),
18    badDir = path.join(__dirname, 'bad-dir');
19
20function createServers (callback) {
21  var base = 0;
22  
23  async.whilst(
24    function () { return base < 5 },
25    function (next) {
26      var server = net.createServer(function () { }),
27          name = base === 0 ? 'test.sock' : 'test' + base + '.sock';
28      
29      server.listen(path.join(socketDir, name), next);
30      base++;
31      servers.push(server);
32    }, callback);
33}
34
35vows.describe('portfinder').addBatch({
36  "When using portfinder module": {
37    "with 5 existing servers": {
38      topic: function () {
39        createServers(this.callback);
40      },
41      "the getPort() method": {
42        topic: function () {
43          portfinder.getSocket({
44            path: path.join(socketDir, 'test.sock')
45          }, this.callback);
46        },
47        "should respond with the first free socket (test5.sock)": function (err, socket) {
48          assert.isTrue(!err);
49          assert.equal(socket, path.join(socketDir, 'test5.sock'));
50        }
51      }
52    }
53  }
54}).addBatch({
55  "When using portfinder module": {
56    "with no existing servers": {
57      "the getSocket() method": {
58        "with a directory that doesnt exist": {
59          topic: function () {
60            var that = this;
61            exec('rm -rf ' + badDir, function () {
62              portfinder.getSocket({
63                path: path.join(badDir, 'test.sock')
64              }, that.callback);
65            });
66          },
67          "should respond with the first free socket (test.sock)": function (err, socket) {
68            assert.isTrue(!err);
69            assert.equal(socket, path.join(badDir, 'test.sock'));
70          }
71        },
72        "with a directory that exists": {
73          topic: function () {
74            portfinder.getSocket({
75              path: path.join(socketDir, 'exists.sock')
76            }, this.callback);
77          },
78          "should respond with the first free socket (exists.sock)": function (err, socket) {
79            assert.isTrue(!err);
80            assert.equal(socket, path.join(socketDir, 'exists.sock'));
81          }
82        }
83      }
84    }
85  }
86}).addBatch({
87  "When the tests are over": {
88    "necessary cleanup should take place": function () {
89      exec('rm -rf ' + badDir + ' ' + path.join(socketDir, '*'), function () { });
90    }
91  }
92}).export(module);