@@ -208,20 +208,42 @@ func checkRPCSendOK(t *testing.T, ctx context.Context, client testpb.TestService
208208 return backendCount
209209}
210210
211+ // makeNonExistentBackends returns a slice of e2e.BackendOptions with num
212+ // listeners, each of which is closed immediately. Useful to simulate servers
213+ // that are unreachable.
214+ func makeNonExistentBackends (t * testing.T , num int ) []e2e.BackendOptions {
215+ closedListeners := make ([]net.Listener , 0 , num )
216+ for i := 0 ; i < num ; i ++ {
217+ lis , err := testutils .LocalTCPListener ()
218+ if err != nil {
219+ t .Fatalf ("testutils.LocalTCPListener() failed: %v" , err )
220+ }
221+ closedListeners = append (closedListeners , lis )
222+ }
223+
224+ // Stop the servers that we want to be unreachable and collect their
225+ // addresses.
226+ backendOptions := make ([]e2e.BackendOptions , 0 , num )
227+ for _ , lis := range closedListeners {
228+ backendOptions = append (backendOptions , e2e.BackendOptions {
229+ Port : testutils .ParsePort (t , lis .Addr ().String ()),
230+ Weight : 1 ,
231+ })
232+ lis .Close ()
233+ }
234+ return backendOptions
235+ }
236+
211237// Tests that when an aggregate cluster is configured with ring hash policy, and
212238// the first cluster is in transient failure, all RPCs are sent to the second
213239// cluster using the ring hash policy.
214240func (s ) TestRingHash_AggregateClusterFallBackFromRingHashAtStartup (t * testing.T ) {
215241 xdsServer , nodeID , _ , xdsResolver , stop := e2e .SetupManagementServer (t , e2e.ManagementServerOptions {AllowResourceSubset : true })
216242 defer stop ()
217243
218- nonExistantServers , stopNonExistant := startTestServiceBackends (t , 2 )
219244 servers , stop := startTestServiceBackends (t , 2 )
220245 defer stop ()
221246
222- // Stop the servers that we want to be unreachable.
223- stopNonExistant ()
224-
225247 primaryClusterName := "new_cluster_1"
226248 primaryServiceName := "new_eds_service_1"
227249 secondaryClusterName := "new_cluster_2"
@@ -233,7 +255,7 @@ func (s) TestRingHash_AggregateClusterFallBackFromRingHashAtStartup(t *testing.T
233255 Localities : []e2e.LocalityOptions {{
234256 Name : "locality0" ,
235257 Weight : 1 ,
236- Backends : backendOptions (t , nonExistantServers ),
258+ Backends : makeNonExistentBackends (t , 2 ),
237259 }},
238260 })
239261 ep2 := e2e .EndpointResourceWithOptions (e2e.EndpointOptions {
@@ -336,15 +358,12 @@ func (s) TestRingHash_AggregateClusterFallBackFromRingHashToLogicalDnsAtStartup(
336358 backends , stop := startTestServiceBackends (t , 1 )
337359 defer stop ()
338360
339- nonExistingBackend , stop1 := startTestServiceBackends (t , 2 )
340- stop1 ()
341-
342361 endpoints := e2e .EndpointResourceWithOptions (e2e.EndpointOptions {
343362 ClusterName : edsClusterName ,
344363 Localities : []e2e.LocalityOptions {{
345364 Name : "locality0" ,
346365 Weight : 1 ,
347- Backends : backendOptions (t , nonExistingBackend ),
366+ Backends : makeNonExistentBackends (t , 1 ),
348367 Priority : 0 ,
349368 }},
350369 })
@@ -420,15 +439,12 @@ func (s) TestRingHash_AggregateClusterFallBackFromRingHashToLogicalDnsAtStartupN
420439 backends , stop := startTestServiceBackends (t , 1 )
421440 defer stop ()
422441
423- nonExistingBackend , stop1 := startTestServiceBackends (t , 2 )
424- stop1 ()
425-
426442 endpoints := e2e .EndpointResourceWithOptions (e2e.EndpointOptions {
427443 ClusterName : edsClusterName ,
428444 Localities : []e2e.LocalityOptions {{
429445 Name : "locality0" ,
430446 Weight : 1 ,
431- Backends : backendOptions (t , nonExistingBackend ),
447+ Backends : makeNonExistentBackends (t , 1 ),
432448 Priority : 0 ,
433449 }},
434450 })
@@ -767,9 +783,9 @@ func (s) TestRingHash_HeaderHashingWithRegexRewrite(t *testing.T) {
767783//
768784// See https://github.com/grpc/grpc/blob/4f6e13bdda9e8c26d6027af97db4b368ca2b3069/test/cpp/end2end/xds/xds_end2end_test_lib.h#L941
769785// for an explanation of the formula.
770- func computeIdealNumberOfRPCs (p , errorTolerance float64 ) int {
786+ func computeIdealNumberOfRPCs (t * testing. T , p , errorTolerance float64 ) int {
771787 if p < 0 || p > 1 {
772- panic ("p must be in (0, 1)" )
788+ t . Fatal ("p must be in (0, 1)" )
773789 }
774790 numRPCs := math .Ceil (p * (1 - p ) * 5. * 5. / errorTolerance / errorTolerance )
775791 return int (numRPCs + 1000. ) // add 1k as a buffer to avoid flakyness.
@@ -804,7 +820,7 @@ func setRingHashLBPolicyWithHighMinRingSize(t *testing.T, cluster *v3clusterpb.C
804820func (s ) TestRingHash_NoHashPolicy (t * testing.T ) {
805821 backends , stop := startTestServiceBackends (t , 2 )
806822 defer stop ()
807- numRPCs := computeIdealNumberOfRPCs (.5 , errorTolerance )
823+ numRPCs := computeIdealNumberOfRPCs (t , .5 , errorTolerance )
808824
809825 xdsServer , nodeID , _ , xdsResolver , stop := e2e .SetupManagementServer (t , e2e.ManagementServerOptions {AllowResourceSubset : true })
810826 defer stop ()
@@ -910,7 +926,7 @@ func (s) TestRingHash_EndpointWeights(t *testing.T) {
910926 client := testgrpc .NewTestServiceClient (conn )
911927
912928 // Send a large number of RPCs and check that they are distributed randomly.
913- numRPCs := computeIdealNumberOfRPCs (.25 , errorTolerance )
929+ numRPCs := computeIdealNumberOfRPCs (t , .25 , errorTolerance )
914930 gotPerBackend := checkRPCSendOK (t , ctx , client , numRPCs )
915931
916932 got := float64 (gotPerBackend [backends [0 ].Address ]) / float64 (numRPCs )
0 commit comments