portprober_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Licensed to the Software Freedom Conservancy (SFC) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The SFC licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. 'use strict';
  18. const assert = require('assert');
  19. const net = require('net');
  20. const portprober = require('../../net/portprober');
  21. describe('isFree', function() {
  22. var server;
  23. beforeEach(function() {
  24. server = net.createServer(function(){});
  25. })
  26. afterEach(function(done) {
  27. if (!server) return done();
  28. server.close(function() {
  29. done();
  30. });
  31. });
  32. it('should work for INADDR_ANY', function(done) {
  33. server.listen(0, function() {
  34. var port = server.address().port;
  35. assertPortNotfree(port).then(function() {
  36. return new Promise(resolve => {
  37. server.close(function() {
  38. server = null;
  39. resolve(assertPortIsFree(port));
  40. });
  41. });
  42. }).then(function() { done(); }, done);
  43. });
  44. });
  45. it('should work for a specific host', function(done) {
  46. var host = '127.0.0.1';
  47. server.listen(0, host, function() {
  48. var port = server.address().port;
  49. assertPortNotfree(port, host).then(function() {
  50. return new Promise(resolve => {
  51. server.close(function() {
  52. server = null;
  53. resolve(assertPortIsFree(port, host));
  54. });
  55. });
  56. }).then(function() { done(); }, done);
  57. });
  58. });
  59. });
  60. describe('findFreePort', function() {
  61. var server;
  62. beforeEach(function() {
  63. server = net.createServer(function(){});
  64. })
  65. afterEach(function(done) {
  66. if (!server) return done();
  67. server.close(function() {
  68. done();
  69. });
  70. });
  71. it('should work for INADDR_ANY', function(done) {
  72. portprober.findFreePort().then(function(port) {
  73. server.listen(port, function() {
  74. assertPortNotfree(port).then(function() {
  75. return new Promise(resolve => {
  76. server.close(function() {
  77. server = null;
  78. resolve(assertPortIsFree(port));
  79. });
  80. });
  81. }).then(function() { done(); }, done);
  82. });
  83. });
  84. });
  85. it('should work for a specific host', function(done) {
  86. var host = '127.0.0.1';
  87. portprober.findFreePort(host).then(function(port) {
  88. server.listen(port, host, function() {
  89. assertPortNotfree(port, host).then(function() {
  90. return new Promise(resolve => {
  91. server.close(function() {
  92. server = null;
  93. resolve(assertPortIsFree(port, host));
  94. });
  95. });
  96. }).then(function() { done(); }, done);
  97. });
  98. });
  99. });
  100. });
  101. function assertPortIsFree(port, opt_host) {
  102. return portprober.isFree(port, opt_host).then(function(free) {
  103. assert.ok(free, 'port should be free');
  104. });
  105. }
  106. function assertPortNotfree(port, opt_host) {
  107. return portprober.isFree(port, opt_host).then(function(free) {
  108. assert.ok(!free, 'port should is not free');
  109. });
  110. }