forked from googleapis/google-cloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrpc_test.go
180 lines (160 loc) · 5.3 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package trace
import (
"context"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"testing"
pb "cloud.google.com/go/trace/testdata/helloworld"
"google.golang.org/grpc"
)
func TestGRPCInterceptors(t *testing.T) {
t.Skip("hangs forever for go < 1.9")
tc := newTestClient(&noopTransport{})
// default sampling with global=1.
parent := tc.SpanFromHeader("parent", "7f27601f17b7a2873739efd18ff83872/123;o=1")
testGRPCInterceptor(t, tc, parent, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if got, want := in.TraceID(), out.TraceID(); got != want {
t.Errorf("incoming call is not tracing the outgoing trace; TraceID = %q; want %q", got, want)
}
if !in.Traced() {
t.Errorf("incoming span is not traced; want traced")
}
})
// default sampling with global=0.
parent = tc.SpanFromHeader("parent", "7f27601f17b7a2873739efd18ff83872/123;o=0")
testGRPCInterceptor(t, tc, parent, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if got, want := in.TraceID(), out.TraceID(); got != want {
t.Errorf("incoming call is not tracing the outgoing trace; TraceID = %q; want %q", got, want)
}
if in.Traced() {
t.Errorf("incoming span is traced; want not traced")
}
})
// sampling all with global=1.
all, _ := NewLimitedSampler(1.0, 1<<32)
tc.SetSamplingPolicy(all)
parent = tc.SpanFromHeader("parent", "7f27601f17b7a2873739efd18ff83872/123;o=1")
testGRPCInterceptor(t, tc, parent, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if got, want := in.TraceID(), out.TraceID(); got != want {
t.Errorf("incoming call is not tracing the outgoing trace; TraceID = %q; want %q", got, want)
}
if !in.Traced() {
t.Errorf("incoming span is not traced; want traced")
}
})
// sampling none with global=1.
none, _ := NewLimitedSampler(0, 0)
tc.SetSamplingPolicy(none)
parent = tc.SpanFromHeader("parent", "7f27601f17b7a2873739efd18ff83872/123;o=1")
testGRPCInterceptor(t, tc, parent, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if got, want := in.TraceID(), out.TraceID(); got != want {
t.Errorf("incoming call is not tracing the outgoing trace; TraceID = %q; want %q", got, want)
}
if in.Traced() {
t.Errorf("incoming span is traced; want not traced")
}
})
// sampling all with no parent span.
tc.SetSamplingPolicy(all)
testGRPCInterceptor(t, tc, nil, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if in.TraceID() == "" {
t.Errorf("incoming call TraceID is empty")
}
if !in.Traced() {
t.Errorf("incoming span is not traced; want traced")
}
})
// sampling none with no parent span.
tc.SetSamplingPolicy(none)
testGRPCInterceptor(t, tc, nil, func(t *testing.T, out, in *Span) {
if in == nil {
t.Fatalf("missing span in the incoming context")
}
if in.TraceID() == "" {
t.Errorf("incoming call TraceID is empty")
}
if in.Traced() {
t.Errorf("incoming span is traced; want not traced")
}
})
}
func testGRPCInterceptor(t *testing.T, tc *Client, parent *Span, assert func(t *testing.T, out, in *Span)) {
incomingCh := make(chan *Span, 1)
addrCh := make(chan net.Addr, 1)
go func() {
lis, err := net.Listen("tcp", "")
if err != nil {
t.Errorf("Failed to listen: %v", err)
}
addrCh <- lis.Addr()
s := grpc.NewServer(grpc.UnaryInterceptor(tc.GRPCServerInterceptor()))
pb.RegisterGreeterServer(s, &grpcServer{
fn: func(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
incomingCh <- FromContext(ctx)
return &pb.HelloReply{}, nil
},
})
if err := s.Serve(lis); err != nil {
t.Errorf("Failed to serve: %v", err)
}
}()
addr := <-addrCh
conn, err := grpc.Dial(addr.String(), grpc.WithInsecure(), grpc.WithBlock(), grpc.WithUnaryInterceptor(tc.GRPCClientInterceptor()))
if err != nil {
t.Fatalf("Did not connect: %v", err)
}
defer conn.Close()
c := pb.NewGreeterClient(conn)
outgoingCtx := NewContext(context.Background(), parent)
_, err = c.SayHello(outgoingCtx, &pb.HelloRequest{})
if err != nil {
log.Fatalf("Could not SayHello: %v", err)
}
assert(t, parent, <-incomingCh)
}
type noopTransport struct{}
func (rt *noopTransport) RoundTrip(req *http.Request) (*http.Response, error) {
resp := &http.Response{
Status: "200 OK",
StatusCode: 200,
Body: ioutil.NopCloser(strings.NewReader("{}")),
}
return resp, nil
}
type grpcServer struct {
fn func(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error)
}
func (s *grpcServer) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return s.fn(ctx, in)
}