mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 16:13:40 +08:00
d04888e748
* feature: configurable janitor interval and deletion batch size * warn user when they set a big number of janitor batch size * Update CHANGELOG.md --------- Co-authored-by: Agung Hariadi Tedja <agung.tedja@kumparan.com>
300 lines
7.1 KiB
Go
300 lines
7.1 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 (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
var errRedisDown = errors.New("testutil: 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(ctx context.Context, msg *base.TaskMessage) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Enqueue(ctx, msg)
|
|
}
|
|
|
|
func (tb *TestBroker) EnqueueUnique(ctx context.Context, msg *base.TaskMessage, ttl time.Duration) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.EnqueueUnique(ctx, 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(ctx context.Context, msg *base.TaskMessage) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Done(ctx, msg)
|
|
}
|
|
|
|
func (tb *TestBroker) MarkAsComplete(ctx context.Context, msg *base.TaskMessage) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.MarkAsComplete(ctx, msg)
|
|
}
|
|
|
|
func (tb *TestBroker) Requeue(ctx context.Context, msg *base.TaskMessage) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Requeue(ctx, msg)
|
|
}
|
|
|
|
func (tb *TestBroker) Schedule(ctx context.Context, msg *base.TaskMessage, processAt time.Time) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Schedule(ctx, msg, processAt)
|
|
}
|
|
|
|
func (tb *TestBroker) ScheduleUnique(ctx context.Context, 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(ctx, msg, processAt, ttl)
|
|
}
|
|
|
|
func (tb *TestBroker) Retry(ctx context.Context, msg *base.TaskMessage, processAt time.Time, errMsg string, isFailure bool) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Retry(ctx, msg, processAt, errMsg, isFailure)
|
|
}
|
|
|
|
func (tb *TestBroker) Archive(ctx context.Context, msg *base.TaskMessage, errMsg string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.Archive(ctx, 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) DeleteExpiredCompletedTasks(qname string, batchSize int) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.DeleteExpiredCompletedTasks(qname, batchSize)
|
|
}
|
|
|
|
func (tb *TestBroker) ListLeaseExpired(cutoff time.Time, qnames ...string) ([]*base.TaskMessage, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, errRedisDown
|
|
}
|
|
return tb.real.ListLeaseExpired(cutoff, qnames...)
|
|
}
|
|
|
|
func (tb *TestBroker) ExtendLease(qname string, ids ...string) (time.Time, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return time.Time{}, errRedisDown
|
|
}
|
|
return tb.real.ExtendLease(qname, ids...)
|
|
}
|
|
|
|
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) WriteResult(qname, id string, data []byte) (int, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return 0, errRedisDown
|
|
}
|
|
return tb.real.WriteResult(qname, id, data)
|
|
}
|
|
|
|
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()
|
|
}
|
|
|
|
func (tb *TestBroker) AddToGroup(ctx context.Context, msg *base.TaskMessage, gname string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.AddToGroup(ctx, msg, gname)
|
|
}
|
|
|
|
func (tb *TestBroker) AddToGroupUnique(ctx context.Context, msg *base.TaskMessage, gname string, ttl time.Duration) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.AddToGroupUnique(ctx, msg, gname, ttl)
|
|
}
|
|
|
|
func (tb *TestBroker) ListGroups(qname string) ([]string, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, errRedisDown
|
|
}
|
|
return tb.real.ListGroups(qname)
|
|
}
|
|
|
|
func (tb *TestBroker) AggregationCheck(qname, gname string, t time.Time, gracePeriod, maxDelay time.Duration, maxSize int) (aggregationSetID string, err error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return "", errRedisDown
|
|
}
|
|
return tb.real.AggregationCheck(qname, gname, t, gracePeriod, maxDelay, maxSize)
|
|
}
|
|
|
|
func (tb *TestBroker) ReadAggregationSet(qname, gname, aggregationSetID string) ([]*base.TaskMessage, time.Time, error) {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return nil, time.Time{}, errRedisDown
|
|
}
|
|
return tb.real.ReadAggregationSet(qname, gname, aggregationSetID)
|
|
}
|
|
|
|
func (tb *TestBroker) DeleteAggregationSet(ctx context.Context, qname, gname, aggregationSetID string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.DeleteAggregationSet(ctx, qname, gname, aggregationSetID)
|
|
}
|
|
|
|
func (tb *TestBroker) ReclaimStaleAggregationSets(qname string) error {
|
|
tb.mu.Lock()
|
|
defer tb.mu.Unlock()
|
|
if tb.sleeping {
|
|
return errRedisDown
|
|
}
|
|
return tb.real.ReclaimStaleAggregationSets(qname)
|
|
}
|