You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB

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