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.

66 lines
1.6 KiB

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