random-port 0.8.1 → 0.8.3

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: 2c6250d7a7a9a68ca737b299b7cef6b4816e35a6fe9452b78633c2df1132bd41
4
- data.tar.gz: 9ec96d9ae9ce5e2f58c4bd3821f24877cfbc7a88913005d21a32c90d8f465b77
3
+ metadata.gz: e50a54cd0f5cc4a719500af4b3b8184f3ed16613c6b34c2680fa27a373907b43
4
+ data.tar.gz: d4fa618c58dda21879ebda7344153ac9b92f63434ec7b9a380063a2d908120f4
5
5
  SHA512:
6
- metadata.gz: da1f7e7b9daeaf7a60fd935cbc8b0c578a352c558683a85f7c6d66c4a7ef391586a02b389107516c270d2a38215979aa8f4f6cdba5b70f7f128515cc21058a8c
7
- data.tar.gz: bf6db1f8503993e5388b4cb48909f2684a0618eb26e4f5bc5677fe92b248c67a22e11f9ad8e9f5405223718d7f7287dc51ea50ffe47def18b6b8561ea3dab582
6
+ metadata.gz: 7860cbb767a8b2d8646addd5c33a2881fea122a3b1ebb8e1729b22afbbcc5513de4b2e60e0e6e3744d08685d0798f41af3a0fed2bf61521ba75e788b66a7dd4d
7
+ data.tar.gz: b55047366c5854d1e19eb9c2009c09a6bcff868e0e05b6228768c5cf959b9c144842f42e12b08a1bed719c721bf698c6f674d36429e65b99ead9b3417093d079
data/Gemfile CHANGED
@@ -13,11 +13,12 @@ gem 'os', '~> 1.1', require: false
13
13
  gem 'qbash', '~>0.2', require: false
14
14
  gem 'rake', '~>13.2', require: false
15
15
  gem 'rubocop', '~>1.75', require: false
16
+ gem 'rubocop-elegant', '~>0.5', require: false
16
17
  gem 'rubocop-minitest', '~>0.38', require: false
17
18
  gem 'rubocop-performance', '~>1.25', require: false
18
19
  gem 'rubocop-rake', '~>0.7', require: false
19
20
  gem 'rubocop-rspec', '~> 3.7', require: false
20
- gem 'simplecov', '~>0.22', require: false
21
- gem 'simplecov-cobertura', '~>3.0', require: false
21
+ gem 'simplecov', '~>1.0', require: false
22
+ gem 'simplecov-cobertura', '~>4.0', require: false
22
23
  gem 'threads', '~>0.4', require: false
23
24
  gem 'yard', '~>0.9', require: false
data/Gemfile.lock CHANGED
@@ -7,93 +7,95 @@ PATH
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- ansi (1.5.0)
10
+ ansi (1.6.0)
11
11
  ast (2.4.3)
12
12
  backtrace (0.4.1)
13
13
  builder (3.3.0)
14
- concurrent-ruby (1.3.6)
15
- docile (1.4.1)
14
+ concurrent-ruby (1.3.8)
15
+ drb (2.2.3)
16
16
  elapsed (0.3.1)
17
17
  loog (~> 0.6)
18
18
  tago (~> 0.1)
19
19
  ellipsized (0.3.0)
20
- json (2.18.1)
21
- language_server-protocol (3.17.0.5)
20
+ json (3.2.0)
21
+ language_server-protocol (3.17.0.6)
22
22
  lint_roller (1.1.0)
23
23
  logger (1.7.0)
24
24
  loog (0.8.0)
25
25
  ellipsized
26
26
  logger (~> 1.0)
27
- minitest (6.0.1)
28
- prism (~> 1.5)
27
+ minitest (6.1.0)
28
+ drb (~> 2.0)
29
+ prism (>= 1.5, < 3)
29
30
  minitest-mock (5.27.0)
30
- minitest-reporters (1.7.1)
31
+ minitest-reporters (1.8.0)
31
32
  ansi
32
33
  builder
33
- minitest (>= 5.0)
34
+ minitest (>= 5.0, < 7)
34
35
  ruby-progressbar
35
36
  os (1.1.4)
36
- parallel (1.27.0)
37
- parser (3.3.10.1)
37
+ parallel (2.3.0)
38
+ parser (3.3.14.0)
38
39
  ast (~> 2.4.1)
39
40
  racc
40
41
  prism (1.9.0)
41
- qbash (0.7.2)
42
+ qbash (0.8.4)
42
43
  backtrace (> 0)
43
44
  elapsed (> 0)
44
45
  loog (> 0)
45
46
  tago (> 0)
46
47
  racc (1.8.1)
47
48
  rainbow (3.1.1)
48
- rake (13.3.1)
49
- regexp_parser (2.11.3)
49
+ rake (13.4.2)
50
+ regexp_parser (2.13.4)
50
51
  rexml (3.4.4)
51
- rubocop (1.84.1)
52
- json (~> 2.3)
52
+ rubocop (1.93.0)
53
+ json (>= 2.3)
53
54
  language_server-protocol (~> 3.17.0.2)
54
55
  lint_roller (~> 1.1.0)
55
- parallel (~> 1.10)
56
+ parallel (>= 1.10)
56
57
  parser (>= 3.3.0.2)
57
58
  rainbow (>= 2.2.2, < 4.0)
58
- regexp_parser (>= 2.9.3, < 3.0)
59
- rubocop-ast (>= 1.49.0, < 2.0)
59
+ regexp_parser (>= 2.11.3, < 3.0)
60
+ rubocop-ast (>= 1.54.0, < 2.0)
60
61
  ruby-progressbar (~> 1.7)
61
62
  unicode-display_width (>= 2.4.0, < 4.0)
62
- rubocop-ast (1.49.0)
63
+ rubocop-ast (1.54.0)
64
+ ast (~> 2.4)
63
65
  parser (>= 3.3.7.2)
64
- prism (~> 1.7)
65
- rubocop-minitest (0.38.2)
66
+ prism (>= 1.7)
67
+ racc
68
+ rubocop-elegant (0.8.0)
69
+ lint_roller (~> 1.1)
70
+ rubocop (~> 1.75)
71
+ rubocop-minitest (0.41.0)
66
72
  lint_roller (~> 1.1)
67
- rubocop (>= 1.75.0, < 2.0)
73
+ rubocop (>= 1.89.0, < 2.0)
68
74
  rubocop-ast (>= 1.38.0, < 2.0)
69
- rubocop-performance (1.26.1)
75
+ rubocop-performance (1.27.0)
70
76
  lint_roller (~> 1.1)
71
- rubocop (>= 1.75.0, < 2.0)
77
+ rubocop (>= 1.89.0, < 2.0)
72
78
  rubocop-ast (>= 1.47.1, < 2.0)
73
79
  rubocop-rake (0.7.1)
74
80
  lint_roller (~> 1.1)
75
81
  rubocop (>= 1.72.1)
76
- rubocop-rspec (3.9.0)
82
+ rubocop-rspec (3.10.2)
77
83
  lint_roller (~> 1.1)
78
- rubocop (~> 1.81)
84
+ regexp_parser (>= 2.0)
85
+ rubocop (~> 1.86, >= 1.86.2)
79
86
  ruby-progressbar (1.13.0)
80
- simplecov (0.22.0)
81
- docile (~> 1.1)
82
- simplecov-html (~> 0.11)
83
- simplecov_json_formatter (~> 0.1)
84
- simplecov-cobertura (3.1.0)
87
+ simplecov (1.3.2)
88
+ simplecov-cobertura (4.0.0)
85
89
  rexml
86
- simplecov (~> 0.19)
87
- simplecov-html (0.13.2)
88
- simplecov_json_formatter (0.1.4)
90
+ simplecov (~> 1.0)
89
91
  tago (0.7.0)
90
92
  threads (0.5.0)
91
93
  backtrace (~> 0)
92
94
  concurrent-ruby (~> 1.0)
93
- unicode-display_width (3.2.0)
94
- unicode-emoji (~> 4.1)
95
- unicode-emoji (4.2.0)
96
- yard (0.9.38)
95
+ unicode-display_width (3.3.0)
96
+ unicode-emoji (~> 4.3)
97
+ unicode-emoji (4.3.0)
98
+ yard (0.9.45)
97
99
 
98
100
  PLATFORMS
99
101
  arm64-darwin-22
@@ -111,12 +113,13 @@ DEPENDENCIES
111
113
  rake (~> 13.2)
