random-port 0.3.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rultor.yml +1 -1
- data/lib/random-port/pool.rb +20 -4
- data/random-port.gemspec +1 -1
- data/test/test_pool.rb +8 -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: c70d46732ca42efae0aceaf8abc0c366b5f96d7742808d1f9b6c20f21ec560b7
|
4
|
+
data.tar.gz: dc873aa7fcecfd86f40ea5cdde11569b863fad043f584b4a463f9fc46a627dc0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: adb1ecb591ebb526b110b5c478a6a54db907335a2d89ae5f58d542ea9071f85d6a258b4bc046a3ef33fb0a73b5bb32145cb6ec6de3840db108a1cb211f37d565
|
7
|
+
data.tar.gz: d1ab0249446805c014fcc2bdf2ecce929c69149af8f798dedfcc55129461ae567ad9fc912761e8cb4971b36bc4cb1d13decacc26b6e26a14bbe36a9b2fd14500
|
data/.rultor.yml
CHANGED
data/lib/random-port/pool.rb
CHANGED
@@ -35,6 +35,9 @@ module RandomPort
|
|
35
35
|
# # to the pool afterwards.
|
36
36
|
# end
|
37
37
|
#
|
38
|
+
# You can specify the maximum amount of ports to acquire, using +limit+.
|
39
|
+
# If more acquiring requests will arrive, an exception will be raised.
|
40
|
+
#
|
38
41
|
# The class is thread-safe, by default. You can configure it to be
|
39
42
|
# not-thread-safe, using optional <tt>sync</tt> argument of the constructor.
|
40
43
|
#
|
@@ -42,10 +45,15 @@ module RandomPort
|
|
42
45
|
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
|
43
46
|
# License:: MIT
|
44
47
|
class Pool
|
45
|
-
|
48
|
+
# If can't acquire by time out.
|
49
|
+
class Timeout < StandardError; end
|
50
|
+
|
51
|
+
# Ctor.
|
52
|
+
def initialize(sync: false, limit: 65_536)
|
46
53
|
@ports = []
|
47
54
|
@sync = sync
|
48
55
|
@monitor = Monitor.new
|
56
|
+
@limit = limit
|
49
57
|
end
|
50
58
|
|
51
59
|
# Application wide pool of ports
|
@@ -62,13 +70,21 @@ module RandomPort
|
|
62
70
|
end
|
63
71
|
|
64
72
|
# Acquire a new random TCP port.
|
65
|
-
|
73
|
+
#
|
74
|
+
# You can specify the amount of seconds to wait until a new port
|
75
|
+
# is available.
|
76
|
+
def acquire(timeout: 4)
|
77
|
+
start = Time.now
|
66
78
|
loop do
|
79
|
+
raise Timeout if Time.now > start + timeout
|
80
|
+
next if @ports.count >= @limit
|
67
81
|
server = TCPServer.new('127.0.0.1', 0)
|
68
82
|
port = server.addr[1]
|
69
83
|
server.close
|
70
|
-
|
71
|
-
|
84
|
+
safe do
|
85
|
+
next if @ports.include?(port)
|
86
|
+
@ports << port
|
87
|
+
end
|
72
88
|
return port unless block_given?
|
73
89
|
begin
|
74
90
|
return yield port
|
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.4.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
@@ -71,6 +71,14 @@ module RandomPort
|
|
71
71
|
assert_equal(total, numbers.uniq.count)
|
72
72
|
end
|
73
73
|
|
74
|
+
def test_raises_when_too_many
|
75
|
+
pool = Pool.new(limit: 1)
|
76
|
+
pool.acquire
|
77
|
+
assert_raises Pool::Timeout do
|
78
|
+
pool.acquire(timeout: 0.1)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
74
82
|
def test_acquires_unique_numbers_in_no_sync_mode
|
75
83
|
total = 25
|
76
84
|
pool = Pool.new(sync: false)
|
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.4.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-08-
|
11
|
+
date: 2019-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: codecov
|