random-port 0.7.0 → 0.7.2

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: b71762c94a8e91f18c8ad99635fccc5a1d970933383788e33ce5c7e727309bb5
4
- data.tar.gz: 6615e680ca4ff3307658e32bc259068ec0d18b8855233f7f2f0e4f9677de59dc
3
+ metadata.gz: 7955c6c026633ceb489c05357e4d0383aca879252680a87053339682e014fe0f
4
+ data.tar.gz: ab010bcab3569cce98f608c38dfe23929ff5d8c94bba8a15e46c1e486aef71d0
5
5
  SHA512:
6
- metadata.gz: 1d682b677cbc3cd75ea175905f7329606deb6bf791e78d7bdad3b011c9c0c2ef2b020cbda389cd83ae6677d57d42cf5dfa3884f78a6e22fd2961abd1c808c04f
7
- data.tar.gz: 273fd54dcd45d2acad27ec446eea6cb8570e79318cf92e20144f7dad46000a534a1ff958be7b347e0a20a9c1c8156b2a63e233013a5e064be0e5fa12ad576905
6
+ metadata.gz: 5861bf9d24fbb7c4cdc9ca9393736c0d65ee3e30f207a715ac0f7366f8b13ebaf442e5ebff10ae1787515bceab7f257aea44b9592a09c7f9f27d57015805ac2b
7
+ data.tar.gz: de319e8a9c70bc72a98ea456297079c732a41a3deac2b9c943065a601446ff2f828cfc7bb1a0a25f24327ed72fd6605129021aa06e23defe89095a7257bfe0a8
@@ -28,7 +28,7 @@ name: actionlint
28
28
  - master
29
29
  jobs:
30
30
  actionlint:
31
- runs-on: ubuntu-22.04
31
+ runs-on: ubuntu-24.04
32
32
  steps:
33
33
  - uses: actions/checkout@v4
34
34
  - name: Download actionlint
@@ -25,7 +25,7 @@ on:
25
25
  - master
26
26
  jobs:
27
27
  codecov:
28
- runs-on: ubuntu-20.04
28
+ runs-on: ubuntu-24.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@v5
37
37
  with:
38
38
  token: ${{ secrets.CODECOV_TOKEN }}
@@ -24,7 +24,7 @@ name: copyrights
24
24
  pull_request:
25
25
  jobs:
26
26
  copyrights:
27
- runs-on: ubuntu-22.04
27
+ runs-on: ubuntu-24.04
28
28
  steps:
29
29
  - uses: actions/checkout@v4
30
- - uses: yegor256/copyrights-action@0.0.4
30
+ - uses: yegor256/copyrights-action@0.0.8
@@ -32,7 +32,7 @@ concurrency:
32
32
  cancel-in-progress: true
33
33
  jobs:
34
34
  markdown-lint:
35
- runs-on: ubuntu-22.04
35
+ runs-on: ubuntu-24.04
36
36
  steps:
37
37
  - uses: actions/checkout@v4
38
38
  - uses: articulate/actions-markdownlint@v1
@@ -28,7 +28,7 @@ on:
28
28
  - master
29
29
  jobs:
30
30
  pdd:
31
- runs-on: ubuntu-22.04
31
+ runs-on: ubuntu-24.04
32
32
  steps:
33
33
  - uses: actions/checkout@v4
34
34
  - uses: volodya-lombrozo/pdd-action@master
@@ -31,8 +31,8 @@ jobs:
31
31
  name: test
32
32
  strategy:
33
33
  matrix:
34
- os: [ubuntu-20.04, macos-12]
35
- ruby: [2.7, 3.2]
34
+ os: [ubuntu-24.04, macos-15]
35
+ ruby: [2.7, 3.3]
36
36
  runs-on: ${{ matrix.os }}
37
37
  steps:
38
38
  - uses: actions/checkout@v4
@@ -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-24.04
28
28
  steps:
29
29
  - uses: actions/checkout@v4
30
30
  - uses: g4s8/xcop-action@master
data/.rultor.yml CHANGED
@@ -19,7 +19,7 @@
19
19
  # SOFTWARE.
20
20
  ---
21
21
  docker:
22
- image: yegor256/rultor-image:1.23.1
22
+ image: yegor256/rultor-ruby
23
23
  assets:
24
24
  rubygems.yml: yegor256/home#assets/rubygems.yml
25
25
  install: |-
data/Gemfile CHANGED
@@ -23,10 +23,10 @@
23
23
  source 'https://rubygems.org'
