-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgrpc_test.go
105 lines (97 loc) · 2.77 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package debug
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"goa.design/clue/internal/testsvc"
"goa.design/clue/log"
)
func TestUnaryServerInterceptor(t *testing.T) {
var buf bytes.Buffer
ctx := log.Context(context.Background(), log.WithOutput(&buf), log.WithFormat(logKeyValsOnly))
cli, stop := testsvc.SetupGRPC(t,
testsvc.WithServerOptions(grpc.ChainUnaryInterceptor(
log.UnaryServerInterceptor(ctx, log.WithDisableCallLogging(), log.WithDisableCallID()),
UnaryServerInterceptor())),
testsvc.WithUnaryFunc(logUnaryMethod))
defer stop()
mux := http.NewServeMux()
MountDebugLogEnabler(mux)
ts := httptest.NewServer(mux)
defer ts.Close()
steps := []struct {
name string
on bool
off bool
wantLog string
}{
{"start", false, false, ""},
{"turn debug logs on", true, false, "debug=message "},
{"with debug logs on", false, false, "debug=message "},
{"turn debug logs off", false, true, ""},
{"with debug logs off", false, false, ""},
}
for _, step := range steps {
if step.on {
makeRequest(t, ts.URL+"/debug?debug-logs=on")
}
if step.off {
makeRequest(t, ts.URL+"/debug?debug-logs=off")
}
_, err := cli.GRPCMethod(context.Background(), nil)
assert.NoError(t, err)
assert.Equal(t, step.wantLog, buf.String())
buf.Reset()
}
}
func TestStreamServerInterceptor(t *testing.T) {
var buf bytes.Buffer
ctx := log.Context(context.Background(), log.WithOutput(&buf), log.WithFormat(logKeyValsOnly))
cli, stop := testsvc.SetupGRPC(t,
testsvc.WithServerOptions(grpc.ChainStreamInterceptor(
log.StreamServerInterceptor(ctx, log.WithDisableCallLogging(), log.WithDisableCallID()),
StreamServerInterceptor())),
testsvc.WithStreamFunc(echoMethod))
defer stop()
steps := []struct {
name string
enableDebugLogs bool
wantLog string
}{
{"no debug logs", false, ""},
{"debug logs", true, "debug=message "},
{"revert to no debug logs", false, ""},
}
for _, step := range steps {
debugLogs = step.enableDebugLogs
stream, err := cli.GRPCStream(context.Background())
assert.NoError(t, err)
defer stream.Close()
err = stream.Send(&testsvc.Fields{})
assert.NoError(t, err)
_, err = stream.Recv()
assert.NoError(t, err)
assert.Equal(t, step.wantLog, buf.String())
buf.Reset()
}
}
func logUnaryMethod(ctx context.Context, _ *testsvc.Fields) (*testsvc.Fields, error) {
log.Debug(ctx, log.KV{K: "debug", V: "message"})
return &testsvc.Fields{}, nil
}
func echoMethod(ctx context.Context, stream testsvc.Stream) (err error) {
for {
_, err := stream.Recv()
if err != nil {
return err
}
log.Debug(ctx, log.KV{K: "debug", V: "message"})
if err = stream.Send(&testsvc.Fields{}); err != nil {
return err
}
}
}