-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_test.go
132 lines (124 loc) · 2.73 KB
/
test_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package ntest_test
import (
"sync"
"testing"
"time"
"github.com/muir/nject"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/memsql/ntest"
)
func TestRun(t *testing.T) {
t.Parallel()
var called bool
ntest.RunTest(t,
func() string { return "xyz" },
func(t *testing.T) {
require.True(t, true)
},
func(t ntest.T) {
require.True(t, true)
},
func(s string) {
called = s == "xyz"
},
)
assert.True(t, called)
}
func TestParallelMatrix(t *testing.T) {
var mu sync.Mutex
doneA := make(chan struct{})
doneB := make(chan struct{})
testsRun := make(map[string]struct{})
ntest.RunParallelMatrix(t,
func() int { return 7 },
map[string]nject.Provider{
"testA": nject.Provide("testA",
func(t ntest.T) (string, chan struct{}) {
return t.Name(), doneA
}),
"testB": nject.Sequence("testB",
func(t ntest.T, _ int) string { return t.Name() },
func(t ntest.T) chan struct{} {
return doneB
},
),
},
func(t *testing.T, s string, c chan struct{}) {
t.Logf("final func for %s", t.Name())
t.Logf("s = %s", s)
mu.Lock()
defer mu.Unlock()
testsRun[s] = struct{}{}
close(c)
},
)
t.Run("validate", func(t *testing.T) {
t.Parallel()
select {
case <-doneA:
case <-time.After(time.Second):
require.False(t, true, "timeout")
}
select {
case <-doneB:
case <-time.After(time.Second):
require.False(t, true, "timeout")
}
assert.Equal(t, map[string]struct{}{
"TestParallelMatrix/testA": {},
"TestParallelMatrix/testB": {},
}, testsRun)
})
}
func TestMatrix(t *testing.T) {
t.Parallel()
testsRun := make(map[string]struct{})
ntest.RunMatrix(t,
func() int { return 7 },
map[string]nject.Provider{
"testA": nject.Provide("testA", func(t ntest.T) string { return t.Name() }),
"testB": nject.Sequence("testB",
func(t ntest.T, _ int) string { return t.Name() },
),
},
func(t *testing.T, s string) {
t.Logf("final func for %s", t.Name())
t.Logf("s = %s", s)
testsRun[s] = struct{}{}
},
)
assert.Equal(t, map[string]struct{}{
"TestMatrix/testA": {},
"TestMatrix/testB": {},
}, testsRun)
}
func TestExtra(t *testing.T) {
t.Parallel()
var a int
var b int
var c int
baseSequence := nject.Sequence("base",
nject.Provide("string", func() string { return "abc" }),
func() int { return 7 },
)
ntest.RunTest(t,
baseSequence,
ntest.Extra(func(s string) int { return len(s) }, &a),
ntest.Extra(func(s string) int { return len(s) + 1 }, &b),
func() {
c = a + b
},
)
assert.Equal(t, 7, c)
}
func TestEmptyMatrix(t *testing.T) {
t.Skip("this test is expected to fail")
t.Parallel()
ntest.RunMatrix(t,
func() int { return 7 },
func(t *testing.T, i int) {
assert.Equal(t, 7, i)
},
)
}