-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinterfaces_test.go
More file actions
65 lines (60 loc) · 1.63 KB
/
Copy pathinterfaces_test.go
File metadata and controls
65 lines (60 loc) · 1.63 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
package routine
import (
"context"
"errors"
"testing"
)
func TestExecutorFunc(t *testing.T) {
called := false
f := ExecutorFunc(func(ctx context.Context) error {
called = true
return nil
})
if err := f.Execute(context.Background()); err != nil {
t.Fatal(err)
}
if !called {
t.Fatal("executor not called")
}
}
func TestExecutorFuncError(t *testing.T) {
want := errors.New("exec error")
f := ExecutorFunc(func(ctx context.Context) error { return want })
if got := f.Execute(context.Background()); got != want {
t.Fatalf("expected %v, got %v", want, got)
}
}
func TestUseExecutorMiddleware(t *testing.T) {
order := []int{}
base := ExecutorFunc(func(ctx context.Context) error {
order = append(order, 0)
return nil
})
m1 := ExecutorMiddleware(func(next Executor) Executor {
return ExecutorFunc(func(ctx context.Context) error {
order = append(order, 1)
return next.Execute(ctx)
})
})
m2 := ExecutorMiddleware(func(next Executor) Executor {
return ExecutorFunc(func(ctx context.Context) error {
order = append(order, 2)
return next.Execute(ctx)
})
})
exec := UseExecutorMiddleware(base, m1, m2)
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
// Middleware applied in reverse: m1 wraps m2 wraps base → order 1,2,0
if len(order) != 3 || order[0] != 1 || order[1] != 2 || order[2] != 0 {
t.Fatalf("unexpected call order: %v", order)
}
}
func TestUseExecutorMiddlewareNone(t *testing.T) {
base := ExecutorFunc(func(ctx context.Context) error { return nil })
exec := UseExecutorMiddleware(base)
if err := exec.Execute(context.Background()); err != nil {
t.Fatal(err)
}
}