Skip to content

Commit f59ccc8

Browse files
committed
Fix slow test cases which depend on timeout seconds
1 parent 1317ecb commit f59ccc8

File tree

5 files changed

+63
-63
lines changed

5 files changed

+63
-63
lines changed

test/blocking_commands_test.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
require_relative "helper"
2-
require_relative "lint/blocking_commands"
1+
require_relative 'helper'
2+
require_relative 'lint/blocking_commands'
33

44
class TestBlockingCommands < Test::Unit::TestCase
5-
65
include Helper::Client
76
include Lint::BlockingCommands
87

98
def assert_takes_longer_than_client_timeout
10-
timeout = OPTIONS[:timeout]
11-
delay = timeout * 2
9+
timeout = LOW_TIMEOUT
10+
delay = timeout * 5
1211

13-
mock(:delay => delay) do |r|
12+
mock(delay: delay) do |r|
1413
t1 = Time.now
1514
yield(r)
1615
t2 = Time.now
@@ -22,19 +21,19 @@ def assert_takes_longer_than_client_timeout
2221

2322
def test_blpop_disable_client_timeout
2423
assert_takes_longer_than_client_timeout do |r|
25-
assert_equal ["foo", "0"], r.blpop("foo")
24+
assert_equal %w[foo 0], r.blpop('foo')
2625
end
2726
end
2827

2928
def test_brpop_disable_client_timeout
3029
assert_takes_longer_than_client_timeout do |r|
31-
assert_equal ["foo", "0"], r.brpop("foo")
30+
assert_equal %w[foo 0], r.brpop('foo')
3231
end
3332
end
3433

3534
def test_brpoplpush_disable_client_timeout
3635
assert_takes_longer_than_client_timeout do |r|
37-
assert_equal "0", r.brpoplpush("foo", "bar")
36+
assert_equal '0', r.brpoplpush('foo', 'bar')
3837
end
3938
end
4039
end

test/cluster_blocking_commands_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ class TestClusterBlockingCommands < Test::Unit::TestCase
1010

1111
def mock(options = {}, &blk)
1212
commands = build_mock_commands(options)
13-
redis_cluster_mock(commands, &blk)
13+
redis_cluster_mock(commands, { timeout: LOW_TIMEOUT }, &blk)
1414
end
1515
end

test/distributed_blocking_commands_test.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
require_relative "helper"
2-
require_relative "lint/blocking_commands"
1+
require_relative 'helper'
2+
require_relative 'lint/blocking_commands'
33

44
class TestDistributedBlockingCommands < Test::Unit::TestCase
5-
65
include Helper::Distributed
76
include Lint::BlockingCommands
87

98
def test_blpop_raises
109
assert_raises(Redis::Distributed::CannotDistribute) do
11-
r.blpop(["foo", "bar"])
10+
r.blpop(%w[foo bar])
1211
end
1312
end
1413

1514
def test_blpop_raises_with_old_prototype
1615
assert_raises(Redis::Distributed::CannotDistribute) do
17-
r.blpop("foo", "bar", 0)
16+
r.blpop('foo', 'bar', 0)
1817
end
1918
end
2019

2120
def test_brpop_raises
2221
assert_raises(Redis::Distributed::CannotDistribute) do
23-
r.brpop(["foo", "bar"])
22+
r.brpop(%w[foo bar])
2423
end
2524
end
2625

2726
def test_brpop_raises_with_old_prototype
2827
assert_raises(Redis::Distributed::CannotDistribute) do
29-
r.brpop("foo", "bar", 0)
28+
r.brpop('foo', 'bar', 0)
3029
end
3130
end
3231

3332
def test_brpoplpush_raises
3433
assert_raises(Redis::Distributed::CannotDistribute) do
35-
r.brpoplpush("foo", "bar")
34+
r.brpoplpush('foo', 'bar')
3635
end
3736
end
3837

3938
def test_brpoplpush_raises_with_old_prototype
4039
assert_raises(Redis::Distributed::CannotDistribute) do
41-
r.brpoplpush("foo", "bar", 0)
40+
r.brpoplpush('foo', 'bar', 0)
4241
end
4342
end
4443

test/lint/blocking_commands.rb

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module Lint
22
module BlockingCommands
3+
LOW_TIMEOUT = 0.01
4+
35
def setup
46
super
57

@@ -25,7 +27,7 @@ def to_protocol(obj)
2527

2628
def mock(options = {}, &blk)
2729
commands = build_mock_commands(options)
28-
redis_mock(commands, &blk)
30+
redis_mock(commands, { timeout: LOW_TIMEOUT }, &blk)
2931
end
3032

3133
def build_mock_commands(options = {})
@@ -54,82 +56,82 @@ def build_mock_commands(options = {})
5456
end
5557

