2
0
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:
Ken Hibino
2020-04-17 07:52:12 -07:00
parent f8a94fb839
commit 46ab4417dd
3 changed files with 105 additions and 4 deletions

View 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()
}