2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 11:05:58 +08:00

Fix tests

This commit is contained in:
Ken Hibino 2022-02-16 06:51:09 -08:00
parent ebd7a32c0f
commit 5490d2c625
2 changed files with 22 additions and 11 deletions

View File

@ -6,6 +6,7 @@ package context
import ( import (
"context" "context"
"fmt"
"testing" "testing"
"time" "time"
@ -60,9 +61,22 @@ func TestCreateContextWithBaseContext(t *testing.T) {
var value ctxValue = "value" var value ctxValue = "value"
tests := []struct { tests := []struct {
baseCtx context.Context baseCtx context.Context
validate func(ctx context.Context, t *testing.T) error
}{ }{
{context.WithValue(context.Background(), key, value)}, {
baseCtx: context.WithValue(context.Background(), key, value),
validate: func(ctx context.Context, t *testing.T) error {
got, ok := ctx.Value(key).(ctxValue)
if !ok {
return fmt.Errorf("ctx.Value().(ctxValue) returned false, expected to be true")
}
if want := value; got != want {
return fmt.Errorf("ctx.Value().(ctxValue) returned unknown value (%v), expected to be %s", got, value)
}
return nil
},
},
} }
for _, tc := range tests { for _, tc := range tests {
@ -76,18 +90,13 @@ func TestCreateContextWithBaseContext(t *testing.T) {
defer cancel() defer cancel()
select { select {
case <-ctx.Done(): case x := <-ctx.Done():
t.Errorf("<-ctx.Done() == %v, want nothing (it should block)", x)
default: default:
t.Errorf("ctx.Done() blocked, want it to be non-blocking")
} }
v, ok := ctx.Value(key).(ctxValue) if err := tc.validate(ctx, t); err != nil {
original, _ := tc.baseCtx.Value(key).(ctxValue) t.Errorf("%v", err)
if !ok {
t.Errorf("ctx.Value().(ctxValue) returned false, expected to be true")
}
if v != original {
t.Errorf("ctx.Value().(ctxValue) returned unknown value (%v), expected to be %s", v, value)
} }
} }
} }

View File

@ -61,6 +61,7 @@ func newProcessorForTest(t *testing.T, r *rdb.RDB, h Handler) *processor {
p := newProcessor(processorParams{ p := newProcessor(processorParams{
logger: testLogger, logger: testLogger,
broker: r, broker: r,
baseCtxFn: context.Background,
retryDelayFunc: DefaultRetryDelayFunc, retryDelayFunc: DefaultRetryDelayFunc,
isFailureFunc: defaultIsFailureFunc, isFailureFunc: defaultIsFailureFunc,
syncCh: syncCh, syncCh: syncCh,
@ -592,6 +593,7 @@ func TestProcessorWithStrictPriority(t *testing.T) {
p := newProcessor(processorParams{ p := newProcessor(processorParams{
logger: testLogger, logger: testLogger,
broker: rdbClient, broker: rdbClient,
baseCtxFn: context.Background,
retryDelayFunc: DefaultRetryDelayFunc, retryDelayFunc: DefaultRetryDelayFunc,
isFailureFunc: defaultIsFailureFunc, isFailureFunc: defaultIsFailureFunc,
syncCh: syncCh, syncCh: syncCh,