2014-03-13 21:56:16 -04:00
|
|
|
require_relative 'helper'
|
2014-02-06 21:05:45 -05:00
|
|
|
|
|
|
|
class TestConnectionPoolTimedStack < Minitest::Test
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@stack = ConnectionPool::TimedStack.new { Object.new }
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:10:29 -05:00
|
|
|
def test_empty_eh
|
2014-02-17 19:25:22 -05:00
|
|
|
stack = ConnectionPool::TimedStack.new(1) { Object.new }
|
2014-02-06 21:10:29 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
refute_empty stack
|
2014-02-06 21:10:29 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
popped = stack.pop
|
2014-02-06 21:10:29 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
assert_empty stack
|
|
|
|
|
|
|
|
stack.push popped
|
2014-02-17 17:45:57 -05:00
|
|
|
|
|
|
|
refute_empty stack
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:10:58 -05:00
|
|
|
def test_length
|
2014-02-17 19:25:22 -05:00
|
|
|
stack = ConnectionPool::TimedStack.new(1) { Object.new }
|
2014-02-06 21:10:58 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
assert_equal 1, stack.length
|
2014-02-06 21:10:58 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
popped = stack.pop
|
2014-02-06 21:10:58 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
assert_equal 0, stack.length
|
|
|
|
|
|
|
|
stack.push popped
|
2014-02-17 17:47:41 -05:00
|
|
|
|
|
|
|
assert_equal 1, stack.length
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:07:58 -05:00
|
|
|
def test_pop
|
2014-02-06 21:20:23 -05:00
|
|
|
object = Object.new
|
|
|
|
@stack.push object
|
|
|
|
|
|
|
|
popped = @stack.pop
|
|
|
|
|
|
|
|
assert_same object, popped
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pop_empty
|
2014-02-06 21:07:58 -05:00
|
|
|
e = assert_raises Timeout::Error do
|
2014-02-06 22:02:06 -05:00
|
|
|
@stack.pop 0
|
2014-02-06 21:07:58 -05:00
|
|
|
end
|
|
|
|
|
2014-02-06 22:02:06 -05:00
|
|
|
assert_equal 'Waited 0 sec', e.message
|
2014-02-06 21:07:58 -05:00
|
|
|
end
|
|
|
|
|
2014-02-17 17:34:51 -05:00
|
|
|
def test_pop_full
|
|
|
|
stack = ConnectionPool::TimedStack.new(1) { Object.new }
|
|
|
|
|
|
|
|
popped = stack.pop
|
|
|
|
|
|
|
|
refute_nil popped
|
|
|
|
assert_empty stack
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:47:23 -05:00
|
|
|
def test_pop_wait
|
|
|
|
thread = Thread.start do
|
|
|
|
@stack.pop
|
|
|
|
end
|
|
|
|
|
|
|
|
Thread.pass while thread.status == 'run'
|
|
|
|
|
|
|
|
object = Object.new
|
|
|
|
|
|
|
|
@stack.push object
|
|
|
|
|
|
|
|
assert_same object, thread.value
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:21:28 -05:00
|
|
|
def test_pop_shutdown
|
2014-02-06 22:06:16 -05:00
|
|
|
@stack.shutdown { }
|
2014-02-06 21:21:28 -05:00
|
|
|
|
|
|
|
assert_raises ConnectionPool::PoolShuttingDownError do
|
|
|
|
@stack.pop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:09:45 -05:00
|
|
|
def test_push
|
2014-02-17 19:25:22 -05:00
|
|
|
stack = ConnectionPool::TimedStack.new(1) { Object.new }
|
2014-02-06 21:09:45 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
conn = stack.pop
|
2014-02-06 21:09:45 -05:00
|
|
|
|
2014-02-17 19:25:22 -05:00
|
|
|
stack.push conn
|
|
|
|
|
|
|
|
refute_empty stack
|
2014-02-06 21:09:45 -05:00
|
|
|
end
|
|
|
|
|
2014-02-06 21:17:59 -05:00
|
|
|
def test_push_shutdown
|
2014-02-06 21:16:25 -05:00
|
|
|
called = []
|
|
|
|
|
|
|
|
@stack.shutdown do |object|
|
|
|
|
called << object
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:17:59 -05:00
|
|
|
@stack.push Object.new
|
|
|
|
|
2014-02-06 21:16:25 -05:00
|
|
|
refute_empty called
|
|
|
|
assert_empty @stack
|
2014-02-06 21:17:59 -05:00
|
|
|
end
|
2014-02-06 21:16:25 -05:00
|
|
|
|
2014-02-06 21:17:59 -05:00
|
|
|
def test_shutdown
|
2014-02-06 21:16:25 -05:00
|
|
|
@stack.push Object.new
|
|
|
|
|
2014-02-06 21:17:59 -05:00
|
|
|
called = []
|
|
|
|
|
|
|
|
@stack.shutdown do |object|
|
|
|
|
called << object
|
|
|
|
end
|
|
|
|
|
|
|
|
refute_empty called
|
2014-02-06 21:16:25 -05:00
|
|
|
assert_empty @stack
|
|
|
|
end
|
|
|
|
|
2014-02-06 21:05:45 -05:00
|
|
|
end
|
|
|
|
|