forked from redis/redis-rb
/
pipelining_commands_test.rb
262 lines (210 loc) · 5.44 KB
/
pipelining_commands_test.rb
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
# frozen_string_literal: true
require_relative "helper"
class TestPipeliningCommands < Minitest::Test
include Helper::Client
def test_bulk_commands
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")
end
def test_multi_bulk_commands
r.pipelined do
r.mset("foo", "s1", "bar", "s2")
r.mset("baz", "s3", "qux", "s4")
end
assert_equal "s1", r.get("foo")
assert_equal "s2", r.get("bar")
assert_equal "s3", r.get("baz")
assert_equal "s4", r.get("qux")
end
def test_bulk_and_multi_bulk_commands_mixed
r.pipelined do
r.lpush "foo", "s1"
r.lpush "foo", "s2"
r.mset("baz", "s3", "qux", "s4")
end
assert_equal 2, r.llen("foo")
assert_equal "s2", r.lpop("foo")
assert_equal "s1", r.lpop("foo")
assert_equal "s3", r.get("baz")
assert_equal "s4", r.get("qux")
end
def test_multi_bulk_and_bulk_commands_mixed
r.pipelined do
r.mset("baz", "s3", "qux", "s4")
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 "s3", r.get("baz")
assert_equal "s4", r.get("qux")
end
def test_pipelined_with_an_empty_block
r.pipelined do
end
assert_equal 0, r.dbsize
end
def test_returning_the_result_of_a_pipeline
result = r.pipelined do
r.set "foo", "bar"
r.get "foo"
r.get "bar"
end
assert_equal ["OK", "bar", nil], result
end
def test_assignment_of_results_inside_the_block
r.pipelined do
@first = r.sadd("foo", 1)
@second = r.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_of_results_inside_the_block_with_errors
assert_raises(Redis::CommandError) do
r.pipelined do
r.doesnt_exist
@first = r.sadd("foo", 1)
@second = r.sadd("foo", 1)
end
end
assert_raises(Redis::FutureNotReady) { @first.value }
assert_raises(Redis::FutureNotReady) { @second.value }
end
def test_assignment_of_results_inside_a_nested_block
r.pipelined do
@first = r.sadd("foo", 1)
r.pipelined do
@second = r.sadd("foo", 1)
end
end
assert_equal true, @first.value
assert_equal false, @second.value
end
def test_futures_raise_when_confused_with_something_else
r.pipelined do
@result = r.sadd("foo", 1)
end
assert_raises(NoMethodError) { @result.to_s }
end
def test_futures_raise_when_trying_to_access_their_values_too_early
r.pipelined do
assert_raises(Redis::FutureNotReady) do
r.sadd("foo", 1).value
end
end
end
def test_futures_raise_when_command_errors_and_needs_transformation
assert_raises(Redis::CommandError) do
r.pipelined do
@result = r.zrange("a", "b", 5, with_scores: true)
end
end
end
def test_futures_warn_when_tested_for_equality
r.pipelined do
@result = r.sadd("foo", 1)
end
assert_output(nil, /deprecated/) do
@result == 1
end
end
def test_futures_can_be_identified
r.pipelined do
@result = r.sadd("foo", 1)
end
assert_equal true, @result.is_a?(Redis::Future)
assert_equal true, @result.is_a?(::BasicObject)
assert_equal Redis::Future, @result.class
end
def test_returning_the_result_of_an_empty_pipeline
result = r.pipelined do
end
assert_equal [], result
end
def test_nesting_pipeline_blocks
r.pipelined do
r.set("foo", "s1")
r.pipelined do
r.set("bar", "s2")
end
end
assert_equal "s1", r.get("foo")
assert_equal "s2", r.get("bar")
end
def test_info_in_a_pipeline_returns_hash
result = r.pipelined do
r.info
end
assert result.first.is_a?(Hash)
end
def test_config_get_in_a_pipeline_returns_hash
result = r.pipelined do
r.config(:get, "*")
end
assert result.first.is_a?(Hash)
end
def test_hgetall_in_a_pipeline_returns_hash
r.hmset("hash", "field", "value")
result = r.pipelined do
r.hgetall("hash")
end
assert_equal result.first, { "field" => "value" }
end
def test_keys_in_a_pipeline
r.set("key", "value")
result = r.pipelined do
r.keys("*")
end
assert_equal ["key"], result.first
end
def test_pipeline_yields_a_connection
r.pipelined do |p|
p.set("foo", "bar")
end
assert_equal "bar", r.get("foo")
end
def test_pipeline_select
r.select 1
r.set("db", "1")
r.pipelined do |p|
p.select 2
p.set("db", "2")
end
r.select 1
assert_equal "1", r.get("db")
r.select 2
assert_equal "2", r.get("db")
end
def test_pipeline_select_client_db
r.select 1
r.pipelined do |p2|
p2.select 2
end
assert_equal 2, r._client.db
end
def test_nested_pipeline_select_client_db
r.select 1
r.pipelined do |p2|
p2.select 2
p2.pipelined do |p3|
p3.select 3
end
end
assert_equal 3, r._client.db
end
def test_pipeline_interrupt_preserves_client
original = r._client
Redis::Pipeline.stubs(:new).raises(Interrupt)
assert_raises(Interrupt) { r.pipelined {} }
assert_equal r._client, original
end
end