1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
# encoding: UTF-8
require 'em-synchrony'
require 'em-synchrony/connection_pool'
require 'redis'
require 'redis/connection/synchrony'
require File.expand_path("./helper", File.dirname(__FILE__))
PORT = 6381
OPTIONS = {:port => PORT, :db => 15}
#
# if running under Eventmachine + Synchrony (Ruby 1.9+), then
# we can simulate the blocking API while performing the network
# IO via the EM reactor.
#
EM.synchrony do
r = Redis.new OPTIONS
r.flushdb
r.rpush "foo", "s1"
r.rpush "foo", "s2"
assert_equal 2, r.llen("foo")
assert_equal "s2", r.rpop("foo")
r.set("foo", "bar")
assert_equal "bar", r.getset("foo", "baz")
assert_equal "baz", r.get("foo")
r.set("foo", "a")
assert_equal 1, r.getbit("foo", 1)
assert_equal 1, r.getbit("foo", 2)
assert_equal 0, r.getbit("foo", 3)
assert_equal 0, r.getbit("foo", 4)
assert_equal 0, r.getbit("foo", 5)
assert_equal 0, r.getbit("foo", 6)
assert_equal 1, r.getbit("foo", 7)
r.flushdb
# command pipelining
r.pipelined do
r.lpush "foo", "s1"
r.lpush "foo", "s2"
end
assert_equal 2, r.llen("foo")
assert_equal "s2", r.lpop("foo")
assert_equal "s1", r.lpop("foo")
assert_equal "OK", r.client.call(:quit)
assert_equal "PONG", r.ping
rpool = EM::Synchrony::ConnectionPool.new(size: 5) { Redis.new OPTIONS }
result = rpool.watch 'foo' do |rd|
assert_kind_of Redis, rd
rd.set "foo", "s1"
rd.multi do |multi|
multi.set "foo", "s2"
end
end
assert_equal nil, result
assert_equal "s1", rpool.get("foo")
result = rpool.watch "foo" do |rd|
assert_kind_of Redis, rd
rd.multi do |multi|
multi.set "foo", "s3"
end
end
assert_equal ["OK"], result
assert_equal "s3", rpool.get("foo")
EM.stop
end
|