Skip to content

Commit

Permalink
#11 it is
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Sep 13, 2019
1 parent ead2103 commit b9deeee
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AllCops:
DisplayCopNames: true
TargetRubyVersion: 2.3.3

Layout/EmptyLineAfterGuardClause:
Enabled: false
Style/ClassAndModuleChildren:
Enabled: false
Naming/FileName:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require 'rubygems'
require 'rake'
require 'rake/clean'

CLEAN = FileList['coverage']
CLEAN << 'coverage'

def name
@name ||= File.basename(Dir['*.gemspec'].first, '.*')
Expand Down
11 changes: 2 additions & 9 deletions lib/random-port.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,5 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# The module for all classes.
#
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module RandomPort
end

require 'random-port/pool'
require_relative 'random-port/module'
require_relative 'random-port/pool'
31 changes: 31 additions & 0 deletions lib/random-port/module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

# (The MIT License)
#
# Copyright (c) 2018 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# The module for all classes.
#
# Author:: Yegor Bugayenko (yegor256@gmail.com)
# Copyright:: Copyright (c) 2018 Yegor Bugayenko
# License:: MIT
module RandomPort
end
2 changes: 1 addition & 1 deletion lib/random-port/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

require 'socket'
require 'monitor'
require_relative '../random-port'
require_relative 'module'

# Pool of TPC ports.
#
Expand Down
1 change: 1 addition & 0 deletions random-port.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rdoc', '4.3.0'
s.add_development_dependency 'rubocop', '0.58.1'
s.add_development_dependency 'rubocop-rspec', '1.27.0'
s.add_development_dependency 'threads', '0.3.0'
end
13 changes: 13 additions & 0 deletions test/test_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
# SOFTWARE.

require 'minitest/autorun'
require 'threads'
require_relative '../lib/random-port/pool'

# Pool test.
Expand Down Expand Up @@ -78,6 +79,18 @@ def test_acquires_and_releases_in_block
assert_equal(123, result)
end

def test_acquires_and_releases_in_threads
pool = RandomPort::Pool.new
Threads.new(100).assert do
pool.acquire(5) do |ports|
ports.each do |p|
server = TCPServer.new(p)
server.close
end
end
end
end

def test_acquires_and_releases_safely
pool = RandomPort::Pool.new
assert_raises do
Expand Down

0 comments on commit b9deeee

Please sign in to comment.