mirror of
https://github.com/hibiken/asynq.git
synced 2024-11-10 11:31:58 +08:00
7af3981929
Changes: - Task messages are stored under "asynq:{<qname>}:t:<task_id>" key in redis, value is a HASH type and message are stored under "msg" key in the hash. The hash also stores "deadline", "timeout". - Redis LIST and ZSET stores task message IDs - Task messages are serialized using protocol buffer
200 lines
4.3 KiB
Go
200 lines
4.3 KiB
Go
// 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"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v7"
|
|
"github.com/hibiken/asynq/internal/base"
|
|
)
|
|
|
|
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
|
|
|
|
// real broker
|
|
real base.Broker
|
|
}
|
|
|
|
// Make sure TestBroker implements Broker interface at compile time.
|
|
var _ base.Broker = (*TestBroker)(nil)
|
|
|
|
func NewTestBroker(b base.Broker) *TestBroker {
|
|
return &TestBroker{real: b}
|
|
}
|
|
|
|
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) 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)
|
|
}
|
|
|
|
func (tb *TestBroker) Dequeue(qnames ...string) (*base.TaskMessage, time.Time, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, time.Time{}, errRedisDown
|
|
}
|
|
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) Archive(msg *base.TaskMessage, errMsg string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Archive(msg, errMsg)
|
|
}
|
|
|
|
func (tb *TestBroker) ForwardIfReady(qnames ...string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.ForwardIfReady(qnames...)
|
|
}
|
|
|
|
func (tb *TestBroker) ListDeadlineExceeded(deadline time.Time, qnames ...string) ([]*base.TaskMessage, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, errRedisDown
|
|
}
|
|
return tb.real.ListDeadlineExceeded(deadline, qnames...)
|
|
}
|
|
|
|
func (tb *TestBroker) WriteServerState(info *base.ServerInfo, workers []*base.WorkerInfo, ttl time.Duration) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.WriteServerState(info, workers, ttl)
|
|
}
|
|
|
|
func (tb *TestBroker) ClearServerState(host string, pid int, serverID string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.ClearServerState(host, pid, serverID)
|
|
}
|
|
|
|
func (tb *TestBroker) CancelationPubSub() (*redis.PubSub, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, errRedisDown
|
|
}
|
|
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) Ping() error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Ping()
|
|
}
|
|
|
|
func (tb *TestBroker) Close() error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Close()
|
|
}
|