1
1
package dispatch_test
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"fmt"
6
7
"net/http"
@@ -41,10 +42,10 @@ func TestWebhook(t *testing.T) {
41
42
}
42
43
43
44
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 )
48
49
49
50
expectSuccess bool
50
51
expectRetryable bool
@@ -76,10 +77,13 @@ func TestWebhook(t *testing.T) {
76
77
},
77
78
{
78
79
name : "timeout" ,
79
- serverTimeout : time .Nanosecond ,
80
+ serverDeadline : time .Now (). Add ( - time . Hour ) ,
80
81
expectSuccess : false ,
81
82
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" ,
83
87
},
84
88
{
85
89
name : "non-200 response" ,
@@ -99,14 +103,20 @@ func TestWebhook(t *testing.T) {
99
103
t .Run (tc .name , func (t * testing.T ) {
100
104
t .Parallel ()
101
105
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 )
105
115
}
116
+ t .Cleanup (cancel )
106
117
107
118
var (
108
119
err error
109
- ctx = testutil .Context (t , timeout )
110
120
msgID = uuid .New ()
111
121
)
112
122
0 commit comments