Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/test/net/portprober_test.js
2885 views
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
18
'use strict'
19
20
const assert = require('node:assert')
21
const net = require('node:net')
22
const portprober = require('selenium-webdriver/net/portprober')
23
const host = '127.0.0.1'
24
25
describe('isFree', function () {
26
let server
27
28
beforeEach(function () {
29
server = net.createServer(function () {})
30
})
31
32
afterEach(function (done) {
33
if (!server) return done()
34
server.close(function () {
35
done()
36
})
37
})
38
39
it('should work for INADDR_ANY', function (done) {
40
server.listen(0, function () {
41
const port = server.address().port
42
assertPortNotFree(port)
43
.then(function () {
44
return new Promise((resolve) => {
45
server.close(function () {
46
server = null
47
resolve(assertPortIsFree(port))
48
})
49
})
50
})
51
.then(function () {
52
done()
53
}, done)
54
})
55
})
56
57
it('should work for a specific host', function (done) {
58
server.listen(0, host, function () {
59
const port = server.address().port
60
assertPortNotFree(port, host)
61
.then(function () {
62
return new Promise((resolve) => {
63
server.close(function () {
64
server = null
65
resolve(assertPortIsFree(port, host))
66
})
67
})
68
})
69
.then(function () {
70
done()
71
}, done)
72
})
73
})
74
})
75
76
describe('findFreePort', function () {
77
let server
78
79
beforeEach(function () {
80
server = net.createServer(function () {})
81
})
82
83
afterEach(function (done) {
84
if (!server) return done()
85
server.close(function () {
86
done()
87
})
88
})
89
90
it('should work for INADDR_ANY', function (done) {
91
portprober.findFreePort().then(function (port) {
92
server.listen(port, function () {
93
assertPortNotFree(port)
94
.then(function () {
95
return new Promise((resolve) => {
96
server.close(function () {
97
server = null
98
resolve(assertPortIsFree(port))
99
})
100
})
101
})
102
.then(function () {
103
done()
104
}, done)
105
})
106
})
107
})
108
109
it('should work for a specific host', function (done) {
110
portprober.findFreePort(host).then(function (port) {
111
server.listen(port, host, function () {
112
assertPortNotFree(port, host)
113
.then(function () {
114
return new Promise((resolve) => {
115
server.close(function () {
116
server = null
117
resolve(assertPortIsFree(port, host))
118
})
119
})
120
})
121
.then(function () {
122
done()
123
}, done)
124
})
125
})
126
})
127
})
128
129
function assertPortIsFree(port, opt_host) {
130
return portprober.isFree(port, opt_host).then(function (free) {
131
assert.ok(free, 'port should be free')
132
})
133
}
134
135
function assertPortNotFree(port, opt_host) {
136
return portprober.isFree(port, opt_host).then(function (free) {
137
assert.ok(!free, 'port should is not free')
138
})
139
}
140
141