@@ -105,17 +105,26 @@ type delayedServer struct {
105
105
maxWait time.Duration
106
106
}
107
107
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 {
109
118
return & delayedServer {
110
- minWait : 500 * time . Millisecond ,
111
- maxWait : 2 * time . Second ,
119
+ minWait : minWait ,
120
+ maxWait : maxWait ,
112
121
}
113
122
}
114
123
115
- var _ = newDelayedServer () // Suppress unused lint error.
124
+ var _ = newDelayedServer (time . Second , time . Second ) // Suppress unused lint error.
116
125
117
126
func (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 )
119
128
time .Sleep (delay )
120
129
w .Write ([]byte ("hello" ))
121
130
}
@@ -360,7 +369,8 @@ func TestProxyDial_RequestCancelled_GRPC(t *testing.T) {
360
369
func TestProxyDial_RequestCancelled_Concurrent_GRPC (t * testing.T ) {
361
370
expectCleanShutdown (t )
362
371
363
- slowServer := newDelayedServer ()
372
+ // An HTTP server that always waits 3 seconds before replying
373
+ slowServer := newDelayedServer (3 * time .Second , 3 * time .Second )
364
374
server := httptest .NewServer (slowServer )
365
375
defer server .Close ()
366
376
@@ -420,8 +430,12 @@ func TestProxyDial_RequestCancelled_Concurrent_GRPC(t *testing.T) {
420
430
const concurrentConns = 50
421
431
wg .Add (concurrentConns )
422
432
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 )
425
439
}
426
440
wg .Wait ()
427
441
0 commit comments