Skip to content

Commit ea4e1e6

Browse files
committed
tests: tweak timings so we cancel during HTTP processing
There are 3 possibilities: * We cancel before the HTTP request (possibly even before the dial has completed) * We cancel during the delay in our slow server * We cancel after request processing Because our intervals were [0, 1s] and [0.5s, 2s] I think we previously could have hit all three cases. Likely there is a bug lurking in one of these three cases, but let's start by making the delays such that we are always in the second situation (assuming dialing localhost takes < 1s) We now delay the HTTP response by 3 seconds, and cancel after a random value in the interval [1s, 2s)
1 parent a515c83 commit ea4e1e6

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

tests/proxy_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

117126
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)
119128
time.Sleep(delay)
120129
w.Write([]byte("hello"))
121130
}
@@ -360,7 +369,8 @@ func TestProxyDial_RequestCancelled_GRPC(t *testing.T) {
360369
func 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

Comments
 (0)