forked from contribsys/faktory_worker_go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_test.go
48 lines (37 loc) · 1.08 KB
/
middleware_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package faktory_worker
import (
"context"
"testing"
faktory "github.com/contribsys/faktory/client"
"github.com/stretchr/testify/assert"
)
type middlewareValue string
var EXAMPLE = middlewareValue("a")
func TestMiddleware(t *testing.T) {
mgr := NewManager()
pool, err := faktory.NewPool(2)
assert.NoError(t, err)
mgr.Pool = pool
mgr.Use(func(ctx context.Context, job *faktory.Job, next func(context.Context) error) error {
modctx := context.WithValue(ctx, EXAMPLE, 4.0)
return next(modctx)
})
counter := 0
blahFunc := func(ctx context.Context, job *faktory.Job) error {
assert.EqualValues(t, 4.0, ctx.Value(EXAMPLE))
help := HelperFor(ctx)
assert.Equal(t, job.Jid, help.Jid())
assert.Equal(t, job.Type, help.JobType())
assert.Equal(t, "", help.Bid())
counter += 1
return nil
}
job := faktory.NewJob("blah", 1, 2)
ctx := jobContext(mgr.Pool, job)
assert.Nil(t, ctx.Value(EXAMPLE))
assert.EqualValues(t, 0, counter)
err = dispatch(mgr.middleware, ctx, job, blahFunc)
assert.NoError(t, err)
assert.EqualValues(t, 1, counter)
assert.Nil(t, ctx.Value(EXAMPLE))
}