@@ -105,17 +105,26 @@ type delayedServer struct {
105105 maxWait time.Duration
106106}
107107
108- func newDelayedServer () * delayedServer {
108+ // randomDuration returns a random duration in the [min, max) interval
109+ func randomDuration (min , max time.Duration ) time.Duration {
110+ d := min
111+ if max != min {
112+ d += time .Duration (rand .Int63n (int64 (max - min )))
113+ }
114+ return d
115+ }
116+
117+ func newDelayedServer (minWait time.Duration , maxWait time.Duration ) * delayedServer {
109118 return & delayedServer {
110- minWait : 500 * time . Millisecond ,
111- maxWait : 2 * time . Second ,
119+ minWait : minWait ,
120+ maxWait : maxWait ,
112121 }
113122}
114123
115- var _ = newDelayedServer () // Suppress unused lint error.
124+ var _ = newDelayedServer (time . Second , time . Second ) // Suppress unused lint error.
116125
117126func (s * delayedServer ) ServeHTTP (w http.ResponseWriter , _ * http.Request ) {
118- delay := time . Duration ( rand . Int63n ( int64 ( s . maxWait - s . minWait ))) + s . minWait /* #nosec G404 */
127+ delay := randomDuration ( s . minWait , s . maxWait )
119128 time .Sleep (delay )
120129 w .Write ([]byte ("hello" ))
121130}
@@ -360,7 +369,8 @@ func TestProxyDial_RequestCancelled_GRPC(t *testing.T) {
360369func TestProxyDial_RequestCancelled_Concurrent_GRPC (t * testing.T ) {
361370 expectCleanShutdown (t )
362371
363- slowServer := newDelayedServer ()
372+ // An HTTP server that always waits 3 seconds before replying
373+ slowServer := newDelayedServer (3 * time .Second , 3 * time .Second )
364374 server := httptest .NewServer (slowServer )
365375 defer server .Close ()
366376
@@ -420,8 +430,12 @@ func TestProxyDial_RequestCancelled_Concurrent_GRPC(t *testing.T) {
420430 const concurrentConns = 50
421431 wg .Add (concurrentConns )
422432 for i := 0 ; i < concurrentConns ; i ++ {
423- cancelDelayMs := rand .Int63n (1000 ) + 5 /* #nosec G404 */
424- go dialFn (i , time .Duration (cancelDelayMs )* time .Millisecond )
433+ // The delay before we cancel the HTTP request:
434+ // * at least 1 second for the HTTP connection to establish
435+ // * less than 2 seconds so that we cancel during the 3 second delay in our slowServer
436+ // TODO: Can we try to cancel during the dial? Or after the HTTP request has returned?
437+ cancelDelay := randomDuration (time .Second , 2 * time .Second )
438+ go dialFn (i , cancelDelay )
425439 }
426440 wg .Wait ()
427441
0 commit comments