-
Notifications
You must be signed in to change notification settings - Fork 14
/
testing_gorums.go
60 lines (56 loc) · 1.5 KB
/
testing_gorums.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
package gorums
import (
"net"
"testing"
"time"
)
// ServerIface is the interface that must be implemented by a server in order to support the TestSetup function.
type ServerIface interface {
Serve(net.Listener) error
Stop()
}
// TestSetup starts numServers gRPC servers using the given registration
// function, and returns the server addresses along with a stop function
// that should be called to shut down the test.
// This function can be used by other packages for testing purposes.
func TestSetup(t testing.TB, numServers int, srvFn func(i int) ServerIface) ([]string, func()) {
t.Helper()
servers := make([]ServerIface, numServers)
addrs := make([]string, numServers)
for i := 0; i < numServers; i++ {
srv := srvFn(i)
// listen on any available port
lis, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("Failed to listen on port: %v", err)
}
addrs[i] = lis.Addr().String()
servers[i] = srv
go func() { _ = srv.Serve(lis) }()
}
stopFn := func() {
for _, srv := range servers {
srv.Stop()
}
}
return addrs, stopFn
}
func testServerSetup(t testing.TB, addr string, srv ServerIface) (func(), func()) {
t.Helper()
var lis net.Listener
var err error
startFn := func() {
lis, err = net.Listen("tcp", addr)
if err != nil {
t.Fatalf("Failed to listen on port: %v", err)
}
go func() { _ = srv.Serve(lis) }()
// to ensure that the server has started
time.Sleep(100 * time.Millisecond)
}
stopFn := func() {
lis.Close()
srv.Stop()
}
return startFn, stopFn
}