24
24
  gemspec
25
25
 
26
- gem 'minitest', '5.24.1', require: false
26
+ gem 'minitest', '5.25.4', 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.69.2', require: false
29
+ gem 'rubocop-rspec', '3.3.0', require: false
30
30
  gem 'simplecov', '0.22.0', require: false
31
- gem 'threads', '0.3.0', require: false
32
- gem 'yard', '0.9.36', require: false
31
+ gem 'threads', '0.4.1', require: false
32
+ gem 'yard', '0.9.37', require: false
@@ -22,8 +22,9 @@
22
22
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
23
  # SOFTWARE.
24
24
 
25
- require 'socket'
26
25
  require 'monitor'
26
+ require 'socket'
27
+ require 'tago'
27
28
  require_relative 'module'
28
29
 
29
30
  # Pool of TPC ports.
@@ -52,13 +53,14 @@ class RandomPort::Pool
52
53
  attr_reader :limit
53
54
 
54
55
  # 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
56
+ # @param [Boolean] sync Set it to FALSE if you want this pool to be NOT thread-safe
57
+ # @param [Integer] limit Set the maximum number of ports in the pool
57
58
  def initialize(sync: true, limit: 65_536)
58
59
  @ports = []
59
60
  @sync = sync
60
61
  @monitor = Monitor.new
61
62
  @limit = limit
63
+ @next = 1024
62
64
  end
63
65
 
64
66
  # Application wide pool of ports
@@ -84,17 +86,21 @@ class RandomPort::Pool
84
86
  # is available.
85
87
  def acquire(total = 1, timeout: 4)
86
88
  start = Time.now
89
+ attempt = 0
87
90
  loop do
88
91
  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"
92
+ raise \
93
+ Timeout,
94
+ "Can't find a place in the pool of #{@limit} ports " \
95
+ "for #{total} port(s), after #{attempt} attempts in #{start.ago}"
91
96
  end
97
+ attempt += 1
92
98
  opts = safe do
93
99
  next if @ports.count + total > @limit
94
100
  opts = Array.new(0, total)
95
101
  begin
96
102
  (0..(total - 1)).each do |i|
97
- opts[i] = i.zero? ? take : take(opts[i - 1] + 1)
103
+ opts[i] = take(i.zero? ? @next : opts[i - 1] + 1)
98
104
  end
99
105
  rescue Errno::EADDRINUSE, SocketError
100
106
  next
@@ -106,6 +112,8 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
106
112
  opts
107
113
  end
108
114
  next if opts.nil?
115
+ @next = opts.max + 1
116
+ @next = 0 if @next > 65_535
109
117
  opts = opts[0] if total == 1
110
118
  return opts unless block_given?
111
119
  begin
@@ -129,6 +137,9 @@ for #{total} port(s), in #{format('%.02f', Time.now - start)}s"
129
137
 
130
138
  private
131
139
 
140
+ # Find one possible TCP port.
141
+ # @param [Integer] opt Suggested port number
142
+ # @return [Integer] Port found
132
143
  def take(opt = 0)
133
144
  server = TCPServer.new('127.0.0.1', opt)
134
145
  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.2'
31
31
  s.license = 'MIT'
32
32
  s.summary = 'Random TCP port'
33
33
  s.description = 'Reserves a random TCP port'
@@ -37,5 +37,6 @@ Gem::Specification.new do |s|
37
37
  s.files = `git ls-files`.split($RS)
38
38
  s.rdoc_options = ['--charset=UTF-8']
39
39
  s.extra_rdoc_files = ['README.md']
40
+ s.add_dependency 'tago', '>0'
40
41
  s.metadata['rubygems_mfa_required'] = 'true'
41
42
  end
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,15 +1,29 @@
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-10 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tago
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Reserves a random TCP port
14
28
  email: yegor256@gmail.com
15
29
  executables: []
@@ -45,7 +59,7 @@ licenses:
45
59
  - MIT
46
60
  metadata:
47
61
  rubygems_mfa_required: 'true'
48
- post_install_message:
62
+ post_install_message:
49
63
  rdoc_options:
50
64
  - "--charset=UTF-8"
51
65
  require_paths:
@@ -62,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
76
  version: '0'
63
77
  requirements: []
64
78
  rubygems_version: 3.4.10
65
- signing_key:
79
+ signing_key:
66
80
  specification_version: 4
67
81
  summary: Random TCP port
68
82
  test_files: []