-
Notifications
You must be signed in to change notification settings - Fork 2
/
testing.go
221 lines (191 loc) · 5.77 KB
/
testing.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package minimatch
import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/redis/rueidis"
"github.com/redis/rueidis/rueidislock"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
pb "github.com/castaneai/minimatch/gen/openmatch"
"github.com/castaneai/minimatch/gen/openmatch/openmatchconnect"
"github.com/castaneai/minimatch/pkg/statestore"
)
type TestServer struct {
mm *MiniMatch
frontend *TestFrontendServer
options *testServerOptions
}
type TestServerOption interface {
apply(opts *testServerOptions)
}
type TestServerOptionFunc func(*testServerOptions)
func (f TestServerOptionFunc) apply(opts *testServerOptions) {
f(opts)
}
func WithTestServerListenAddr(addr string) TestServerOption {
return TestServerOptionFunc(func(opts *testServerOptions) {
opts.frontendListenAddr = addr
})
}
func WithTestServerBackendTick(tick time.Duration) TestServerOption {
return TestServerOptionFunc(func(opts *testServerOptions) {
opts.backendTick = tick
})
}
func WithTestServerFrontendOptions(frontendOptions ...FrontendOption) TestServerOption {
return TestServerOptionFunc(func(opts *testServerOptions) {
opts.frontendOptions = frontendOptions
})
}
func WithTestServerBackendOptions(backendOptions ...BackendOption) TestServerOption {
return TestServerOptionFunc(func(opts *testServerOptions) {
opts.backendOptions = backendOptions
})
}
type testServerOptions struct {
frontendListenAddr string
frontendOptions []FrontendOption
backendTick time.Duration
backendOptions []BackendOption
}
func defaultTestServerOpts() *testServerOptions {
return &testServerOptions{
frontendListenAddr: "127.0.0.1:0", // random port
frontendOptions: nil,
backendTick: 1 * time.Second,
backendOptions: nil,
}
}
type TestFrontendServer struct {
sv *httptest.Server
}
func (ts *TestFrontendServer) Addr() string {
return ts.sv.Listener.Addr().String()
}
func (ts *TestFrontendServer) Dial(t *testing.T) openmatchconnect.FrontendServiceClient {
return openmatchconnect.NewFrontendServiceClient(ts.sv.Client(), ts.sv.URL)
}
func (ts *TestFrontendServer) Start(t *testing.T) {
ts.sv.Start()
waitForTCPServerReady(t, ts.Addr(), 10*time.Second)
}
func (ts *TestFrontendServer) Stop() {
ts.sv.Close()
}
func NewTestFrontendServer(t *testing.T, store statestore.FrontendStore, addr string, opts ...FrontendOption) *TestFrontendServer {
// start frontend
mux := http.NewServeMux()
mux.Handle(openmatchconnect.NewFrontendServiceHandler(NewFrontendService(store, opts...)))
lis, err := newLocalListener(addr)
if err != nil {
t.Fatalf("failed to listen TCP addr: %+v", err)
}
sv := &httptest.Server{
Listener: lis,
Config: &http.Server{Handler: h2c.NewHandler(mux, &http2.Server{})},
EnableHTTP2: true,
}
ts := &TestFrontendServer{
sv: sv,
}
t.Cleanup(func() { ts.Stop() })
return ts
}
func newLocalListener(addr string) (net.Listener, error) {
return net.Listen("tcp", addr)
}
// RunTestServer helps with integration tests using Open Match.
// It provides an Open Match Frontend equivalent API in the Go process using a random port.
func RunTestServer(t *testing.T, matchFunctions map[*pb.MatchProfile]MatchFunction, assigner Assigner, opts ...TestServerOption) *TestServer {
options := defaultTestServerOpts()
for _, o := range opts {
o.apply(options)
}
front, back, _ := NewStateStoreWithMiniRedis(t)
mm := NewMiniMatch(front, back)
for profile, mmf := range matchFunctions {
mm.AddMatchFunction(profile, mmf)
}
frontend := NewTestFrontendServer(t, front, options.frontendListenAddr)
ts := &TestServer{mm: mm, frontend: frontend, options: options}
// start backend
go func() {
if err := mm.StartBackend(context.Background(), assigner, options.backendTick, options.backendOptions...); err != nil {
t.Logf("error occured in minimatch backend: %+v", err)
}
}()
// start frontend
frontend.Start(t)
return ts
}
func (ts *TestServer) DialFrontend(t *testing.T) openmatchconnect.FrontendServiceClient {
return ts.frontend.Dial(t)
}
// TickBackend triggers a Director's Tick, which immediately calls Match Function and Assigner.
// This is useful for sleep-independent testing.
func (ts *TestServer) TickBackend() error {
return ts.mm.TickBackend(context.Background())
}
// FrontendAddr returns the address listening as frontend.
func (ts *TestServer) FrontendAddr() string {
return ts.frontend.Addr()
}
func waitForTCPServerReady(t *testing.T, addr string, timeout time.Duration) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
serverReady := make(chan struct{})
check := func() bool {
d := net.Dialer{Timeout: 100 * time.Millisecond}
conn, err := d.Dial("tcp", addr)
if err == nil {
_ = conn.Close()
return true
}
return false
}
go func() {
if check() {
close(serverReady)
return
}
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if check() {
close(serverReady)
return
}
case <-ctx.Done():
return
}
}
}()
select {
case <-serverReady:
case <-time.After(timeout):
t.Fatalf("timeout(%v) for TCP server ready listening on %s", timeout, addr)
}
}
func NewStateStoreWithMiniRedis(t *testing.T) (statestore.FrontendStore, statestore.BackendStore, *miniredis.Miniredis) {
mr := miniredis.RunT(t)
copt := rueidis.ClientOption{InitAddress: []string{mr.Addr()}, DisableCache: true}
redis, err := rueidis.NewClient(copt)
if err != nil {
t.Fatalf("failed to create redis client: %+v", err)
}
locker, err := rueidislock.NewLocker(rueidislock.LockerOption{
ClientOption: copt,
})
if err != nil {
t.Fatalf("failed to create rueidis locker: %+v", err)
}
redisStore := statestore.NewRedisStore(redis, locker)
return redisStore, redisStore, mr
}