/
clitest.go
66 lines (59 loc) · 1.73 KB
/
clitest.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
package clitest
import (
"context"
"fmt"
"net"
"testing"
"github.com/modernice/goes/codec"
"github.com/modernice/goes/event"
"github.com/modernice/goes/event/eventbus"
"github.com/modernice/goes/event/eventstore"
"google.golang.org/grpc"
"google.golang.org/grpc/test/bufconn"
)
// SetupEvents returns an event registry, bus and store.
func SetupEvents() (*codec.Registry, event.Bus, event.Store) {
reg := event.NewRegistry()
bus := eventbus.New()
store := eventstore.New()
return reg, bus, store
}
// NewServer returns a new *grpc.Server and a *grpc.ClientConn that is connected
// to the returned *bufconn.Listener.
func NewServer(t *testing.T, init func(*grpc.Server)) (*grpc.Server, *grpc.ClientConn, *bufconn.Listener) {
srv, lis := newServer()
if init != nil {
init(srv)
}
return srv, ClientConn(t, lis), lis
}
// NewRunningServer returns the same as NewServer, but also starts the server in a new goroutine.
func NewRunningServer(t *testing.T, init func(*grpc.Server)) (*grpc.Server, *grpc.ClientConn, *bufconn.Listener) {
srv, conn, lis := NewServer(t, init)
go func() {
if err := srv.Serve(lis); err != nil {
panic(err)
}
}()
return srv, conn, lis
}
// ClientConn returns a *grpc.ClientConn that dials using the provided
// *bufconn.Listener.
func ClientConn(t *testing.T, lis *bufconn.Listener) *grpc.ClientConn {
conn, err := grpc.DialContext(
context.Background(), "",
grpc.WithInsecure(),
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}),
)
if err != nil {
t.Fatal(fmt.Errorf("grpc.DialContext: %w", err))
}
return conn
}
func newServer() (*grpc.Server, *bufconn.Listener) {
lis := bufconn.Listen(1024 * 1024)
srv := grpc.NewServer()
return srv, lis
}