@@ -20,6 +20,7 @@ package test
2020
2121import (
2222 "context"
23+ "fmt"
2324 "net"
2425 "testing"
2526
@@ -32,15 +33,15 @@ import (
3233)
3334
3435// TestPeerForClientStatsHandler configures a stats handler that
35- // verifies that peer is sent for OutPayload, InPayload, End
36- // stats handlers .
36+ // verifies that peer is sent all stats handler callouts instead
37+ // of Begin and PickerUpdated .
3738func (s ) TestPeerForClientStatsHandler (t * testing.T ) {
38- statsHandler := & peerStatsHandler {}
39+ psh := & peerStatsHandler {}
3940
40- // Define expected stats callouts and whether a peer object should be populated .
41+ // Stats callouts & peer object population .
4142 // Note:
42- // * Begin stats don't have peer information as the RPC begins before peer resolution.
43- // * PickerUpdated stats don't have peer information as the picker operates without transport-level knowledge .
43+ // * Begin stats lack peer info ( RPC starts pre- resolution) .
44+ // * PickerUpdated: no peer info ( picker lacks transport details) .
4445 expectedCallouts := map [stats.RPCStats ]bool {
4546 & stats.OutPayload {}: true ,
4647 & stats.InHeader {}: true ,
@@ -74,7 +75,7 @@ func (s) TestPeerForClientStatsHandler(t *testing.T) {
7475 cc , err := grpc .NewClient (
7576 l .Addr ().String (),
7677 grpc .WithTransportCredentials (insecure .NewCredentials ()),
77- grpc .WithStatsHandler (statsHandler ))
78+ grpc .WithStatsHandler (psh ))
7879 if err != nil {
7980 t .Fatal (err )
8081 }
@@ -83,17 +84,17 @@ func (s) TestPeerForClientStatsHandler(t *testing.T) {
8384 t .Error (err )
8485 }
8586 })
86-
8787 ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
8888 defer cancel ()
8989 client := testgrpc .NewTestServiceClient (cc )
9090 interop .DoClientStreaming (ctx , client )
9191
92- if len (getUniqueRPCStats (statsHandler .Args )) < len (expectedCallouts ) {
93- t .Errorf ("Unexpected number of stats handler callouts." )
92+ sc := getUniqueRPCStatsCount (psh .args )
93+ if sc != len (expectedCallouts ) {
94+ t .Errorf ("Unexpected number of stats handler callouts. Got %v, want %v" , sc , len (expectedCallouts ))
9495 }
9596
96- for _ , callbackArgs := range statsHandler . Args {
97+ for _ , callbackArgs := range psh . args {
9798 expectedPeer , found := expectedCallouts [callbackArgs .rpcStats ]
9899 // In case expectation is set to false and still we got the peer,
99100 // then it's good to have it. So no need to assert those conditions.
@@ -106,19 +107,17 @@ func (s) TestPeerForClientStatsHandler(t *testing.T) {
106107}
107108
108109// getUniqueRPCStats extracts a list of unique stats.RPCStats types from peer list of RPC callback.
109- func getUniqueRPCStats (args []peerStats ) []stats.RPCStats {
110- uniqueStatsTypes := make (map [stats.RPCStats ]struct {})
111-
110+ func getUniqueRPCStatsCount (args []peerStats ) int {
111+ uniqueStatsTypes := make (map [string ]struct {})
112112 for _ , callbackArgs := range args {
113- uniqueStatsTypes [callbackArgs .rpcStats ] = struct {}{}
114- }
115-
116- var uniqueStatsList []stats.RPCStats
117- for statsType := range uniqueStatsTypes {
118- uniqueStatsList = append (uniqueStatsList , statsType )
113+ key := fmt .Sprintf ("%T" , callbackArgs .rpcStats )
114+ if _ , exists := uniqueStatsTypes [key ]; exists {
115+ continue
116+ }
117+ uniqueStatsTypes [fmt .Sprintf ("%T" , callbackArgs .rpcStats )] = struct {}{}
119118 }
120119
121- return uniqueStatsList
120+ return len ( uniqueStatsTypes )
122121}
123122
124123type peerStats struct {
@@ -127,7 +126,7 @@ type peerStats struct {
127126}
128127
129128type peerStatsHandler struct {
130- Args []peerStats
129+ args []peerStats
131130}
132131
133132func (h * peerStatsHandler ) TagRPC (ctx context.Context , info * stats.RPCTagInfo ) context.Context {
@@ -136,7 +135,7 @@ func (h *peerStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) c
136135
137136func (h * peerStatsHandler ) HandleRPC (ctx context.Context , rs stats.RPCStats ) {
138137 p , _ := peer .FromContext (ctx )
139- h .Args = append (h .Args , peerStats {rs , p })
138+ h .args = append (h .args , peerStats {rs , p })
140139}
141140
142141func (h * peerStatsHandler ) TagConn (ctx context.Context , info * stats.ConnTagInfo ) context.Context {
0 commit comments