1
0
Fork 0
mirror of https://github.com/mperham/connection_pool synced 2023-03-27 23:22:21 -04:00

Parameterize

This commit is contained in:
Tamir Duberstein 2015-04-16 18:16:34 -07:00
parent 6007d32b62
commit 6ca937432d

View file

@ -3,13 +3,15 @@ require_relative 'helper'
class TestConnectionPool < Minitest::Test class TestConnectionPool < Minitest::Test
class NetworkConnection class NetworkConnection
SLEEP_TIME = 0.05
def initialize def initialize
@x = 0 @x = 0
end end
def do_something def do_something
@x += 1 @x += 1
sleep 0.05 sleep SLEEP_TIME
@x @x
end end
@ -19,7 +21,7 @@ class TestConnectionPool < Minitest::Test
def do_something_with_block def do_something_with_block
@x += yield @x += yield
sleep 0.05 sleep SLEEP_TIME
@x @x
end end
@ -58,21 +60,26 @@ class TestConnectionPool < Minitest::Test
end end
def test_basic_multithreaded_usage def test_basic_multithreaded_usage
pool = ConnectionPool.new(:size => 5) { NetworkConnection.new } pool_size = 5
pool = ConnectionPool.new(:size => pool_size) { NetworkConnection.new }
threads = Array.new(15) do start = Time.new
generations = 3
result = Array.new(pool_size * generations) do
Thread.new do Thread.new do
pool.with do |net| pool.with do |net|
net.do_something net.do_something
end end
end end
end end.map(&:value)
a = Time.now finish = Time.new
result = threads.map(&:value)
b = Time.now assert_equal((1..generations).cycle(pool_size).sort, result.sort)
assert_operator((b - a), :>, 0.125)
assert_equal([1,2,3].cycle(5).sort, result.sort) assert_operator(finish - start, :>, generations * NetworkConnection::SLEEP_TIME)
end end
def test_timeout def test_timeout
@ -179,7 +186,7 @@ class TestConnectionPool < Minitest::Test
pool.with { |net| net.do_something } pool.with { |net| net.do_something }
end end
pool.with(:timeout => 0.1) do |conn| pool.with(:timeout => 2 * NetworkConnection::SLEEP_TIME) do |conn|
refute_nil conn refute_nil conn
end end
end end
@ -287,11 +294,11 @@ class TestConnectionPool < Minitest::Test
pool.checkout pool.checkout
end end
assert pool.checkout :timeout => 0.1 assert pool.checkout :timeout => 2 * NetworkConnection::SLEEP_TIME
end end
def test_passthru def test_passthru
pool = ConnectionPool.wrap(:timeout => 0.1, :size => 1) { NetworkConnection.new } pool = ConnectionPool.wrap(:timeout => 2 * NetworkConnection::SLEEP_TIME, :size => 1) { NetworkConnection.new }
assert_equal 1, pool.do_something assert_equal 1, pool.do_something
assert_equal 2, pool.do_something assert_equal 2, pool.do_something
assert_equal 5, pool.do_something_with_block { 3 } assert_equal 5, pool.do_something_with_block { 3 }
@ -299,7 +306,7 @@ class TestConnectionPool < Minitest::Test
end end
def test_passthru_respond_to def test_passthru_respond_to
pool = ConnectionPool.wrap(:timeout => 0.1, :size => 1) { NetworkConnection.new } pool = ConnectionPool.wrap(:timeout => 2 * NetworkConnection::SLEEP_TIME, :size => 1) { NetworkConnection.new }
assert pool.respond_to?(:with) assert pool.respond_to?(:with)
assert pool.respond_to?(:do_something) assert pool.respond_to?(:do_something)
assert pool.respond_to?(:do_magic) assert pool.respond_to?(:do_magic)
@ -307,7 +314,7 @@ class TestConnectionPool < Minitest::Test
end end
def test_return_value def test_return_value
pool = ConnectionPool.new(:timeout => 0.1, :size => 1) { NetworkConnection.new } pool = ConnectionPool.new(:timeout => 2 * NetworkConnection::SLEEP_TIME, :size => 1) { NetworkConnection.new }
result = pool.with do |net| result = pool.with do |net|
net.fast net.fast
end end