5658
def test_blpop
57-
assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo")
58-
assert_equal ["{zap}foo", "s2"], r.blpop(["{zap}foo"])
59-
assert_equal ["{zap}bar", "s1"], r.blpop(["{zap}bar", "{zap}foo"])
60-
assert_equal ["{zap}bar", "s2"], r.blpop(["{zap}foo", "{zap}bar"])
59+
assert_equal ['{zap}foo', 's1'], r.blpop('{zap}foo')
60+
assert_equal ['{zap}foo', 's2'], r.blpop(['{zap}foo'])
61+
assert_equal ['{zap}bar', 's1'], r.blpop(['{zap}bar', '{zap}foo'])
62+
assert_equal ['{zap}bar', 's2'], r.blpop(['{zap}foo', '{zap}bar'])
6163
end
6264

6365
def test_blpop_timeout
6466
mock do |r|
65-
assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo")
66-
assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", :timeout => 1)
67+
assert_equal ['{zap}foo', '0'], r.blpop('{zap}foo')
68+
assert_equal ['{zap}foo', LOW_TIMEOUT.to_s], r.blpop('{zap}foo', timeout: LOW_TIMEOUT)
6769
end
6870
end
6971

7072
def test_blpop_with_old_prototype
71-
assert_equal ["{zap}foo", "s1"], r.blpop("{zap}foo", 0)
72-
assert_equal ["{zap}foo", "s2"], r.blpop("{zap}foo", 0)
73-
assert_equal ["{zap}bar", "s1"], r.blpop("{zap}bar", "{zap}foo", 0)
74-
assert_equal ["{zap}bar", "s2"], r.blpop("{zap}foo", "{zap}bar", 0)
73+
assert_equal ['{zap}foo', 's1'], r.blpop('{zap}foo', 0)
74+
assert_equal ['{zap}foo', 's2'], r.blpop('{zap}foo', 0)
75+
assert_equal ['{zap}bar', 's1'], r.blpop('{zap}bar', '{zap}foo', 0)
76+
assert_equal ['{zap}bar', 's2'], r.blpop('{zap}foo', '{zap}bar', 0)
7577
end
7678

7779
def test_blpop_timeout_with_old_prototype
7880
mock do |r|
79-
assert_equal ["{zap}foo", "0"], r.blpop("{zap}foo", 0)
80-
assert_equal ["{zap}foo", "1"], r.blpop("{zap}foo", 1)
81+
assert_equal ['{zap}foo', '0'], r.blpop('{zap}foo', 0)
82+
assert_equal ['{zap}foo', '1'], r.blpop('{zap}foo', 1)
8183
end
8284
end
8385

8486
def test_brpop
85-
assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo")
86-
assert_equal ["{zap}foo", "s1"], r.brpop(["{zap}foo"])
87-
assert_equal ["{zap}bar", "s2"], r.brpop(["{zap}bar", "{zap}foo"])
88-
assert_equal ["{zap}bar", "s1"], r.brpop(["{zap}foo", "{zap}bar"])
87+
assert_equal ['{zap}foo', 's2'], r.brpop('{zap}foo')
88+
assert_equal ['{zap}foo', 's1'], r.brpop(['{zap}foo'])
89+
assert_equal ['{zap}bar', 's2'], r.brpop(['{zap}bar', '{zap}foo'])
90+
assert_equal ['{zap}bar', 's1'], r.brpop(['{zap}foo', '{zap}bar'])
8991
end
9092

9193
def test_brpop_timeout
9294
mock do |r|
93-
assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo")
94-
assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", :timeout => 1)
95+
assert_equal ['{zap}foo', '0'], r.brpop('{zap}foo')
96+
assert_equal ['{zap}foo', LOW_TIMEOUT.to_s], r.brpop('{zap}foo', timeout: LOW_TIMEOUT)
9597
end
9698
end
9799

98100
def test_brpop_with_old_prototype
99-
assert_equal ["{zap}foo", "s2"], r.brpop("{zap}foo", 0)
100-
assert_equal ["{zap}foo", "s1"], r.brpop("{zap}foo", 0)
101-
assert_equal ["{zap}bar", "s2"], r.brpop("{zap}bar", "{zap}foo", 0)
102-
assert_equal ["{zap}bar", "s1"], r.brpop("{zap}foo", "{zap}bar", 0)
101+
assert_equal ['{zap}foo', 's2'], r.brpop('{zap}foo', 0)
102+
assert_equal ['{zap}foo', 's1'], r.brpop('{zap}foo', 0)
103+
assert_equal ['{zap}bar', 's2'], r.brpop('{zap}bar', '{zap}foo', 0)
104+
assert_equal ['{zap}bar', 's1'], r.brpop('{zap}foo', '{zap}bar', 0)
103105
end
104106

