random-port 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b71762c94a8e91f18c8ad99635fccc5a1d970933383788e33ce5c7e727309bb5
4
- data.tar.gz: 6615e680ca4ff3307658e32bc259068ec0d18b8855233f7f2f0e4f9677de59dc
3
+ metadata.gz: 32d50b829c0ddc8747b50eaf9f25bcb4bcbf4d42858e4271ece9b92ac650a6e9
4
+ data.tar.gz: 034eeff20d6f3d8015fca8d4dada4002c9afeefbb49c8f0a94eaf9db45049562
5
5
  SHA512:
6
- metadata.gz: 1d682b677cbc3cd75ea175905f7329606deb6bf791e78d7bdad3b011c9c0c2ef2b020cbda389cd83ae6677d57d42cf5dfa3884f78a6e22fd2961abd1c808c04f
7
- data.tar.gz: 273fd54dcd45d2acad27ec446eea6cb8570e79318cf92e20144f7dad46000a534a1ff958be7b347e0a20a9c1c8156b2a63e233013a5e064be0e5fa12ad576905
6
+ metadata.gz: 74b9f7720d63cb7c30bcc0fb3e14a82832a2de8a36eb8e7025261d0b7073d75ea73fd9d7d4a58be5afb350e616bf0a95954cd4e798d1482f6844a87ac537fe9c
7
+ data.tar.gz: e0532ba0cc8d19fa521f236c1a7ca6d265a603b6207880217b4101c3466f625e1b03029983967c4956a7f163ced06e70c31670665d9224bf7f4c278253a267ad
@@ -25,7 +25,7 @@ on:
25
25
  - master
26
26
  jobs:
27
27
  codecov:
28
- runs-on: ubuntu-20.04
28
+ runs-on: ubuntu-22.04
29
29
  steps:
30
30
  - uses: actions/checkout@v4
31
31
  - uses: ruby/setup-ruby@v1
@@ -33,6 +33,6 @@ jobs:
33
33
  ruby-version: 2.7
34
34
  - run: bundle update
35
35
  - run: bundle exec rake
36
- - uses: codecov/codecov-action@v3
36
+ - uses: codecov/codecov-action@v4
37
37
  with:
38
38
  token: ${{ secrets.CODECOV_TOKEN }}
@@ -24,7 +24,7 @@ on:
24
24
  pull_request:
25
25
  jobs:
26
26
  xcop:
27
- runs-on: ubuntu-20.04
27
+ runs-on: ubuntu-22.04
28
28
  steps:
29
29
  - uses: actions/checkout@v4
30
30
  - uses: g4s8/xcop-action@master
data/Gemfile CHANGED
@@ -25,8 +25,8 @@ gemspec
25
25
 
26
26
  gem 'minitest', '5.24.1', require: false
27
27
  gem 'rake', '13.2.1', require: false
28
- gem 'rubocop', '1.64.1', require: false
29
- gem 'rubocop-rspec', '3.0.2', require: false
28
+ gem 'rubocop', '1.65.0', require: false
29
+ gem 'rubocop-rspec', '3.0.3', require: false
30
30
  gem 'simplecov', '0.22.0', require: false
31
- gem 'threads', '0.3.0', require: false
31
+ gem 'threads', '0.4.0', require: false
32
32
  gem 'yard', '0.9.36', require: false
@@ -52,13 +52,14 @@ class RandomPort::Pool
52
52
  attr_reader :limit
53
53
 
54
54
  # Ctor.
55
- # @param [bool] Set it to FALSE if you want this pool to be NOT thread-safe
56
- # @param [int] Set the maximum number of ports in the pool
55
+ # @param [Boolean] sync Set it to FALSE if you want this pool to be NOT thread-safe
56
+ # @param [Integer] limit Set the maximum number of ports in the pool
57
57
  def initialize(sync: true, limit: 65_536)
58
58
  @ports = []
59
59
  @sync = sync
60
60
  @monitor = Monitor.new
61
61
  @limit = limit
62
+ @next = 0
62
63
  end
63
64
 
64
65
  # Application wide pool of ports
@@ -86,15 +87,17 @@ class RandomPort::Pool
86
87
  start = Time.now
87
88
  loop do
88
89
  if Time.now > start + timeout
89
- raise Timeout, "Can't find a place in the pool of #{@limit} ports \
90
- for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
90
+ raise \
91
+ Timeout,
92
+ "Can't find a place in the pool of #{@limit} ports " \
93
+ "for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
91
94
  end
92
95
  opts = safe do
93
96
  next if @ports.count + total > @limit
94
97
  opts = Array.new(0, total)
95
98
  begin
96
99
  (0..(total - 1)).each do |i|
97
- opts[i] = i.zero? ? take : take(opts[i - 1] + 1)
100
+ opts[i] = take(i.zero? ? @next : opts[i - 1] + 1)
98
101
  end
99
102
  rescue Errno::EADDRINUSE, SocketError
100
103
  next
@@ -106,6 +109,8 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
106
109
  opts
107
110
  end
108
111
  next if opts.nil?
112
+ @next = opts.max + 1
113
+ @next = 0 if @next > 65_535
109
114
  opts = opts[0] if total == 1
110
115
  return opts unless block_given?
111
116
  begin
@@ -129,6 +134,9 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
129
134
 
130
135
  private
131
136
 
137
+ # Find one possible TCP port.
138
+ # @param [Integer] opt Suggested port number
139
+ # @return [Integer] Port found
132
140
  def take(opt = 0)
133
141
  server = TCPServer.new('127.0.0.1', opt)
134
142
  p = server.addr[1]
data/random-port.gemspec CHANGED
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
28
28
  s.required_ruby_version = '>=2.3'
29
29
  s.name = 'random-port'
30
- s.version = '0.7.0'
30
+ s.version = '0.7.1'
31
31
  s.license = 'MIT'
32
32
  s.summary = 'Random TCP port'
33
33
  s.description = 'Reserves a random TCP port'
data/test/test_pool.rb CHANGED
@@ -115,6 +115,16 @@ class RandomPort::TestPool < Minitest::Test
115
115
  assert_equal(total, numbers.uniq.count)
116
116
  end
117
117
 
118
+ def test_acquires_unique_numbers_in_block
119
+ total = 25
120
+ numbers = (0..total - 1).map do
121
+ RandomPort::Pool::SINGLETON.acquire do |port|
122
+ port
123
+ end
124
+ end
125
+ assert_equal(total, numbers.uniq.count)
126
+ end
127
+
118
128
  def test_raises_when_too_many
119
129
  pool = RandomPort::Pool.new(limit: 1)
120
130
  pool.acquire
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.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-10 00:00:00.000000000 Z
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Reserves a random TCP port
14
14
  email: yegor256@gmail.com