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.7 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 getPorts() method with an argument of 3": {
  31. topic: function () {
  32. portfinder.getPorts(3, this.callback);
  33. },
  34. "should respond with the first three available ports (8005, 8006, 8007)": function (err, ports) {
  35. assert.isTrue(!err);
  36. assert.deepEqual(ports, [8005, 8006, 8007]);
  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 getPorts() method with an argument of 3": {
  51. topic: function () {
  52. portfinder.getPorts(3, this.callback);
  53. },
  54. "should respond with the first three available ports (8000, 8001, 80072": function (err, ports) {
  55. assert.isTrue(!err);
  56. assert.deepEqual(ports, [8000, 8001, 8002]);
  57. }
  58. }
  59. }
  60. }
  61. }).export(module);