diff --git a/CHANGELOG.md b/CHANGELOG.md index b9e1541..36f742d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,11 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [0.22.0] - 2022-01-29 - ### Added -- `BaseContext` introduced in `Config` to specify callback hook to provide a base `context` from which `Handler` `context` is derived +- `BaseContext` is introduced in `Config` to specify callback hook to provide a base `context` from which `Handler` `context` is derived ## [0.21.0] - 2022-01-22 diff --git a/internal/context/context_test.go b/internal/context/context_test.go index ce11a69..3befdba 100644 --- a/internal/context/context_test.go +++ b/internal/context/context_test.go @@ -54,10 +54,15 @@ func TestCreateContextWithFutureDeadline(t *testing.T) { } func TestCreateContextWithBaseContext(t *testing.T) { + type ctxKey string + type ctxValue string + var key ctxKey = "key" + var value ctxValue = "value" + tests := []struct { - deadline time.Time + baseCtx context.Context }{ - {time.Now().Add(-2 * time.Hour)}, + {context.WithValue(context.Background(), key, value)}, } for _, tc := range tests { @@ -66,13 +71,8 @@ func TestCreateContextWithBaseContext(t *testing.T) { ID: uuid.NewString(), Payload: nil, } - type ctxKey string - type ctxValue string - var key ctxKey = "key" - var value ctxValue = "value" - baseCtx := context.WithValue(context.Background(), key, value) - ctx, cancel := New(baseCtx, msg, tc.deadline) + ctx, cancel := New(tc.baseCtx, msg, time.Now().Add(30*time.Minute)) defer cancel() select { @@ -82,10 +82,11 @@ func TestCreateContextWithBaseContext(t *testing.T) { } v, ok := ctx.Value(key).(ctxValue) + original, _ := tc.baseCtx.Value(key).(ctxValue) if !ok { t.Errorf("ctx.Value().(ctxValue) returned false, expected to be true") } - if v != value { + if v != original { t.Errorf("ctx.Value().(ctxValue) returned unknown value (%v), expected to be %s", v, value) } } diff --git a/processor.go b/processor.go index b7d7824..2ad9687 100644 --- a/processor.go +++ b/processor.go @@ -27,7 +27,7 @@ type processor struct { broker base.Broker handler Handler - baseCtxFn BaseCtxFn + baseCtxFn func() context.Context queueConfig map[string]int @@ -72,7 +72,7 @@ type processor struct { type processorParams struct { logger *log.Logger broker base.Broker - baseCtxFn BaseCtxFn + baseCtxFn func() context.Context retryDelayFunc RetryDelayFunc isFailureFunc func(error) bool syncCh chan<- *syncRequest diff --git a/server.go b/server.go index ca0b821..c1be0d9 100644 --- a/server.go +++ b/server.go @@ -101,10 +101,7 @@ type Config struct { // // If BaseContext is nil, the default is context.Background(). // If this is defined, then it MUST return a non-nil context - BaseContext BaseCtxFn - - // SleepOnEmptyQueue optionally specifies the amount of time to wait before polling again when there are no messages in the queue - SleepOnEmptyQueue time.Duration + BaseContext func() context.Context // Function to calculate retry delay for a failed task. // @@ -212,9 +209,6 @@ func (fn ErrorHandlerFunc) HandleError(ctx context.Context, task *Task, err erro fn(ctx, task, err) } -// BaseCtxFn provides the root context from where the execution contexts of tasks are derived -type BaseCtxFn func() context.Context - // RetryDelayFunc calculates the retry delay duration for a failed task given // the retry count, error, and the task. //