forked from grpc-ecosystem/go-grpc-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared_test.go
95 lines (78 loc) · 2.76 KB
/
shared_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
package kit_test
import (
"bytes"
"encoding/json"
"io"
"testing"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/kit/ctxkit"
"context"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_testing "github.com/grpc-ecosystem/go-grpc-middleware/testing"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
)
var (
goodPing = &pb_testproto.PingRequest{Value: "something", SleepTimeMs: 9999}
)
type loggingPingService struct {
pb_testproto.TestServiceServer
}
func (s *loggingPingService) Ping(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.PingResponse, error) {
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
ctxkit.AddFields(ctx, []interface{}{"custom_field", "custom_value"}...)
level.Info(ctxkit.Extract(ctx)).Log("msg", "some ping")
return s.TestServiceServer.Ping(ctx, ping)
}
func (s *loggingPingService) PingError(ctx context.Context, ping *pb_testproto.PingRequest) (*pb_testproto.Empty, error) {
return s.TestServiceServer.PingError(ctx, ping)
}
func (s *loggingPingService) PingList(ping *pb_testproto.PingRequest, stream pb_testproto.TestService_PingListServer) error {
grpc_ctxtags.Extract(stream.Context()).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
ctxkit.AddFields(stream.Context(), []interface{}{"custom_field", "custom_value"}...)
level.Info(ctxkit.Extract(stream.Context())).Log("msg", "some pinglist")
return s.TestServiceServer.PingList(ping, stream)
}
func (s *loggingPingService) PingEmpty(ctx context.Context, empty *pb_testproto.Empty) (*pb_testproto.PingResponse, error) {
return s.TestServiceServer.PingEmpty(ctx, empty)
}
type kitBaseSuite struct {
*grpc_testing.InterceptorTestSuite
mutexBuffer *grpc_testing.MutexReadWriter
buffer *bytes.Buffer
logger log.Logger
}
func newKitBaseSuite(t *testing.T) *kitBaseSuite {
b := &bytes.Buffer{}
muB := grpc_testing.NewMutexReadWriter(b)
logger := log.NewJSONLogger(log.NewSyncWriter(muB))
return &kitBaseSuite{
logger: logger,
buffer: b,
mutexBuffer: muB,
InterceptorTestSuite: &grpc_testing.InterceptorTestSuite{
TestService: &loggingPingService{&grpc_testing.TestPingService{T: t}},
},
}
}
func (s *kitBaseSuite) SetupTest() {
s.mutexBuffer.Lock()
s.buffer.Reset()
s.mutexBuffer.Unlock()
}
func (s *kitBaseSuite) getOutputJSONs() []map[string]interface{} {
ret := make([]map[string]interface{}, 0)
dec := json.NewDecoder(s.mutexBuffer)
for {
var val map[string]interface{}
err := dec.Decode(&val)
if err == io.EOF {
break
}
if err != nil {
s.T().Fatalf("failed decoding output from go-kit JSON: %v", err)
}
ret = append(ret, val)
}
return ret
}