mirror of
https://github.com/hibiken/asynq.git
synced 2025-08-19 15:08:55 +08:00
Add test to simulate situation where redis is down
This commit is contained in:
50
internal/testbroker/testbroker.go
Normal file
50
internal/testbroker/testbroker.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2020 Kentaro Hibino. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license
|
||||
// that can be found in the LICENSE file.
|
||||
|
||||
// Package testbroker exports a broker implementation that should be used in package testing.
|
||||
package testbroker
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/go-redis/redis/v7"
|
||||
"github.com/hibiken/asynq/internal/rdb"
|
||||
)
|
||||
|
||||
var errRedisDown = errors.New("asynqtest: redis is down")
|
||||
|
||||
// TestBroker is a broker implementation which enables
|
||||
// to simulate Redis failure in tests.
|
||||
type TestBroker struct {
|
||||
mu sync.Mutex
|
||||
sleeping bool
|
||||
|
||||
*rdb.RDB
|
||||
}
|
||||
|
||||
func NewTestBroker(r *rdb.RDB) *TestBroker {
|
||||
return &TestBroker{RDB: r}
|
||||
}
|
||||
|
||||
func (tb *TestBroker) Sleep() {
|
||||
tb.mu.Lock()
|
||||
defer tb.mu.Unlock()
|
||||
tb.sleeping = true
|
||||
}
|
||||
|
||||
func (tb *TestBroker) Wakeup() {
|
||||
tb.mu.Lock()
|
||||
defer tb.mu.Unlock()
|
||||
tb.sleeping = false
|
||||
}
|
||||
|
||||
func (tb *TestBroker) CancelationPubSub() (*redis.PubSub, error) {
|
||||
tb.mu.Lock()
|
||||
defer tb.mu.Unlock()
|
||||
if tb.sleeping {
|
||||
return nil, errRedisDown
|
||||
}
|
||||
return tb.RDB.CancelationPubSub()
|
||||
}
|
Reference in New Issue
Block a user