Path: blob/trunk/javascript/selenium-webdriver/test/net/portprober_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const net = require('node:net')21const portprober = require('selenium-webdriver/net/portprober')22const host = '127.0.0.1'2324describe('isFree', function () {25let server2627beforeEach(function () {28server = net.createServer(function () {})29})3031afterEach(function (done) {32if (!server) return done()33server.close(function () {34done()35})36})3738it('should work for INADDR_ANY', function (done) {39server.listen(0, function () {40const port = server.address().port41assertPortNotFree(port)42.then(function () {43return new Promise((resolve) => {44server.close(function () {45server = null46resolve(assertPortIsFree(port))47})48})49})50.then(function () {51done()52}, done)53})54})5556it('should work for a specific host', function (done) {57server.listen(0, host, function () {58const port = server.address().port59assertPortNotFree(port, host)60.then(function () {61return new Promise((resolve) => {62server.close(function () {63server = null64resolve(assertPortIsFree(port, host))65})66})67})68.then(function () {69done()70}, done)71})72})73})7475describe('findFreePort', function () {76let server7778beforeEach(function () {79server = net.createServer(function () {})80})8182afterEach(function (done) {83if (!server) return done()84server.close(function () {85done()86})87})8889it('should work for INADDR_ANY', function (done) {90portprober.findFreePort().then(function (port) {91server.listen(port, function () {92assertPortNotFree(port)93.then(function () {94return new Promise((resolve) => {95server.close(function () {96server = null97resolve(assertPortIsFree(port))98})99})100})101.then(function () {102done()103}, done)104})105})106})107108it('should work for a specific host', function (done) {109portprober.findFreePort(host).then(function (port) {110server.listen(port, host, function () {111assertPortNotFree(port, host)112.then(function () {113return new Promise((resolve) => {114server.close(function () {115server = null116resolve(assertPortIsFree(port, host))117})118})119})120.then(function () {121done()122}, done)123})124})125})126})127128function assertPortIsFree(port, opt_host) {129return portprober.isFree(port, opt_host).then(function (free) {130assert.ok(free, 'port should be free')131})132}133134function assertPortNotFree(port, opt_host) {135return portprober.isFree(port, opt_host).then(function (free) {136assert.ok(!free, 'port should is not free')137})138}139140141