random-port 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/random-port/pool.rb +40 -14
- data/random-port.gemspec +1 -1
- data/test/test_pool.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f52b8bc7d8b8a31486ae04316b4c91491de3df9b617d8c3ef293e6b889edd4b4
|
4
|
+
data.tar.gz: 98f9ac9302aa206669e4a73f19ab0d32e4cec55312214934fd25059e76a25726
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0586b04575f809747b52719f9e614b982d4bbd9960c0b81f426827b6ceafff8b8d9539bad2882e7f3254cf743bf4d4c65aea7cd515ab0033eafc2bcf26ed301
|
7
|
+
data.tar.gz: 6b73af33efeb0b03a691e5113e7611d20f6b9e2597b0ff7431e2bfdb5498a1a5e30efc250fbebcf98e07046008d4b06a2e64340429f1960abcabd6305a014ce2
|
data/lib/random-port/pool.rb
CHANGED
@@ -73,39 +73,65 @@ module RandomPort
|
|
73
73
|
|
74
74
|
# Acquire a new random TCP port.
|
75
75
|
#
|
76
|
+
# You can specify the number of ports to acquire. If it's more than one,
|
77
|
+
# an array will be returned.
|
78
|
+
#
|
76
79
|
# You can specify the amount of seconds to wait until a new port
|
77
80
|
# is available.
|
78
|
-
def acquire(timeout: 4)
|
81
|
+
def acquire(total = 1, timeout: 4)
|
79
82
|
start = Time.now
|
80
83
|
loop do
|
81
84
|
if Time.now > start + timeout
|
82
85
|
raise Timeout, "Can't find a place in the pool of #{@limit} ports \
|
83
|
-
in #{format('%.02f', Time.now - start)}s"
|
86
|
+
for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
|
84
87
|
end
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
opts = safe do
|
89
|
+
next if @ports.count + total > @limit
|
90
|
+
opts = Array.new(0, total)
|
91
|
+
begin
|
92
|
+
(0..(total - 1)).each do |i|
|
93
|
+
opts[i] = i.zero? ? take : take(opts[i - 1] + 1)
|
94
|
+
end
|
95
|
+
rescue Errno::EADDRINUSE, SocketError
|
96
|
+
next
|
97
|
+
end
|
98
|
+
next if opts.any? { |p| @ports.include?(p) }
|
99
|
+
d = total * (total - 1) / 2
|
100
|
+
next unless opts.inject(&:+) - total * opts.min == d
|
101
|
+
@ports += opts
|
102
|
+
opts
|
92
103
|
end
|
93
|
-
|
104
|
+
next if opts.nil?
|
105
|
+
opts = opts[0] if total == 1
|
106
|
+
return opts unless block_given?
|
94
107
|
begin
|
95
|
-
return yield
|
108
|
+
return yield opts
|
96
109
|
ensure
|
97
|
-
safe { @ports.delete(
|
110
|
+
safe { @ports.delete(opts) }
|
98
111
|
end
|
99
112
|
end
|
100
113
|
end
|
101
114
|
|
102
|
-
# Return it back to the pool.
|
115
|
+
# Return it/them back to the pool.
|
103
116
|
def release(port)
|
104
|
-
safe
|
117
|
+
safe do
|
118
|
+
if port.is_a?(Array)
|
119
|
+
port.each { |p| @ports.delete(p) }
|
120
|
+
else
|
121
|
+
@ports.delete(port)
|
122
|
+
end
|
123
|
+
end
|
105
124
|
end
|
106
125
|
|
107
126
|
private
|
108
127
|
|
128
|
+
def take(opt = 0)
|
129
|
+
server = TCPServer.new('127.0.0.1', opt)
|
130
|
+
p = server.addr[1]
|
131
|
+
server.close
|
132
|
+
p
|
133
|
+
end
|
134
|
+
|
109
135
|
def safe
|
110
136
|
if @sync
|
111
137
|
@monitor.synchronize { yield }
|
data/random-port.gemspec
CHANGED
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.rubygems_version = '2.3.3'
|
32
32
|
s.required_ruby_version = '>=2.3'
|
33
33
|
s.name = 'random-port'
|
34
|
-
s.version = '0.
|
34
|
+
s.version = '0.5.0'
|
35
35
|
s.license = 'MIT'
|
36
36
|
s.summary = 'Random TCP port'
|
37
37
|
s.description = 'Reserves a random TCP port'
|
data/test/test_pool.rb
CHANGED
@@ -39,6 +39,13 @@ module RandomPort
|
|
39
39
|
pool.release(port)
|
40
40
|
end
|
41
41
|
|
42
|
+
def test_acquires_and_releases_three_ports
|
43
|
+
pool = Pool.new
|
44
|
+
ports = pool.acquire(3, timeout: 16)
|
45
|
+
assert_equal(3, ports.count)
|
46
|
+
pool.release(ports)
|
47
|
+
end
|
48
|
+
|
42
49
|
def test_acquires_and_releases_in_block
|
43
50
|
result = Pool.new.acquire do |port|
|
44
51
|
assert(!port.nil?)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: random-port
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yegor Bugayenko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|