Skip to content

Commit 9596f23

Browse files
authored
fix: use negative deadline to ensure timeout in TestWebhook/timeout (coder#14498)
1 parent 0f414a0 commit 9596f23

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

coderd/notifications/dispatch/webhook_test.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dispatch_test
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"net/http"
@@ -41,10 +42,10 @@ func TestWebhook(t *testing.T) {
4142
}
4243

4344
tests := []struct {
44-
name string
45-
serverURL string
46-
serverTimeout time.Duration
47-
serverFn func(uuid.UUID, http.ResponseWriter, *http.Request)
45+
name string
46+
serverURL string
47+
serverDeadline time.Time
48+
serverFn func(uuid.UUID, http.ResponseWriter, *http.Request)
4849

4950
expectSuccess bool
5051
expectRetryable bool
@@ -76,10 +77,13 @@ func TestWebhook(t *testing.T) {
7677
},
7778
{
7879
name: "timeout",
79-
serverTimeout: time.Nanosecond,
80+
serverDeadline: time.Now().Add(-time.Hour),
8081
expectSuccess: false,
8182
expectRetryable: true,
82-
expectErr: "request timeout",
83+
serverFn: func(u uuid.UUID, writer http.ResponseWriter, request *http.Request) {
84+
t.Fatalf("should not get here")
85+
},
86+
expectErr: "request timeout",
8387
},
8488
{
8589
name: "non-200 response",
@@ -99,14 +103,20 @@ func TestWebhook(t *testing.T) {
99103
t.Run(tc.name, func(t *testing.T) {
100104
t.Parallel()
101105

102-
timeout := testutil.WaitLong
103-
if tc.serverTimeout > 0 {
104-
timeout = tc.serverTimeout
106+
var (
107+
ctx context.Context
108+
cancel context.CancelFunc
109+
)
110+
111+
if !tc.serverDeadline.IsZero() {
112+
ctx, cancel = context.WithDeadline(context.Background(), tc.serverDeadline)
113+
} else {
114+
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitLong)
105115
}
116+
t.Cleanup(cancel)
106117

107118
var (
108119
err error
109-
ctx = testutil.Context(t, timeout)
110120
msgID = uuid.New()
111121
)
112122

0 commit comments

Comments
 (0)