-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_middleware_test.go
More file actions
90 lines (81 loc) · 1.59 KB
/
Copy pathqueue_middleware_test.go
File metadata and controls
90 lines (81 loc) · 1.59 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cq
import (
"context"
"reflect"
"sync"
"testing"
)
func TestQueueMiddleware_Order(t *testing.T) {
var (
mu sync.Mutex
seq []string
)
push := func(v string) {
mu.Lock()
seq = append(seq, v)
mu.Unlock()
}
m1 := func(next Job) Job {
return func(ctx context.Context) error {
push("m1-pre")
err := next(ctx)
push("m1-post")
return err
}
}
m2 := func(next Job) Job {
return func(ctx context.Context) error {
push("m2-pre")
err := next(ctx)
push("m2-post")
return err
}
}
q := NewQueue(1, 1, 10, WithMiddleware(m1, m2))
q.Start()
mustSubmit(t, q, func(ctx context.Context) error {
push("job")
return nil
})
q.Stop(true)
got := seq
want := []string{"m1-pre", "m2-pre", "job", "m2-post", "m1-post"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("middleware order mismatch: got %v, want %v", got, want)
}
}
func TestQueueMiddleware_AppendsAcrossOptions(t *testing.T) {
var (
mu sync.Mutex
seq []string
)
push := func(v string) {
mu.Lock()
seq = append(seq, v)
mu.Unlock()
}
m1 := func(next Job) Job {
return func(ctx context.Context) error {
push("m1")
return next(ctx)
}
}
m2 := func(next Job) Job {
return func(ctx context.Context) error {
push("m2")
return next(ctx)
}
}
q := NewQueue(1, 1, 10, WithMiddleware(m1), WithMiddleware(nil, m2))
q.Start()
mustSubmit(t, q, func(ctx context.Context) error {
push("job")
return nil
})
q.Stop(true)
got := seq
want := []string{"m1", "m2", "job"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("middleware append mismatch: got %v, want %v", got, want)
}
}