-
Notifications
You must be signed in to change notification settings - Fork 0
/
chainstats_test.go
69 lines (56 loc) · 1.69 KB
/
chainstats_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
package chainstats
import (
"context"
"testing"
"google.golang.org/grpc/stats"
)
type mockStatsHandler struct {
tagRPCCalled bool
handleRPCCalled bool
tagConnCalled bool
handleConnCalled bool
}
func (m *mockStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
m.tagRPCCalled = true
return ctx
}
func (m *mockStatsHandler) HandleRPC(ctx context.Context, stat stats.RPCStats) {
m.handleRPCCalled = true
}
func (m *mockStatsHandler) TagConn(ctx context.Context, info *stats.ConnTagInfo) context.Context {
m.tagConnCalled = true
return ctx
}
func (m *mockStatsHandler) HandleConn(ctx context.Context, stat stats.ConnStats) {
m.handleConnCalled = true
}
func TestChainStatsHandler(t *testing.T) {
handler1 := &mockStatsHandler{}
handler2 := &mockStatsHandler{}
chainHandler := NewChainStatsHandler(handler1, handler2)
ctx := context.Background()
info := &stats.RPCTagInfo{}
stat := &stats.InPayload{}
// Test TagRPC
ctx = chainHandler.TagRPC(ctx, info)
if !handler1.tagRPCCalled || !handler2.tagRPCCalled {
t.Errorf("TagRPC was not called on all handlers")
}
// Test HandleRPC
chainHandler.HandleRPC(ctx, stat)
if !handler1.handleRPCCalled || !handler2.handleRPCCalled {
t.Errorf("HandleRPC was not called on all handlers")
}
// Test TagConn
connInfo := &stats.ConnTagInfo{}
ctx = chainHandler.TagConn(ctx, connInfo)
if !handler1.tagConnCalled || !handler2.tagConnCalled {
t.Errorf("TagConn was not called on all handlers")
}
// Test HandleConn
connStat := &stats.ConnBegin{}
chainHandler.HandleConn(ctx, connStat)
if !handler1.handleConnCalled || !handler2.handleConnCalled {
t.Errorf("HandleConn was not called on all handlers")
}
}