105107
def test_brpop_timeout_with_old_prototype
106108
mock do |r|
107-
assert_equal ["{zap}foo", "0"], r.brpop("{zap}foo", 0)
108-
assert_equal ["{zap}foo", "1"], r.brpop("{zap}foo", 1)
109+
assert_equal ['{zap}foo', '0'], r.brpop('{zap}foo', 0)
110+
assert_equal ['{zap}foo', '1'], r.brpop('{zap}foo', 1)
109111
end
110112
end
111113

112114
def test_brpoplpush
113-
assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux")
114-
assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
115+
assert_equal 's2', r.brpoplpush('{zap}foo', '{zap}qux')
116+
assert_equal ['s2'], r.lrange('{zap}qux', 0, -1)
115117
end
116118

117119
def test_brpoplpush_timeout
118120
mock do |r|
119-
assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar")
120-
assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
121+
assert_equal '0', r.brpoplpush('{zap}foo', '{zap}bar')
122+
assert_equal LOW_TIMEOUT.to_s, r.brpoplpush('{zap}foo', '{zap}bar', timeout: LOW_TIMEOUT)
121123
end
122124
end
123125

124126
def test_brpoplpush_with_old_prototype
125-
assert_equal "s2", r.brpoplpush("{zap}foo", "{zap}qux", 0)
126-
assert_equal ["s2"], r.lrange("{zap}qux", 0, -1)
127+
assert_equal 's2', r.brpoplpush('{zap}foo', '{zap}qux', 0)
128+
assert_equal ['s2'], r.lrange('{zap}qux', 0, -1)
127129
end
128130

129131
def test_brpoplpush_timeout_with_old_prototype
130132
mock do |r|
131-
assert_equal "0", r.brpoplpush("{zap}foo", "{zap}bar", 0)
132-
assert_equal "1", r.brpoplpush("{zap}foo", "{zap}bar", 1)
133+
assert_equal '0', r.brpoplpush('{zap}foo', '{zap}bar', 0)
134+
assert_equal '1', r.brpoplpush('{zap}foo', '{zap}bar', 1)
133135
end
134136
end
135137

@@ -147,25 +149,25 @@ def test_bzpopmax
147149

148150
driver(:ruby, :hiredis) do
149151
def test_blpop_socket_timeout
150-
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
152+
mock(delay: LOW_TIMEOUT * 5) do |r|
151153
assert_raises(Redis::TimeoutError) do
152-
r.blpop("{zap}foo", :timeout => 1)
154+
r.blpop('{zap}foo', timeout: LOW_TIMEOUT)
153155
end
154156
end
155157
end
156158

157159
def test_brpop_socket_timeout
158-
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
160+
mock(delay: LOW_TIMEOUT * 5) do |r|
159161
assert_raises(Redis::TimeoutError) do
160-
r.brpop("{zap}foo", :timeout => 1)
162+
r.brpop('{zap}foo', timeout: LOW_TIMEOUT)
161163
end
162164
end
163165
end
164166

165167
def test_brpoplpush_socket_timeout
166-
mock(:delay => 1 + OPTIONS[:timeout] * 2) do |r|
168+
mock(delay: LOW_TIMEOUT * 5) do |r|
167169
assert_raises(Redis::TimeoutError) do
168-
r.brpoplpush("{zap}foo", "{zap}bar", :timeout => 1)
170+
r.brpoplpush('{zap}foo', '{zap}bar', timeout: LOW_TIMEOUT)
169171
end
170172
end
171173
end

test/publish_subscribe_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,15 @@ def test_unsubscribe_without_a_subscribe
232232

233233
def test_subscribe_past_a_timeout
234234
# For some reason, a thread here doesn't reproduce the issue.
235-
sleep = %{sleep #{OPTIONS[:timeout] * 2}}
235+
sleep = %(sleep 0.05)
236236
publish = %{ruby -rsocket -e 't=TCPSocket.new("127.0.0.1",#{OPTIONS[:port]});t.write("publish foo bar\\r\\n");t.read(4);t.close'}
237-
cmd = [sleep, publish].join("; ")
237+
cmd = [sleep, publish].join('; ')
238238

239-
IO.popen(cmd, "r+") do |pipe|
239+
IO.popen(cmd, 'r+') do |_pipe|
240240
received = false
241241

242-
r.subscribe "foo" do |on|
243-
on.message do |channel, message|
242+
r.subscribe 'foo' do |on|
243+
on.message do |_channel, _message|
244244
received = true
245245
r.unsubscribe
246246
end

0 commit comments

Comments
 (0)