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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
# frozen_string_literal: true
require_relative "helper"
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"
end
assert_equal "s1", r.get("foo")
end
def test_multi_exec_with_a_block_doesn_t_return_replies_for_multi_and_exec
r1, r2, nothing_else = r.multi do |multi|
multi.set "foo", "s1"
multi.get "foo"
end
assert_equal "OK", r1
assert_equal "s1", r2
assert_nil nothing_else
end
def test_assignment_inside_multi_exec_block
r.multi do |m|
@first = m.sadd("foo", 1)
@second = m.sadd("foo", 1)
end
assert_equal true, @first.value
assert_equal false, @second.value
end
# Although we could support accessing the values in these futures,
# it doesn't make a lot of sense.
def test_assignment_inside_multi_exec_block_with_delayed_command_errors
assert_raises(Redis::CommandError) do
r.multi do |m|
@first = m.set("foo", "s1")
@second = m.incr("foo") # not an integer
@third = m.lpush("foo", "value") # wrong kind of value
end
end
assert_equal "OK", @first.value
assert_raises(Redis::CommandError) { @second.value }
assert_raises(Redis::FutureNotReady) { @third.value }
end
def test_assignment_inside_multi_exec_block_with_immediate_command_errors
assert_raises(Redis::CommandError) do
r.multi do |m|
m.doesnt_exist
@first = m.sadd("foo", 1)
@second = m.sadd("foo", 1)
end
end
assert_raises(Redis::FutureNotReady) { @first.value }
assert_raises(Redis::FutureNotReady) { @second.value }
end
def test_raise_immediate_errors_in_multi_exec
assert_raises(RuntimeError) do
r.multi do |multi|
multi.set "bar", "s2"
raise "Some error"
end
end
assert_nil r.get("bar")
assert_nil r.get("baz")
end
def test_transformed_replies_as_return_values_for_multi_exec_block
info, = r.multi do |_m|
r.info
end
assert info.is_a?(Hash)
end
def test_transformed_replies_inside_multi_exec_block
r.multi do |_m|
@info = r.info
end
assert @info.value.is_a?(Hash)
end
def test_raise_command_errors_when_reply_is_not_transformed
assert_raises(Redis::CommandError) do
r.multi do |m|
m.set("foo", "s1")
m.incr("foo") # not an integer
m.lpush("foo", "value") # wrong kind of value
end
end
assert_equal "s1", r.get("foo")
end
def test_empty_multi_exec
result = nil
redis_mock(exec: ->(*_) { "-ERROR" }) do |redis|
result = redis.multi {}
end
assert_equal [], result
end
def test_raise_command_errors_when_reply_is_transformed_from_int_to_boolean
assert_raises(Redis::CommandError) do
r.multi do |m|
m.set("foo", 1)
m.sadd("foo", 2)
end
end
end
def test_raise_command_errors_when_reply_is_transformed_from_ok_to_boolean
assert_raises(Redis::CommandError) do
r.multi do |m|
m.set("foo", 1, ex: 0, nx: true)
end
end
end
def test_raise_command_errors_when_reply_is_transformed_to_float
assert_raises(Redis::CommandError) do
r.multi do |m|
m.set("foo", 1)
m.zscore("foo", "b")
end
end
end
def test_raise_command_errors_when_reply_is_transformed_to_floats
assert_raises(Redis::CommandError) do
r.multi do |m|
m.zrange("a", "b", 5, with_scores: true)
end
end
end
def test_raise_command_errors_when_reply_is_transformed_to_hash
assert_raises(Redis::CommandError) do
r.multi do |m|
m.set("foo", 1)
m.hgetall("foo")
end
end
end
def test_raise_command_errors_when_accessing_futures_after_multi_exec
begin
r.multi do |m|
m.set("foo", "s1")
@counter = m.incr("foo") # not an integer
end
rescue Exception
# Not gonna deal with it
end
# We should test for Redis::Error here, but hiredis doesn't yet do
# custom error classes.
err = nil
begin
@counter.value
rescue => err
end
assert err.is_a?(RuntimeError)
end
def test_multi_with_a_block_yielding_the_client
r.multi do |multi|
multi.set "foo", "s1"
end
assert_equal "s1", r.get("foo")
end
def test_multi_with_interrupt_preserves_client
original = r._client
Redis::Pipeline.stubs(:new).raises(Interrupt)
assert_raises(Interrupt) { r.multi {} }
assert_equal r._client, original
end
def test_raise_command_error_when_exec_fails
redis_mock(exec: ->(*_) { "-ERROR" }) do |redis|
assert_raises(Redis::CommandError) do
redis.multi do |m|
m.set "foo", "s1"
end
end
end
end
def test_watch
res = r.watch "foo"
assert_equal "OK", res
end
def test_watch_with_an_unmodified_key
r.watch "foo"
r.multi do |multi|
multi.set "foo", "s1"
end
assert_equal "s1", r.get("foo")
end
def test_watch_with_an_unmodified_key_passed_as_array
r.watch ["foo", "bar"]
r.multi do |multi|
multi.set "foo", "s1"
end
assert_equal "s1", r.get("foo")
end
def test_watch_with_a_modified_key
r.watch "foo"
r.set "foo", "s1"
res = r.multi do |multi|
multi.set "foo", "s2"
end
assert_nil res
assert_equal "s1", r.get("foo")
end
def test_watch_with_a_modified_key_passed_as_array
r.watch ["foo", "bar"]
r.set "foo", "s1"
res = r.multi do |multi|
multi.set "foo", "s2"
end
assert_nil res
assert_equal "s1", r.get("foo")
end
def test_watch_with_a_block_and_an_unmodified_key
result = r.watch "foo" do |rd|
assert_same r, rd
rd.multi do |multi|
multi.set "foo", "s1"
end
end
assert_equal ["OK"], result
assert_equal "s1", r.get("foo")
end
def test_watch_with_a_block_and_a_modified_key
result = r.watch "foo" do |rd|
assert_same r, rd
rd.set "foo", "s1"
rd.multi do |multi|
multi.set "foo", "s2"
end
end
assert_nil result
assert_equal "s1", r.get("foo")
end
def test_watch_with_a_block_that_raises_an_exception
r.set("foo", "s1")
begin
r.watch "foo" do
raise "test"
end
rescue RuntimeError
end
r.set("foo", "s2")
# If the watch was still set from within the block above, this multi/exec
# would fail. This proves that raising an exception above unwatches.
r.multi do |multi|
multi.set "foo", "s3"
end
assert_equal "s3", r.get("foo")
end
def test_unwatch_with_a_modified_key
r.watch "foo"
r.set "foo", "s1"
r.unwatch
r.multi do |multi|
multi.set "foo", "s2"
end
assert_equal "s2", r.get("foo")
end
end
|