-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
159 lines (123 loc) · 3.8 KB
/
example_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
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
package service_test
import (
"context"
"errors"
"io"
"testing"
"github.com/ankorstore/yokai-grpc-template/internal"
"github.com/ankorstore/yokai-grpc-template/proto"
"github.com/ankorstore/yokai/grpcserver/grpcservertest"
"github.com/ankorstore/yokai/log/logtest"
"github.com/ankorstore/yokai/trace/tracetest"
"github.com/stretchr/testify/assert"
"go.uber.org/fx"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func TestExampleUnary(t *testing.T) {
var connFactory grpcservertest.TestBufconnConnectionFactory
var logBuffer logtest.TestLogBuffer
var traceExporter tracetest.TestTraceExporter
internal.RunTest(t, fx.Populate(&connFactory, &logBuffer, &traceExporter))
// conn preparation
conn, err := connFactory.Create(
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
assert.NoError(t, err)
defer func() {
err = conn.Close()
assert.NoError(t, err)
}()
// client preparation
assert.NoError(t, err)
client := proto.NewExampleServiceClient(conn)
// call
response, err := client.ExampleUnary(context.Background(), &proto.ExampleRequest{
Text: "test",
})
assert.NoError(t, err)
// response assertions
assert.Equal(t, "response from grpc-app: you sent test", response.Text)
// logs assertions
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "info",
"message": "received: test",
})
// traces assertions
tracetest.AssertHasTraceSpan(t, traceExporter, "ExampleUnary")
}
func TestExampleStreaming(t *testing.T) {
var connFactory grpcservertest.TestBufconnConnectionFactory
var logBuffer logtest.TestLogBuffer
var traceExporter tracetest.TestTraceExporter
internal.RunTest(t, fx.Populate(&connFactory, &logBuffer, &traceExporter))
// conn preparation
conn, err := connFactory.Create(
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
assert.NoError(t, err)
defer func() {
err = conn.Close()
assert.NoError(t, err)
}()
// client preparation
assert.NoError(t, err)
client := proto.NewExampleServiceClient(conn)
// send
stream, err := client.ExampleStreaming(context.Background())
assert.NoError(t, err)
for _, text := range []string{"this", "is", "a", "test"} {
err = stream.Send(&proto.ExampleRequest{
Text: text,
})
assert.NoError(t, err)
}
err = stream.CloseSend()
assert.NoError(t, err)
// receive
var responses []*proto.ExampleResponse
wait := make(chan struct{})
go func() {
for {
resp, err := stream.Recv()
if errors.Is(err, io.EOF) {
break
}
assert.NoError(t, err)
responses = append(responses, resp)
}
close(wait)
}()
<-wait
// responses assertions
assert.Len(t, responses, 4)
assert.Equal(t, "response from grpc-app: you sent this", responses[0].Text)
assert.Equal(t, "response from grpc-app: you sent is", responses[1].Text)
assert.Equal(t, "response from grpc-app: you sent a", responses[2].Text)
assert.Equal(t, "response from grpc-app: you sent test", responses[3].Text)
// logs assertions
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "info",
"message": "received: this",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "info",
"message": "received: is",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "info",
"message": "received: a",
})
logtest.AssertHasLogRecord(t, logBuffer, map[string]interface{}{
"level": "info",
"message": "received: test",
})
// traces assertions
tracetest.AssertHasTraceSpan(t, traceExporter, "ExampleStreaming")
span, err := traceExporter.Span("ExampleStreaming")
assert.NoError(t, err)
assert.Equal(t, "received: this", span.Events[0].Name)
assert.Equal(t, "received: is", span.Events[1].Name)
assert.Equal(t, "received: a", span.Events[2].Name)
assert.Equal(t, "received: test", span.Events[3].Name)
}