112
114
  random-port!
113
115
  rubocop (~> 1.75)
116
+ rubocop-elegant (~> 0.5)
114
117
  rubocop-minitest (~> 0.38)
115
118
  rubocop-performance (~> 1.25)
116
119
  rubocop-rake (~> 0.7)
117
120
  rubocop-rspec (~> 3.7)
118
- simplecov (~> 0.22)
119
- simplecov-cobertura (~> 3.0)
121
+ simplecov (~> 1.0)
122
+ simplecov-cobertura (~> 4.0)
120
123
  threads (~> 0.4)
121
124
  yard (~> 0.9)
122
125
 
data/README.md CHANGED
@@ -25,8 +25,8 @@ require 'random-port'
25
25
  port = RandomPort::Pool.new.acquire
26
26
  ```
27
27
 
28
- The `Pool` guarantees that the port won't be used again. You can put
29
- the port back to the pool after usage:
28
+ The `Pool` guarantees that the port isn't used again.
29
+ You can put the port back to the pool after usage:
30
30
 
31
31
  ```ruby
32
32
  RandomPort::Pool.new.acquire do |port|
@@ -51,8 +51,8 @@ RandomPort::Pool::SINGLETON.acquire do |port|
51
51
  end
52
52
  ```
53
53
 
54
- Or use shortened version, all methods called in `RandomPort`
55
- will be delegated to `Pool::SINGLETON`:
54
+ Or use the shortened version; all methods called on `RandomPort`
55
+ are delegated to `Pool::SINGLETON`:
56
56
 
57
57
  ```ruby
58
58
  RandomPort.acquire do |port|
@@ -61,17 +61,15 @@ end
61
61
  ```
62
62
 
63
63
  The pool is thread-safe by default.
64
- You can configure it to be
65
- not-thread-safe, using optional `sync` argument of the constructor.
64
+ You can configure it to be non-thread-safe,
65
+ using the optional `sync` argument of the constructor.
66
66
 
67
67
  ## How to contribute
68
68
 
69
- Read
70
- [these guidelines](https://www.yegor256.com/2014/04/15/github-guidelines.html).
71
- Make sure your build is green before you contribute
72
- your pull request. You will need to have
73
- [Ruby](https://www.ruby-lang.org/en/) 3.3+ and
74
- [Bundler](https://bundler.io/) installed. Then:
69
+ Read these [guidelines].
70
+ Make sure your build is green before you contribute your pull request.
71
+ You need to have [Ruby] 3.3+ and [Bundler] installed.
72
+ Then:
75
73
 
76
74
  ```bash
77
75
  bundle update
