-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_util.go
More file actions
201 lines (174 loc) · 3.32 KB
/
test_util.go
File metadata and controls
201 lines (174 loc) · 3.32 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package server
import (
"context"
"testing"
// "time"
"crypto/rand"
"encoding/hex"
"fmt"
"net/http"
"os"
"sync"
"time"
_ "net/http/pprof" // Import for side effects
"github.com/urnetwork/glog"
)
// each test runs with its own postgres and redis db
// the database is dropped at the end of the test
var pprofServer = sync.OnceFunc(func() {
go func() {
http.ListenAndServe(":6060", nil)
}()
// e.g. `go tool pprof http://127.0.0.1:6060/debug/pprof/profile`
})
type TestEnv struct {
ApplyDbMigrations bool
Warmup bool
RerunCount int
RerunTimeout time.Duration
}
func DefaultTestEnv() *TestEnv {
return &TestEnv{
ApplyDbMigrations: true,
Warmup: false,
RerunCount: 1,
RerunTimeout: 2 * time.Second,
}
}
// in each test file, `func TestMain(m *testing.M) {(&server.TestEnv{}).TestMain(m)}`
// https://pkg.go.dev/testing
func (self *TestEnv) TestMain(m *testing.M) {
teardown := self.setup()
defer teardown()
code := m.Run()
defer os.Exit(code)
}
func (self *TestEnv) Run(callback func()) {
n := self.RerunCount + 1
for i := 0; i < n; i += 1 {
var r any
func() {
if i+1 < n {
defer func() {
r = recover()
}()
}
teardown := self.setup()
defer teardown()
callback()
}()
if r == nil {
if 0 < i {
glog.Infof("[FLAKY]test passed iteration[%d/%d]", i+i, n, r)
}
return
}
// glog.Infof("[FLAKY]test failed iteration[%d/%d]. err = %s", i+i, n, r)
select {
case <-time.After(self.RerunTimeout):
}
}
}
func (self *TestEnv) setup() func() {
pprofServer()
Reset()
// tests are allowed only in the `local` env
env := RequireEnv()
if env != "local" {
panic(fmt.Errorf("Can only run tests in the local env (%s)", env))
}
ctx := context.Background()
pg := Vault.RequireSimpleResource("pg.yml").Parse()
redis := Vault.RequireSimpleResource("redis.yml").Parse()
bytes := make([]byte, 16)
_, err := rand.Read(bytes)
Raise(err)
testPgDbName := fmt.Sprintf(
"test_%d_%s",
NowUtc().UnixMilli(),
hex.EncodeToString(bytes),
)
testRedisDb := 10
Db(ctx, func(conn PgConn) {
_, err := conn.Exec(
ctx,
fmt.Sprintf(
`
CREATE DATABASE %s
WITH
OWNER=%s
ENCODING=UTF8
LOCALE='en_US.UTF-8'
`,
testPgDbName,
pg["user"],
),
)
Raise(err)
}, OptReadWrite())
popPg := Vault.PushSimpleResource(
"pg.yml",
[]byte(fmt.Sprintf(
`
authority: "%s"
user: "%s"
password: "%s"
db: "%s"`,
pg["authority"],
pg["user"],
pg["password"],
testPgDbName,
)),
)
PgReset()
popRedis := Vault.PushSimpleResource(
"redis.yml",
[]byte(fmt.Sprintf(
`
authority: "%s"
password: "%s"
db: %d
cluster: %t`,
redis["authority"],
redis["password"],
testRedisDb,
false,
)),
)
RedisReset()
Redis(ctx, func(client RedisClient) {
cmd := client.FlushDB(ctx)
_, err := cmd.Result()
Raise(err)
})
if self.ApplyDbMigrations {
ApplyDbMigrations(ctx)
}
if self.Warmup {
Warmup()
}
return func() {
Reset()
Redis(ctx, func(client RedisClient) {
cmd := client.FlushDB(ctx)
_, err := cmd.Result()
Raise(err)
})
popRedis()
RedisReset()
popPg()
PgReset()
Db(ctx, func(conn PgConn) {
_, err := conn.Exec(
ctx,
fmt.Sprintf(
`
DROP DATABASE %s
`,
testPgDbName,
),
)
Raise(err)
}, OptReadWrite())
}
}