@@ -24,6 +24,7 @@ import (
2424
2525 v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
2626 "github.com/google/go-cmp/cmp"
27+ "github.com/google/go-cmp/cmp/cmpopts"
2728 "github.com/google/uuid"
2829 "google.golang.org/grpc/internal/testutils/xds/fakeserver"
2930 "google.golang.org/grpc/xds/internal/testutils"
@@ -35,6 +36,22 @@ import (
3536 v3lrspb "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v3"
3637)
3738
39+ const (
40+ testLocality1 = `{"region":"test-region1"}`
41+ testLocality2 = `{"region":"test-region2"}`
42+ testKey1 = "test-key1"
43+ testKey2 = "test-key2"
44+ )
45+
46+ var (
47+ toleranceCmpOpt = cmpopts .EquateApprox (0 , 1e-5 )
48+ ignoreOrderCmpOpt = protocmp .FilterField (& v3endpointpb.ClusterStats {}, "upstream_locality_stats" ,
49+ cmpopts .SortSlices (func (a , b protocmp.Message ) bool {
50+ return a .String () < b .String ()
51+ }),
52+ )
53+ )
54+
3855func (s ) TestReportLoad (t * testing.T ) {
3956 // Create a fake xDS management server listening on a local port.
4057 mgmtServer , cleanup := startFakeManagementServer (t )
@@ -74,6 +91,13 @@ func (s) TestReportLoad(t *testing.T) {
7491
7592 // Push some loads on the received store.
7693 store1 .PerCluster ("cluster1" , "eds1" ).CallDropped ("test" )
94+ store1 .PerCluster ("cluster1" , "eds1" ).CallStarted (testLocality1 )
95+ store1 .PerCluster ("cluster1" , "eds1" ).CallServerLoad (testLocality1 , testKey1 , 3.14 )
96+ store1 .PerCluster ("cluster1" , "eds1" ).CallServerLoad (testLocality1 , testKey1 , 2.718 )
97+ store1 .PerCluster ("cluster1" , "eds1" ).CallFinished (testLocality1 , nil )
98+ store1 .PerCluster ("cluster1" , "eds1" ).CallStarted (testLocality2 )
99+ store1 .PerCluster ("cluster1" , "eds1" ).CallServerLoad (testLocality2 , testKey2 , 1.618 )
100+ store1 .PerCluster ("cluster1" , "eds1" ).CallFinished (testLocality2 , nil )
77101
78102 // Ensure the initial request is received.
79103 req , err := mgmtServer .LRSRequestChan .Receive (ctx )
@@ -115,8 +139,23 @@ func (s) TestReportLoad(t *testing.T) {
115139 ClusterServiceName : "eds1" ,
116140 TotalDroppedRequests : 1 ,
117141 DroppedRequests : []* v3endpointpb.ClusterStats_DroppedRequests {{Category : "test" , DroppedCount : 1 }},
142+ UpstreamLocalityStats : []* v3endpointpb.UpstreamLocalityStats {
143+ {
144+ Locality : & v3corepb.Locality {Region : "test-region1" },
145+ LoadMetricStats : []* v3endpointpb.EndpointLoadMetricStats {
146+ // TotalMetricValue is the aggregation of 3.14 + 2.718 = 5.858
147+ {MetricName : testKey1 , NumRequestsFinishedWithMetric : 2 , TotalMetricValue : 5.858 }},
148+ TotalSuccessfulRequests : 1 ,
149+ },
150+ {
151+ Locality : & v3corepb.Locality {Region : "test-region2" },
152+ LoadMetricStats : []* v3endpointpb.EndpointLoadMetricStats {
153+ {MetricName : testKey2 , NumRequestsFinishedWithMetric : 1 , TotalMetricValue : 1.618 }},
154+ TotalSuccessfulRequests : 1 ,
155+ },
156+ },
118157 }
119- if diff := cmp .Diff (wantLoad , gotLoad [0 ], protocmp .Transform ()); diff != "" {
158+ if diff := cmp .Diff (wantLoad , gotLoad [0 ], protocmp .Transform (), toleranceCmpOpt , ignoreOrderCmpOpt ); diff != "" {
120159 t .Fatalf ("Unexpected diff in LRS request (-got, +want):\n %s" , diff )
121160 }
122161
0 commit comments