@@ -79,3 +77,7 @@ bundle exec rake
79
77
  ```
80
78
 
81
79
  If it's clean and you don't see any error messages, submit your pull request.
80
+
81
+ [guidelines]: https://www.yegor256.com/2014/04/15/github-guidelines.html
82
+ [Ruby]: https://www.ruby-lang.org/en/
83
+ [Bundler]: https://bundler.io/
data/REUSE.toml CHANGED
@@ -5,8 +5,6 @@ version = 1
5
5
  [[annotations]]
6
6
  path = [
7
7
  ".DS_Store",
8
- ".gitattributes",
9
- ".gitignore",
10
8
  ".pdd",
11
9
  "**.json",
12
10
  "**.md",
data/Rakefile CHANGED
@@ -5,9 +5,9 @@
5
5
 
6
6
  require 'os'
7
7
  require 'qbash'
8
- require 'rubygems'
9
8
  require 'rake'
10
9
  require 'rake/clean'
10
+ require 'rubygems'
11
11
  require 'shellwords'
12
12
 
13
13
  CLEAN << 'coverage'
@@ -3,7 +3,6 @@
3
3
  # SPDX-FileCopyrightText: Copyright (c) 2018-2026 Yegor Bugayenko
4
4
  # SPDX-License-Identifier: MIT
5
5
 
6
- require 'minitest/mock'
7
6
  require 'monitor'
8
7
  require 'socket'
9
8
  require 'tago'
@@ -32,7 +31,6 @@ class RandomPort::Pool
32
31
  # Raised when a port cannot be acquired within the timeout period.
33
32
  class Timeout < StandardError; end
34
33
 
35
- # @return [Integer] The maximum number of ports that can be acquired from this pool
36
34
  attr_reader :limit
37
35
 
38
36
  # Constructor.
@@ -44,10 +42,10 @@ class RandomPort::Pool
44
42
  @sync = sync
45
43
  @monitor = Monitor.new
46
44
  @limit = limit
45
+ @start = start
47
46
  @next = start
48
47
  end
49
48
 
50
- # Application-wide singleton pool of ports.
51
49
  SINGLETON = RandomPort::Pool.new
52
50
 
53
51
  # Returns the number of ports currently acquired from the pool.
@@ -75,15 +73,23 @@ class RandomPort::Pool
75
73
  # @return [Integer|Array<Integer>] The acquired port(s) if no block given
76
74
  # @raise [Timeout] If ports cannot be acquired within the timeout period
77
75
  def acquire(total = 1, timeout: 4)
76
+ unless total.is_a?(Integer) && total.positive?
77
+ raise(
78
+ ArgumentError,
79
+ 'The number of ports to acquire must be a positive integer, ' \
80
+ "but #{total.inspect} was provided"
81
+ )
82
+ end
78
83
  start = Time.now
79
84
  attempt = 0
80
85
  loop do
81
86
  if Time.now > start + timeout
82
- raise \
87
+ raise(
83
88
  Timeout,
84
89
  "Can't find a place in the pool of #{@limit} ports " \
85
90
  "(#{@ports.size} already occupied) " \
86
91
  "for #{total} port(s), after #{attempt} attempts in #{start.ago}"
92
+ )
87
93
  end
88
94
  attempt += 1
89
95
  opts = safe { group(total) }
@@ -92,12 +98,12 @@ class RandomPort::Pool
92
98
  else
93
99
  @next = opts.max + 1
94
100
  end
95
- @next = 0 if @next > 65_535
101
+ @next = @start if @next > 65_535
96
102
  next if opts.nil?
97
103
  opts = opts[0] if total == 1
98
104
  return opts unless block_given?
99
105
  begin
100
- return yield opts
106
+ return yield(opts)
101
107
  ensure
102
108
  release(opts)
103
109
  end
@@ -123,19 +129,17 @@ class RandomPort::Pool
123
129
  # @param [Integer] total The number of ports to acquire
124
130
  # @return [Array<Integer>|nil] An array of port numbers if successful, nil otherwise
125
131
  def group(total)
126
- return nil if @ports.count + total > @limit
132
+ return if @ports.count + total > @limit
127
133
  opts = Array.new(total, 0)
128
134
  begin
129
135
  (0..(total - 1)).each do |i|
130
- port = i.zero? ? @next : opts[i - 1] + 1
131
- opts[i] = take(port)
136
+ opts[i] = take((i.zero? ? @next : opts[i - 1] + 1))
132
137
  end
133
138
  rescue Errno::EADDRINUSE, SocketError
134
- return nil
139
+ return
135
140
  end
136
- return nil if opts.any? { |p| @ports.include?(p) }
137
- d = total * (total - 1) / 2
138
- return nil unless opts.sum - (total * opts.min) == d
141
+ return if opts.intersect?(@ports)
142
+ return unless opts.sum - (total * opts.min) == (total * (total - 1) / 2)
139
143
  @ports += opts
140
144
  opts
141
145
  end
data/random-port.gemspec CHANGED
@@ -5,10 +5,10 @@
5
5
 
6
6
  require 'English'
7
7
  Gem::Specification.new do |s|
8
- s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
8
+ s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to?(:required_rubygems_version=)
9
9
  s.required_ruby_version = '>=3.3'
10
10
  s.name = 'random-port'
11
- s.version = '0.8.1'
11
+ s.version = '0.8.3'
12
12
  s.license = 'MIT'
13
13
  s.summary = 'Random TCP port'
14
14
  s.description = 'Reserves a random TCP port'
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.files = `git ls-files | grep -v -E '^(test/|\\.|renovate)'`.split($RS)
19
19
  s.rdoc_options = ['--charset=UTF-8']
20
20
  s.extra_rdoc_files = ['README.md']
21
- s.add_dependency 'tago', '~>0.0'
21
+ s.add_dependency('tago', '~>0.0')
22
22
  s.metadata['rubygems_mfa_required'] = 'true'
23
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random-port
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko