Skip to content

Commit

Permalink
Update #multi documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Nov 26, 2020
1 parent 7587668 commit 4f9bc0e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 70 deletions.
32 changes: 7 additions & 25 deletions lib/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2451,46 +2451,28 @@ def pipelined

# Mark the start of a transaction block.
#
# Passing a block is optional.
#
# @example With a block
# @example
# redis.multi do |multi|
# multi.set("key", "value")
# multi.incr("counter")
# end # => ["OK", 6]
#
# @example Without a block
# redis.multi
# # => "OK"
# redis.set("key", "value")
# # => "QUEUED"
# redis.incr("counter")
# # => "QUEUED"
# redis.exec
# # => ["OK", 6]
#
# @yield [multi] the commands that are called inside this block are cached
# and written to the server upon returning from it
# @yieldparam [Redis] multi `self`
#
# @return [String, Array<...>]
# - when a block is not given, `OK`
# - when a block is given, an array with replies
#
# @see #watch
# @see #unwatch
def multi
synchronize do |prior_client|
if !block_given?
prior_client.call([:multi])
else
begin
@client = Pipeline::Multi.new(prior_client)
yield(self)
prior_client.call_pipeline(@client)
ensure
@client = prior_client
end
begin
@client = Pipeline::Multi.new(prior_client)
yield(self)
prior_client.call_pipeline(@client)
ensure
@client = prior_client
end
end
end
Expand Down
34 changes: 0 additions & 34 deletions test/distributed_transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
class TestDistributedTransactions < Minitest::Test
include Helper::Distributed

def test_multi_discard
r.set("foo", 1)

r.watch("foo")
r.multi
r.set("foo", 2)

assert_raises Redis::Distributed::CannotDistribute do
r.set("bar", 1)
end

r.discard

assert_equal('1', r.get("foo"))
end

def test_multi_discard_without_watch
@foo = nil

Expand Down Expand Up @@ -83,22 +67,4 @@ def test_watch_multi_with_block
assert_equal [3, 6, 10], result
end
end

def test_watch_multi_exec_without_block
r.set("{qux}baz", 1)

assert_equal "OK", r.watch("{qux}foo", "{qux}bar", "{qux}baz")
assert_equal '1', r.get("{qux}baz")

assert_raises Redis::Distributed::CannotDistribute do
r.get("{foo}baz")
end

assert_equal "OK", r.multi
assert_equal "QUEUED", r.incrby("{qux}baz", 1)
assert_equal "QUEUED", r.incrby("{qux}baz", 1)
assert_equal [2, 3], r.exec

assert_equal "OK", r.set("{other}baz", 1)
end
end
11 changes: 0 additions & 11 deletions test/transactions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
class TestTransactions < Minitest::Test
include Helper::Client

def test_multi_discard
r.multi

assert_equal "QUEUED", r.set("foo", "1")
assert_equal "QUEUED", r.get("foo")

r.discard

assert_nil r.get("foo")
end

def test_multi_exec_with_a_block
r.multi do |multi|
multi.set "foo", "s1"
Expand Down

0 comments on commit 4f9bc0e

Please sign in to comment.