-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
108 lines (78 loc) · 2.85 KB
/
context_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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package sync
import (
"context"
"testing"
"github.com/stretchr/testify/require"
)
func Test_defaultExecutor(t *testing.T) {
t.Run("only default executor", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), ExecutorDefault, &unboundedExecutor{})
e := ContextExecutor(&ctx, "cpu")
require.IsType(t, &unboundedExecutor{}, e)
})
t.Run("default executor with named", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), ExecutorDefault, &unboundedExecutor{})
ctx = SetContextExecutor(ctx, "cpu", &queuedExecutor{})
e := ContextExecutor(&ctx, "cpu")
require.IsType(t, &queuedExecutor{}, e)
})
t.Run("no default executor with named", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "cpu", &queuedExecutor{})
e := ContextExecutor(&ctx, "cpu")
require.IsType(t, &queuedExecutor{}, e)
})
t.Run("no default executor with different named", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "cpu", &queuedExecutor{})
e := ContextExecutor(&ctx, "io")
require.IsType(t, serialExecutor{}, e)
})
t.Run("no executor", func(t *testing.T) {
ctx := context.Background()
e := ContextExecutor(&ctx, "io")
require.IsType(t, serialExecutor{}, e)
})
t.Run("no executor get default", func(t *testing.T) {
ctx := context.Background()
e := ContextExecutor(&ctx, ExecutorDefault)
require.IsType(t, serialExecutor{}, e)
})
t.Run("no context", func(t *testing.T) {
e := ContextExecutor(nil, "cpu")
require.IsType(t, serialExecutor{}, e)
})
t.Run("no context typed nil", func(t *testing.T) {
var ctx context.Context
e := ContextExecutor(&ctx, "cpu")
require.IsType(t, serialExecutor{}, e)
})
}
func Test_HasContextExecutor(t *testing.T) {
t.Run("WithExecutorInContext", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "cpu", &unboundedExecutor{})
require.True(t, HasContextExecutor(ctx, "cpu"))
})
t.Run("WithOtherExecutorInContext", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "cpu", &unboundedExecutor{})
require.False(t, HasContextExecutor(ctx, "io"))
})
}
func Test_ContextExecutor(t *testing.T) {
t.Run("WithExecutorInContext", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "cpu", &unboundedExecutor{})
result := ContextExecutor(&ctx, "cpu")
require.NotNil(t, result)
require.IsType(t, &unboundedExecutor{}, result)
})
t.Run("WithoutExecutorInContext", func(t *testing.T) {
ctx := context.Background()
result := ContextExecutor(&ctx, "cpu")
require.NotNil(t, result)
require.IsType(t, serialExecutor{}, result)
})
t.Run("WithDifferentExecutorInContext", func(t *testing.T) {
ctx := SetContextExecutor(context.Background(), "io", &unboundedExecutor{})
result := ContextExecutor(&ctx, "cpu")
require.NotNil(t, result)
require.IsType(t, serialExecutor{}, result)
})
}