-
Notifications
You must be signed in to change notification settings - Fork 21
/
grpc_test.go
91 lines (78 loc) · 2.26 KB
/
grpc_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
package openmock
import (
"context"
"fmt"
"net"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"github.com/checkr/openmock/demo_protobuf"
)
// arbitraryPort returns a non-used port.
func arbitraryPort() int {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer l.Close()
addr := l.Addr().String()
_, port, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
p, _ := strconv.Atoi(port)
return p
}
var (
testGRPCHost = "127.0.0.1"
testGRPCPort = arbitraryPort()
demoGRPCServiceMap = map[string]GRPCService{
"demo_protobuf.ExampleService": {
"ExampleMethod": GRPCRequestResponsePair{
Request: &demo_protobuf.ExampleRequest{},
Response: &demo_protobuf.ExampleResponse{},
},
},
}
)
// need to have a running grpc server on openmock to run this test
func TestGRPCServer(t *testing.T) {
grpcaddress := fmt.Sprintf("%s:%d", testGRPCHost, testGRPCPort)
om := &OpenMock{}
om.SetupRepo()
om.TemplatesDir = "./demo_templates"
om.GRPCPort = testGRPCPort
om.GRPCHost = testGRPCHost
om.GRPCServiceMap = demoGRPCServiceMap
err := om.Load()
assert.NoError(t, err)
go om.startGRPC()
t.Run("happy code path", func(t *testing.T) {
// Set up a connection to the server.
conn, err := grpc.Dial(grpcaddress, grpc.WithInsecure(), grpc.WithBlock())
assert.NoError(t, err)
defer conn.Close()
c := demo_protobuf.NewExampleServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.ExampleMethod(ctx, &demo_protobuf.ExampleRequest{Two: "success"})
assert.NoError(t, err)
assert.Equal(t, 227, int(r.GetCode()))
assert.Equal(t, "success", r.GetMessage())
})
t.Run("ok with condition", func(t *testing.T) {
// Set up a connection to the server.
conn, err := grpc.Dial(grpcaddress, grpc.WithInsecure(), grpc.WithBlock())
assert.NoError(t, err)
defer conn.Close()
c := demo_protobuf.NewExampleServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.ExampleMethod(ctx, &demo_protobuf.ExampleRequest{Two: "zzzz"})
assert.NoError(t, err)
assert.Equal(t, 200, int(r.GetCode()))
assert.Equal(t, "yyyy", r.GetMessage())
})
}