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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1cd0eda08b292d0482e433b081e984bde31aed33b758faf81815d7e6bebab95a
4
- data.tar.gz: cec78b2727717531359e826f1e650981dfc6c0bc37fcb914ce503424824c66ae
3
+ metadata.gz: c70d46732ca42efae0aceaf8abc0c366b5f96d7742808d1f9b6c20f21ec560b7
4
+ data.tar.gz: dc873aa7fcecfd86f40ea5cdde11569b863fad043f584b4a463f9fc46a627dc0
5
5
  SHA512:
6
- metadata.gz: ca08ad8ead3c38bcc22431d5526bad4142ecfdcc3341af242a162872d3caa33261bbe4618d6c350f20b18e6401a32e69ab5da2af2e4631957a30ef679dd26cac
7
- data.tar.gz: d349dce4ab4e7171b15526f8b731d95874f3311eebbc247970210e5581d694aee1587e12c5044e52f08e1785dece7441b3ba4263bd7c10cc4983f5efa22b44a9
6
+ metadata.gz: adb1ecb591ebb526b110b5c478a6a54db907335a2d89ae5f58d542ea9071f85d6a258b4bc046a3ef33fb0a73b5bb32145cb6ec6de3840db108a1cb211f37d565
7
+ data.tar.gz: d1ab0249446805c014fcc2bdf2ecce929c69149af8f798dedfcc55129461ae567ad9fc912761e8cb4971b36bc4cb1d13decacc26b6e26a14bbe36a9b2fd14500
@@ -3,7 +3,7 @@ assets:
3
3
  install: |-
4
4
  export GEM_HOME=~/.ruby
5
5
  export GEM_PATH=$GEM_HOME:$GEM_PATH
6
- bundle install
6
+ bundle install --no-color
7
7
  release:
8
8
  script: |-
9
9
  bundle exec rake
@@ -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
- def initialize(sync: false)
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
- def acquire
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
- next if @ports.include?(port)
71
- safe { @ports << port }
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
@@ -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.3.2'
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'
@@ -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.3.2
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-05 00:00:00.000000000 Z
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov