Skip to content

Commit

Permalink
Redis#watch yields self to block (redis#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
royaltm committed Jun 6, 2012
1 parent 9e4c5a0 commit cb66c78
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/redis.rb
Expand Up @@ -1950,7 +1950,7 @@ def watch(*keys)

if block_given?
begin
yield
yield(self)
rescue ConnectionError
raise
rescue StandardError
Expand Down
33 changes: 32 additions & 1 deletion test/synchrony_driver.rb
@@ -1,20 +1,25 @@
# 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
r = Redis.new OPTIONS
r.flushdb

r.rpush "foo", "s1"
Expand Down Expand Up @@ -53,5 +58,31 @@
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
16 changes: 11 additions & 5 deletions test/transactions_test.rb
Expand Up @@ -188,8 +188,11 @@ def test_watch_with_a_modified_key_passed_as_array
end

def test_watch_with_a_block_and_an_unmodified_key
result = r.watch "foo" do
r.multi do |multi|
result = r.watch "foo" do |rd|

assert_same r, rd

rd.multi do |multi|
multi.set "foo", "s1"
end
end
Expand All @@ -199,9 +202,12 @@ def test_watch_with_a_block_and_an_unmodified_key
end

def test_watch_with_a_block_and_a_modified_key
result = r.watch "foo" do
r.set "foo", "s1"
r.multi do |multi|
result = r.watch "foo" do |rd|

assert_same r, rd

rd.set "foo", "s1"
rd.multi do |multi|
multi.set "foo", "s2"
end
end
Expand Down

0 comments on commit cb66c78

Please sign in to comment.