1
1
# frozen_string_literal: true
2
2
3
3
# Mock
4
- module RedisMock
4
+ class RedisMock # rubocop:disable Lint/EmptyClass
5
5
end
6
6
7
7
# Mock
8
8
class WorkerMock
9
9
def self . bouncer
10
10
SidekiqBouncer ::Bouncer . new ( self )
11
11
end
12
-
13
- # def self.perform_at(*args); end
14
12
end
15
13
16
14
# Tests
17
15
describe SidekiqBouncer ::Bouncer do
18
16
# careful, the bouncer instance is generally cached on the worker model
19
17
subject ( :bouncer ) { WorkerMock . bouncer }
20
18
21
- before :all do
19
+ let ( :redis ) { SidekiqBouncer . config . redis }
20
+ let ( :worker_klass ) { WorkerMock }
21
+ let ( :now ) { Time . now . to_i }
22
+
23
+ before do
22
24
SidekiqBouncer . configure do |config |
23
- config . redis = RedisMock
25
+ config . redis = RedisMock . new
24
26
end
25
- end
26
27
27
- let ( :redis ) { RedisMock }
28
- let ( :worker_klass ) { WorkerMock }
29
- let ( :now ) { 100 }
28
+ Timecop . freeze ( Time . now )
30
29
31
- before do
32
30
# stubbing
33
- allow ( bouncer ) . to receive ( :now_i ) { now }
34
31
allow ( redis ) . to receive ( :call )
35
32
allow ( worker_klass ) . to receive ( :perform_at )
36
33
end
@@ -43,7 +40,6 @@ def self.bouncer
43
40
it { expect ( bouncer ) . to respond_to ( :delay_buffer= ) }
44
41
it { expect ( bouncer ) . to respond_to ( :debounce ) }
45
42
it { expect ( bouncer ) . to respond_to ( :let_in? ) }
46
- it { expect ( bouncer ) . to respond_to ( :redis ) }
47
43
end
48
44
49
45
describe '.new' do
@@ -82,7 +78,7 @@ def self.bouncer
82
78
it 'sets scoped_key to Redis with delayed timestamp' do
83
79
bouncer . debounce ( 'test_param_1' , 'test_param_2' , key_or_args_indices : [ 0 , 1 ] )
84
80
85
- expect ( SidekiqBouncer . config . redis )
81
+ expect ( redis )
86
82
. to have_received ( :call )
87
83
. with ( 'SET' , 'WorkerMock:test_param_1,test_param_2' , now + bouncer . delay )
88
84
end
@@ -101,7 +97,7 @@ def self.bouncer
101
97
it 'sets scoped_key to Redis with delayed timestamp' do
102
98
bouncer . debounce ( 'test_param_1' , 'test_param_2' , key_or_args_indices : [ 0 ] )
103
99
104
- expect ( SidekiqBouncer . config . redis )
100
+ expect ( redis )
105
101
. to have_received ( :call )
106
102
. with ( 'SET' , 'WorkerMock:test_param_1' , now + bouncer . delay )
107
103
end
@@ -121,7 +117,7 @@ def self.bouncer
121
117
describe '#let_in?' do
122
118
context 'when key is nil' do
123
119
it 'does not call redis' do
124
- expect ( SidekiqBouncer . config . redis ) . not_to have_received ( :call )
120
+ expect ( redis ) . not_to have_received ( :call )
125
121
end
126
122
127
123
it 'returns true' do
@@ -135,7 +131,7 @@ def self.bouncer
135
131
it 'exec call on redis with GET' do
136
132
bouncer . let_in? ( key )
137
133
138
- expect ( SidekiqBouncer . config . redis )
134
+ expect ( redis )
139
135
. to have_received ( :call )
140
136
. with ( 'GET' , key )
141
137
end
@@ -145,14 +141,6 @@ def self.bouncer
145
141
allow ( redis ) . to receive ( :call ) . with ( 'GET' , anything ) . and_return ( now - 10 )
146
142
end
147
143
148
- it 'exec call on redis with DEL' do
149
- bouncer . let_in? ( key )
150
-
151
- expect ( SidekiqBouncer . config . redis )
152
- . to have_received ( :call )
153
- . with ( 'DEL' , key )
154
- end
155
-
156
144
it 'returns true' do
157
145
expect ( bouncer . let_in? ( key ) ) . to be ( true )
158
146
end
@@ -163,14 +151,6 @@ def self.bouncer
163
151
allow ( redis ) . to receive ( :call ) . with ( 'GET' , anything ) . and_return ( Time . now + 10 )
164
152
end
165
153
166
- it 'does not exec call on redis with DEL' do
167
- bouncer . let_in? ( key )
168
-
169
- expect ( SidekiqBouncer . config . redis )
170
- . not_to have_received ( :call )
171
- . with ( 'DEL' , anything )
172
- end
173
-
174
154
it 'returns false' do
175
155
expect ( bouncer . let_in? ( key ) ) . to be ( false )
176
156
end
@@ -181,18 +161,72 @@ def self.bouncer
181
161
allow ( redis ) . to receive ( :call ) . with ( 'GET' , anything ) . and_return ( nil )
182
162
end
183
163
184
- it 'does not exec call on redis with DEL' do
185
- bouncer . let_in? ( key )
186
-
187
- expect ( SidekiqBouncer . config . redis )
188
- . not_to have_received ( :call )
189
- . with ( 'DEL' , anything )
190
- end
191
-
192
164
it 'returns false' do
193
165
expect ( bouncer . let_in? ( key ) ) . to be ( false )
194
166
end
195
167
end
196
168
end
197
169
end
170
+
171
+ describe '#run' do
172
+ before do
173
+ # stubbing
174
+ allow ( bouncer ) . to receive ( :let_in? ) . with ( 'do' ) . and_return ( true )
175
+ allow ( bouncer ) . to receive ( :let_in? ) . with ( 'do_not' ) . and_return ( false )
176
+ end
177
+
178
+ context 'when let_in? returns false' do
179
+ it 'returns false' do
180
+ expect ( bouncer . run ( 'do_not' ) ) . to be ( false )
181
+ end
182
+
183
+ it 'does not yield' do
184
+ expect { |b | bouncer . run ( 'do_not' , &b ) } . not_to yield_control
185
+ end
186
+
187
+ it 'does not exec call on redis with DEL' do
188
+ bouncer . run ( 'do_not' ) { '__test__' }
189
+
190
+ expect ( redis )
191
+ . not_to have_received ( :call )
192
+ . with ( 'DEL' , anything )
193
+ end
194
+ end
195
+
196
+ context 'when let_in? returns true' do
197
+ it 'returns yield return' do
198
+ expect ( bouncer . run ( 'do' ) { '__test__' } ) . to be ( '__test__' )
199
+ end
200
+
201
+ it 'yields' do
202
+ expect { |b | bouncer . run ( 'do' , &b ) } . to yield_control
203
+ end
204
+
205
+ it 'exec call on redis with DEL' do
206
+ bouncer . run ( 'do' ) { '__test__' }
207
+
208
+ expect ( redis )
209
+ . to have_received ( :call )
210
+ . with ( 'DEL' , 'do' )
211
+ end
212
+ end
213
+ end
214
+
215
+ describe '#now_i' do
216
+ it 'returns now as integer' do
217
+ expect ( bouncer . send ( :now_i ) ) . to be ( now )
218
+ end
219
+ end
220
+
221
+ describe '#redis' do
222
+ it 'returns' do
223
+ expect ( bouncer . send ( :redis ) ) . to be_a ( RedisMock )
224
+ end
225
+ end
226
+
227
+ describe '#redis_key' do
228
+ it 'returns now as integer' do
229
+ expect ( bouncer . send ( :redis_key , 'test_key' ) ) . to eql ( 'WorkerMock:test_key' )
230
+ end
231
+ end
198
232
end
0 commit comments