Skip to content

Commit ad41771

Browse files
authored
acc: ensure Delay does not prevent shutdown (#3548)
For tests that set Delay, do not sleep but exit earlier if context is cancelled (means server is shutting down). Before this change: ``` --- PASS: TestAccept/telemetry/timeout/DATABRICKS_CLI_DEPLOYMENT=terraform (5.53s) --- PASS: TestAccept/telemetry/timeout/DATABRICKS_CLI_DEPLOYMENT=direct-exp (5.53s) ``` After this change: ``` --- PASS: TestAccept/telemetry/timeout/DATABRICKS_CLI_DEPLOYMENT=terraform (0.63s) --- PASS: TestAccept/telemetry/timeout/DATABRICKS_CLI_DEPLOYMENT=direct-exp (0.63s) ```
1 parent e3ffa06 commit ad41771

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

acceptance/internal/prepare_server.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,23 @@ func startLocalServer(t *testing.T,
168168
items := strings.Split(stub.Pattern, " ")
169169
require.Len(t, items, 2)
170170
s.Handle(items[0], items[1], func(req testserver.Request) any {
171-
time.Sleep(stub.Delay)
171+
if stub.Delay > 0 {
172+
ctx := req.Context
173+
174+
timer := time.NewTimer(stub.Delay)
175+
defer timer.Stop()
176+
177+
select {
178+
case <-timer.C:
179+
break
180+
case <-ctx.Done():
181+
// Client canceled/connection closed; just exit.
182+
// Optional: log the reason (context deadline, cancellation, etc.)
183+
t.Logf("request canceled: %v", ctx.Err())
184+
return nil
185+
}
186+
}
187+
172188
return stub.Response
173189
})
174190
}

libs/testserver/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package testserver
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -37,6 +38,7 @@ type Request struct {
3738
Body []byte
3839
Vars map[string]string
3940
Workspace *FakeWorkspace
41+
Context context.Context
4042
}
4143

4244
type Response struct {
@@ -64,6 +66,7 @@ func NewRequest(t testutil.TestingT, r *http.Request, fakeWorkspace *FakeWorkspa
6466
Body: body,
6567
Vars: mux.Vars(r),
6668
Workspace: fakeWorkspace,
69+
Context: r.Context(),
6770
}
6871
}
6972

@@ -271,6 +274,9 @@ func (s *Server) Handle(method, path string, handler HandlerFunc) {
271274
}
272275
} else {
273276
respAny := handler(request)
277+
if respAny == nil && request.Context.Err() != nil {
278+
return
279+
}
274280
resp = normalizeResponse(s.t, respAny)
275281
}
276282

0 commit comments

Comments
 (0)