Skip to content

Instantly share code, notes, and snippets.

@ggilder
Last active August 29, 2015 14:13
Show Gist options
  • Save ggilder/b531bdceea44a449c840 to your computer and use it in GitHub Desktop.
Save ggilder/b531bdceea44a449c840 to your computer and use it in GitHub Desktop.
Test connection_pool with Timeout interrupts
#!/usr/bin/env ruby
# Uncomment & fix the following line to test against a local checkout of connection_pool
# $LOAD_PATH.unshift '/Users/gabriel/oss/connection_pool/lib'
require 'redis'
require 'connection_pool'
require 'timeout'
pool = ConnectionPool.new(size: 5, timeout: 5) do
puts "Creating new connection"
Redis.new(host: 'localhost', port: 6379, db: 15)
end
TEST_KEY = 'connection_pool.test.chunky'
TEST_VAL = 'bacon'
pool.with { |conn| conn.set(TEST_KEY, TEST_VAL) }
count = 0
outer_count = 0
loop do
begin
Timeout.timeout(0.5) do
loop do
info = pool.with { |redis| redis.info }
unless info.is_a? Hash
puts "got incorrect info: #{info} (after #{count} operations and #{outer_count} timeouts)"
exit!(0)
end
test = pool.with { |redis| redis.get TEST_KEY }
if test != TEST_VAL
puts "got incorrect value: #{test.to_s[0..40].inspect} (after #{count} operations and #{outer_count} timeouts)"
exit!(0)
else
count += 1
end
end
end
rescue
ensure
outer_count += 1
puts "timeouts handled: #{outer_count}, operations handled: #{count}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment