2020-04-17 22:52:12 +08:00
|
|
|
// 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"
|
2020-04-18 22:55:10 +08:00
|
|
|
"time"
|
2020-04-17 22:52:12 +08:00
|
|
|
|
|
|
|
"github.com/go-redis/redis/v7"
|
2020-04-18 22:55:10 +08:00
|
|
|
"github.com/hibiken/asynq/internal/base"
|
2020-04-17 22:52:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-04-18 22:55:10 +08:00
|
|
|
// real broker
|
|
|
|
real base.Broker
|
2020-04-17 22:52:12 +08:00
|
|
|
}
|
|
|
|
|
2020-04-18 22:55:10 +08:00
|
|
|
func NewTestBroker(b base.Broker) *TestBroker {
|
|
|
|
return &TestBroker{real: b}
|
2020-04-17 22:52:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-04-18 22:55:10 +08:00
|
|
|
func (tb *TestBroker) Enqueue(msg *base.TaskMessage) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Enqueue(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) EnqueueUnique(msg *base.TaskMessage, ttl time.Duration) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.EnqueueUnique(msg, ttl)
|
|
|
|
}
|
|
|
|
|
2020-06-18 20:45:10 +08:00
|
|
|
func (tb *TestBroker) Dequeue(qnames ...string) (*base.TaskMessage, int, error) {
|
2020-04-18 22:55:10 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
2020-06-18 20:45:10 +08:00
|
|
|
return nil, 0, errRedisDown
|
2020-04-18 22:55:10 +08:00
|
|
|
}
|
|
|
|
return tb.real.Dequeue(qnames...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Done(msg *base.TaskMessage) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Done(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Requeue(msg *base.TaskMessage) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Requeue(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Schedule(msg *base.TaskMessage, processAt time.Time) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Schedule(msg, processAt)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) ScheduleUnique(msg *base.TaskMessage, processAt time.Time, ttl time.Duration) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.ScheduleUnique(msg, processAt, ttl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Retry(msg *base.TaskMessage, processAt time.Time, errMsg string) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Retry(msg, processAt, errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Kill(msg *base.TaskMessage, errMsg string) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Kill(msg, errMsg)
|
|
|
|
}
|
|
|
|
|
2020-06-08 04:04:27 +08:00
|
|
|
func (tb *TestBroker) CheckAndEnqueue() error {
|
2020-04-18 22:55:10 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.CheckAndEnqueue()
|
|
|
|
}
|
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
func (tb *TestBroker) WriteServerState(info *base.ServerInfo, workers []*base.WorkerInfo, ttl time.Duration) error {
|
2020-04-18 22:55:10 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
2020-05-19 11:47:35 +08:00
|
|
|
return tb.real.WriteServerState(info, workers, ttl)
|
2020-04-18 22:55:10 +08:00
|
|
|
}
|
|
|
|
|
2020-05-19 11:47:35 +08:00
|
|
|
func (tb *TestBroker) ClearServerState(host string, pid int, serverID string) error {
|
2020-04-18 22:55:10 +08:00
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
2020-05-19 11:47:35 +08:00
|
|
|
return tb.real.ClearServerState(host, pid, serverID)
|
2020-04-18 22:55:10 +08:00
|
|
|
}
|
|
|
|
|
2020-04-17 22:52:12 +08:00
|
|
|
func (tb *TestBroker) CancelationPubSub() (*redis.PubSub, error) {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return nil, errRedisDown
|
|
|
|
}
|
2020-04-18 22:55:10 +08:00
|
|
|
return tb.real.CancelationPubSub()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) PublishCancelation(id string) error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.PublishCancelation(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tb *TestBroker) Close() error {
|
|
|
|
tb.mu.Lock()
|
|
|
|
defer tb.mu.Unlock()
|
|
|
|
if tb.sleeping {
|
|
|
|
return errRedisDown
|
|
|
|
}
|
|
|
|
return tb.real.Close()
|
2020-04-17 22:52:12 +08:00
|
|
|
}
|