-
Notifications
You must be signed in to change notification settings - Fork 42
/
middleware_test.go
51 lines (40 loc) · 1.18 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
49
50
51
package faktory_worker
import (
"context"
"testing"
"time"
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
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
job := faktory.NewJob("blah", 1, 2)
ctx = jobContext(ctx, mgr.Pool, job)
assert.Nil(t, ctx.Value(EXAMPLE))
assert.EqualValues(t, 0, counter)
err = dispatch(ctx, mgr.middleware, job, blahFunc)
assert.NoError(t, err)
assert.EqualValues(t, 1, counter)
assert.Nil(t, ctx.Value(EXAMPLE))
}