forked from grpc-ecosystem/go-grpc-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_metrics_test.go
108 lines (105 loc) · 2.39 KB
/
client_metrics_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
package grpc_prometheus
import (
"context"
"testing"
prom "github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
)
func TestClientMetricsRaces(t *testing.T) {
m := NewClientMetrics()
tests := []struct {
desc string
f func(t *testing.T)
}{
{
desc: "EnableClientHandlingTimeHistogram",
f: func(t *testing.T) {
m.EnableClientHandlingTimeHistogram()
},
},
{
desc: "EnableClientStreamReceiveTimeHistogram",
f: func(t *testing.T) {
m.EnableClientStreamReceiveTimeHistogram()
},
},
{
desc: "EnableClientStreamSendTimeHistogram",
f: func(t *testing.T) {
m.EnableClientStreamSendTimeHistogram()
},
},
{
desc: "Describe",
f: func(t *testing.T) {
descriptor := make(chan *prom.Desc)
m.Describe(descriptor)
go func() {
for range descriptor {
}
}()
},
},
{
desc: "Collect",
f: func(t *testing.T) {
descriptor := make(chan prom.Metric)
m.Collect(descriptor)
go func() {
for range descriptor {
}
}()
},
},
{
desc: "UnaryClientInterceptor",
f: func(t *testing.T) {
var invoker grpc.UnaryInvoker = func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error {
return nil
}
err := m.UnaryClientInterceptor()(context.Background(), "SomeMethod", 1, 2, nil, invoker)
require.NoError(t, err)
},
},
{
desc: "StreamClientInterceptor",
f: func(t *testing.T) {
var invoker grpc.Streamer = func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, opts ...grpc.CallOption) (grpc.ClientStream, error) {
return nil, nil
}
desc := &grpc.StreamDesc{
StreamName: "SomeStream",
Handler: func(srv interface{}, stream grpc.ServerStream) error {
return nil
},
ServerStreams: true,
ClientStreams: true,
}
_, err := m.StreamClientInterceptor()(context.Background(), desc, nil, "MethodName", invoker)
require.NoError(t, err)
},
},
}
wait := make(chan struct{}, len(tests))
ch := make(chan struct{})
const (
goroutines = 50
retries = 50
)
for i := 0; i < goroutines; i++ {
go func() {
wait <- struct{}{}
<-ch
for i := 0; i < retries; i++ {
for _, test := range tests {
test.f(t)
}
}
}()
}
for i := 0; i < goroutines; i++ {
<-wait
}
close(ch)
}