@@ -188,159 +188,162 @@ var _ = Describe("Redis Ring", func() {
188
188
})
189
189
})
190
190
191
- It ("supports Process hook" , func () {
192
- //the health check leads to data race for variable "stack []string".
193
- //here, the health check time is set to 72 hours to avoid health check
194
- opt := redisRingOptions ()
195
- opt .HeartbeatFrequency = 72 * time .Hour
196
- ring = redis .NewRing (opt )
197
-
198
- err := ring .Ping (ctx ).Err ()
199
- Expect (err ).NotTo (HaveOccurred ())
200
-
201
- var stack []string
202
-
203
- ring .AddHook (& hook {
204
- beforeProcess : func (ctx context.Context , cmd redis.Cmder ) (context.Context , error ) {
205
- Expect (cmd .String ()).To (Equal ("ping: " ))
206
- stack = append (stack , "ring.BeforeProcess" )
207
- return ctx , nil
208
- },
209
- afterProcess : func (ctx context.Context , cmd redis.Cmder ) error {
210
- Expect (cmd .String ()).To (Equal ("ping: PONG" ))
211
- stack = append (stack , "ring.AfterProcess" )
212
- return nil
213
- },
191
+ Describe ("Process hook" , func () {
192
+ BeforeEach (func () {
193
+ //the health check leads to data race for variable "stack []string".
194
+ //here, the health check time is set to 72 hours to avoid health check
195
+ opt := redisRingOptions ()
196
+ opt .HeartbeatFrequency = 72 * time .Hour
197
+ ring = redis .NewRing (opt )
214
198
})
199
+ It ("supports Process hook" , func () {
200
+ err := ring .Ping (ctx ).Err ()
201
+ Expect (err ).NotTo (HaveOccurred ())
215
202
216
- ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
217
- shard .AddHook (& hook {
203
+ var stack []string
204
+
205
+ ring .AddHook (& hook {
218
206
beforeProcess : func (ctx context.Context , cmd redis.Cmder ) (context.Context , error ) {
219
207
Expect (cmd .String ()).To (Equal ("ping: " ))
220
- stack = append (stack , "shard .BeforeProcess" )
208
+ stack = append (stack , "ring .BeforeProcess" )
221
209
return ctx , nil
222
210
},
223
211
afterProcess : func (ctx context.Context , cmd redis.Cmder ) error {
224
212
Expect (cmd .String ()).To (Equal ("ping: PONG" ))
225
- stack = append (stack , "shard .AfterProcess" )
213
+ stack = append (stack , "ring .AfterProcess" )
226
214
return nil
227
215
},
228
216
})
229
- return nil
230
- })
231
217
232
- err = ring .Ping (ctx ).Err ()
233
- Expect (err ).NotTo (HaveOccurred ())
234
- Expect (stack ).To (Equal ([]string {
235
- "ring.BeforeProcess" ,
236
- "shard.BeforeProcess" ,
237
- "shard.AfterProcess" ,
238
- "ring.AfterProcess" ,
239
- }))
240
- })
218
+ ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
219
+ shard .AddHook (& hook {
220
+ beforeProcess : func (ctx context.Context , cmd redis.Cmder ) (context.Context , error ) {
221
+ Expect (cmd .String ()).To (Equal ("ping: " ))
222
+ stack = append (stack , "shard.BeforeProcess" )
223
+ return ctx , nil
224
+ },
225
+ afterProcess : func (ctx context.Context , cmd redis.Cmder ) error {
226
+ Expect (cmd .String ()).To (Equal ("ping: PONG" ))
227
+ stack = append (stack , "shard.AfterProcess" )
228
+ return nil
229
+ },
230
+ })
231
+ return nil
232
+ })
241
233
242
- It ("supports Pipeline hook" , func () {
243
- err := ring .Ping (ctx ).Err ()
244
- Expect (err ).NotTo (HaveOccurred ())
234
+ err = ring .Ping (ctx ).Err ()
235
+ Expect (err ).NotTo (HaveOccurred ())
236
+ Expect (stack ).To (Equal ([]string {
237
+ "ring.BeforeProcess" ,
238
+ "shard.BeforeProcess" ,
239
+ "shard.AfterProcess" ,
240
+ "ring.AfterProcess" ,
241
+ }))
242
+ })
245
243
246
- var stack []string
244
+ It ("supports Pipeline hook" , func () {
245
+ err := ring .Ping (ctx ).Err ()
246
+ Expect (err ).NotTo (HaveOccurred ())
247
247
248
- ring .AddHook (& hook {
249
- beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
250
- Expect (cmds ).To (HaveLen (1 ))
251
- Expect (cmds [0 ].String ()).To (Equal ("ping: " ))
252
- stack = append (stack , "ring.BeforeProcessPipeline" )
253
- return ctx , nil
254
- },
255
- afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
256
- Expect (cmds ).To (HaveLen (1 ))
257
- Expect (cmds [0 ].String ()).To (Equal ("ping: PONG" ))
258
- stack = append (stack , "ring.AfterProcessPipeline" )
259
- return nil
260
- },
261
- })
248
+ var stack []string
262
249
263
- ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
264
- shard .AddHook (& hook {
250
+ ring .AddHook (& hook {
265
251
beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
266
252
Expect (cmds ).To (HaveLen (1 ))
267
253
Expect (cmds [0 ].String ()).To (Equal ("ping: " ))
268
- stack = append (stack , "shard .BeforeProcessPipeline" )
254
+ stack = append (stack , "ring .BeforeProcessPipeline" )
269
255
return ctx , nil
270
256
},
271
257
afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
272
258
Expect (cmds ).To (HaveLen (1 ))
273
259
Expect (cmds [0 ].String ()).To (Equal ("ping: PONG" ))
274
- stack = append (stack , "shard .AfterProcessPipeline" )
260
+ stack = append (stack , "ring .AfterProcessPipeline" )
275
261
return nil
276
262
},
277
263
})
278
- return nil
279
- })
280
-
281
- _ , err = ring .Pipelined (ctx , func (pipe redis.Pipeliner ) error {
282
- pipe .Ping (ctx )
283
- return nil
284
- })
285
- Expect (err ).NotTo (HaveOccurred ())
286
- Expect (stack ).To (Equal ([]string {
287
- "ring.BeforeProcessPipeline" ,
288
- "shard.BeforeProcessPipeline" ,
289
- "shard.AfterProcessPipeline" ,
290
- "ring.AfterProcessPipeline" ,
291
- }))
292
- })
293
-
294
- It ("supports TxPipeline hook" , func () {
295
- err := ring .Ping (ctx ).Err ()
296
- Expect (err ).NotTo (HaveOccurred ())
297
264
298
- var stack []string
265
+ ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
266
+ shard .AddHook (& hook {
267
+ beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
268
+ Expect (cmds ).To (HaveLen (1 ))
269
+ Expect (cmds [0 ].String ()).To (Equal ("ping: " ))
270
+ stack = append (stack , "shard.BeforeProcessPipeline" )
271
+ return ctx , nil
272
+ },
273
+ afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
274
+ Expect (cmds ).To (HaveLen (1 ))
275
+ Expect (cmds [0 ].String ()).To (Equal ("ping: PONG" ))
276
+ stack = append (stack , "shard.AfterProcessPipeline" )
277
+ return nil
278
+ },
279
+ })
280
+ return nil
281
+ })
299
282
300
- ring .AddHook (& hook {
301
- beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
302
- Expect (cmds ).To (HaveLen (1 ))
303
- Expect (cmds [0 ].String ()).To (Equal ("ping: " ))
304
- stack = append (stack , "ring.BeforeProcessPipeline" )
305
- return ctx , nil
306
- },
307
- afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
308
- Expect (cmds ).To (HaveLen (1 ))
309
- Expect (cmds [0 ].String ()).To (Equal ("ping: PONG" ))
310
- stack = append (stack , "ring.AfterProcessPipeline" )
283
+ _ , err = ring .Pipelined (ctx , func (pipe redis.Pipeliner ) error {
284
+ pipe .Ping (ctx )
311
285
return nil
312
- },
286
+ })
287
+ Expect (err ).NotTo (HaveOccurred ())
288
+ Expect (stack ).To (Equal ([]string {
289
+ "ring.BeforeProcessPipeline" ,
290
+ "shard.BeforeProcessPipeline" ,
291
+ "shard.AfterProcessPipeline" ,
292
+ "ring.AfterProcessPipeline" ,
293
+ }))
313
294
})
314
295
315
- ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
316
- shard .AddHook (& hook {
296
+ It ("supports TxPipeline hook" , func () {
297
+ err := ring .Ping (ctx ).Err ()
298
+ Expect (err ).NotTo (HaveOccurred ())
299
+
300
+ var stack []string
301
+
302
+ ring .AddHook (& hook {
317
303
beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
318
- Expect (cmds ).To (HaveLen (3 ))
319
- Expect (cmds [1 ].String ()).To (Equal ("ping: " ))
320
- stack = append (stack , "shard .BeforeProcessPipeline" )
304
+ Expect (cmds ).To (HaveLen (1 ))
305
+ Expect (cmds [0 ].String ()).To (Equal ("ping: " ))
306
+ stack = append (stack , "ring .BeforeProcessPipeline" )
321
307
return ctx , nil
322
308
},
323
309
afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
324
- Expect (cmds ).To (HaveLen (3 ))
325
- Expect (cmds [1 ].String ()).To (Equal ("ping: PONG" ))
326
- stack = append (stack , "shard .AfterProcessPipeline" )
310
+ Expect (cmds ).To (HaveLen (1 ))
311
+ Expect (cmds [0 ].String ()).To (Equal ("ping: PONG" ))
312
+ stack = append (stack , "ring .AfterProcessPipeline" )
327
313
return nil
328
314
},
329
315
})
330
- return nil
331
- })
332
316
333
- _ , err = ring .TxPipelined (ctx , func (pipe redis.Pipeliner ) error {
334
- pipe .Ping (ctx )
335
- return nil
317
+ ring .ForEachShard (ctx , func (ctx context.Context , shard * redis.Client ) error {
318
+ shard .AddHook (& hook {
319
+ beforeProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) (context.Context , error ) {
320
+ Expect (cmds ).To (HaveLen (3 ))
321
+ Expect (cmds [1 ].String ()).To (Equal ("ping: " ))
322
+ stack = append (stack , "shard.BeforeProcessPipeline" )
323
+ return ctx , nil
324
+ },
325
+ afterProcessPipeline : func (ctx context.Context , cmds []redis.Cmder ) error {
326
+ Expect (cmds ).To (HaveLen (3 ))
327
+ Expect (cmds [1 ].String ()).To (Equal ("ping: PONG" ))
328
+ stack = append (stack , "shard.AfterProcessPipeline" )
329
+ return nil
330
+ },
331
+ })
332
+ return nil
333
+ })
334
+
335
+ _ , err = ring .TxPipelined (ctx , func (pipe redis.Pipeliner ) error {
336
+ pipe .Ping (ctx )
337
+ return nil
338
+ })
339
+ Expect (err ).NotTo (HaveOccurred ())
340
+ Expect (stack ).To (Equal ([]string {
341
+ "ring.BeforeProcessPipeline" ,
342
+ "shard.BeforeProcessPipeline" ,
343
+ "shard.AfterProcessPipeline" ,
344
+ "ring.AfterProcessPipeline" ,
345
+ }))
336
346
})
337
- Expect (err ).NotTo (HaveOccurred ())
338
- Expect (stack ).To (Equal ([]string {
339
- "ring.BeforeProcessPipeline" ,
340
- "shard.BeforeProcessPipeline" ,
341
- "shard.AfterProcessPipeline" ,
342
- "ring.AfterProcessPipeline" ,
343
- }))
344
347
})
345
348
})
346
349
0 commit comments