test/client_test.rb in suo-0.1.3 vs test/client_test.rb in suo-0.2.0
- old
+ new
@@ -1,157 +1,173 @@
require "test_helper"
TEST_KEY = "suo_test_key".freeze
module ClientTests
- def test_requires_client
- exception = assert_raises(RuntimeError) do
- @klass.lock(TEST_KEY, 1)
- end
-
- assert_equal "Client required", exception.message
- end
-
def test_throws_failed_error_on_bad_client
assert_raises(Suo::LockClientError) do
- @klass.lock(TEST_KEY, 1, client: {})
+ client = @client.class.new(client: {})
+ client.lock(TEST_KEY, 1)
end
end
def test_class_single_resource_locking
- lock1 = @klass.lock(TEST_KEY, 1, client: @klass_client)
+ lock1 = @client.lock(TEST_KEY, 1)
refute_nil lock1
- locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 1)
assert_equal true, locked
- lock2 = @klass.lock(TEST_KEY, 1, client: @klass_client)
+ lock2 = @client.lock(TEST_KEY, 1)
assert_nil lock2
- @klass.unlock(TEST_KEY, lock1, client: @klass_client)
+ @client.unlock(TEST_KEY, lock1)
- locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 1)
assert_equal false, locked
end
def test_class_multiple_resource_locking
- lock1 = @klass.lock(TEST_KEY, 2, client: @klass_client)
+ lock1 = @client.lock(TEST_KEY, 2)
refute_nil lock1
- locked = @klass.locked?(TEST_KEY, 2, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 2)
assert_equal false, locked
- lock2 = @klass.lock(TEST_KEY, 2, client: @klass_client)
+ lock2 = @client.lock(TEST_KEY, 2)
refute_nil lock2
- locked = @klass.locked?(TEST_KEY, 2, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 2)
assert_equal true, locked
- @klass.unlock(TEST_KEY, lock1, client: @klass_client)
+ @client.unlock(TEST_KEY, lock1)
- locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 1)
assert_equal true, locked
- @klass.unlock(TEST_KEY, lock2, client: @klass_client)
+ @client.unlock(TEST_KEY, lock2)
- locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 1)
assert_equal false, locked
end
- def test_instance_single_resource_locking
+ def test_block_single_resource_locking
locked = false
@client.lock(TEST_KEY, 1) { locked = true }
assert_equal true, locked
end
- def test_instance_unlocks_on_exception
+ def test_block_unlocks_on_exception
assert_raises(RuntimeError) do
@client.lock(TEST_KEY, 1) { fail "Test" }
end
- locked = @klass.locked?(TEST_KEY, 1, client: @klass_client)
+ locked = @client.locked?(TEST_KEY, 1)
assert_equal false, locked
end
+ def test_readme_example
+ output = Queue.new
+ threads = []
+
+ threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "One"; sleep 2 } }
+ threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Two"; sleep 2 } }
+ threads << Thread.new { @client.lock(TEST_KEY, 2) { output << "Three" } }
+
+ threads.map(&:join)
+
+ ret = []
+
+ ret << output.pop
+ ret << output.pop
+
+ ret.sort!
+
+ assert_equal 0, output.size
+ assert_equal ["One", "Two"], ret
+ end
+
def test_instance_multiple_resource_locking
success_counter = Queue.new
failure_counter = Queue.new
- 50.times.map do |i|
+ client = @client.class.new(acquisition_timeout: 0.9, client: @client.client)
+
+ 100.times.map do |i|
Thread.new do
- success = @client.lock(TEST_KEY, 25, retry_timeout: 0.9) do
+ success = @client.lock(TEST_KEY, 50) do
sleep(3)
success_counter << i
end
failure_counter << i unless success
end
end.map(&:join)
- assert_equal 25, success_counter.size
- assert_equal 25, failure_counter.size
+ assert_equal 50, success_counter.size
+ assert_equal 50, failure_counter.size
end
def test_instance_multiple_resource_locking_longer_timeout
success_counter = Queue.new
failure_counter = Queue.new
- 50.times.map do |i|
+ client = @client.class.new(acquisition_timeout: 3, client: @client.client)
+
+ 100.times.map do |i|
Thread.new do
- success = @client.lock(TEST_KEY, 25, retry_timeout: 2) do
+ success = client.lock(TEST_KEY, 50) do
sleep(0.5)
success_counter << i
end
failure_counter << i unless success
end
end.map(&:join)
- assert_equal 50, success_counter.size
+ assert_equal 100, success_counter.size
assert_equal 0, failure_counter.size
end
end
class TestBaseClient < Minitest::Test
def setup
- @klass = Suo::Client::Base
+ @client = Suo::Client::Base.new(client: {})
end
def test_not_implemented
assert_raises(NotImplementedError) do
- @klass.send(:get, TEST_KEY, {})
+ @client.send(:get, TEST_KEY)
end
end
end
class TestMemcachedClient < Minitest::Test
include ClientTests
def setup
- @klass = Suo::Client::Memcached
- @client = @klass.new
- @klass_client = Dalli::Client.new("127.0.0.1:11211")
+ @dalli = Dalli::Client.new("127.0.0.1:11211")
+ @client = Suo::Client::Memcached.new
end
def teardown
- @klass_client.delete(TEST_KEY)
+ @dalli.delete(TEST_KEY)
end
end
class TestRedisClient < Minitest::Test
include ClientTests
def setup
- @klass = Suo::Client::Redis
- @client = @klass.new
- @klass_client = Redis.new
+ @redis = Redis.new
+ @client = Suo::Client::Redis.new
end
def teardown
- @klass_client.del(TEST_KEY)
+ @redis.del(TEST_KEY)
end
end
class TestLibrary < Minitest::Test
def test_that_it_has_a_version_number