Skip to content

Commit b4b86b9

Browse files
committed
dial.go: Use timeout on HTTPClient properly
Closes #341
1 parent 2598ea2 commit b4b86b9

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

Diff for: conn_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,37 @@ func TestConn(t *testing.T) {
264264
err = c1.Close(websocket.StatusNormalClosure, "")
265265
assert.Success(t, err)
266266
})
267+
268+
t.Run("HTTPClient.Timeout", func(t *testing.T) {
269+
tt, c1, c2 := newConnTest(t, &websocket.DialOptions{
270+
HTTPClient: &http.Client{Timeout: time.Second*5},
271+
}, nil)
272+
273+
tt.goEchoLoop(c2)
274+
275+
c1.SetReadLimit(1 << 30)
276+
277+
exp := xrand.String(xrand.Int(131072))
278+
279+
werr := xsync.Go(func() error {
280+
return wsjson.Write(tt.ctx, c1, exp)
281+
})
282+
283+
var act interface{}
284+
err := wsjson.Read(tt.ctx, c1, &act)
285+
assert.Success(t, err)
286+
assert.Equal(t, "read msg", exp, act)
287+
288+
select {
289+
case err := <-werr:
290+
assert.Success(t, err)
291+
case <-tt.ctx.Done():
292+
t.Fatal(tt.ctx.Err())
293+
}
294+
295+
err = c1.Close(websocket.StatusNormalClosure, "")
296+
assert.Success(t, err)
297+
})
267298
}
268299

269300
func TestWasm(t *testing.T) {

Diff for: dial.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,13 @@ func (opts *DialOptions) cloneWithDefaults(ctx context.Context) (context.Context
5959
}
6060
if o.HTTPClient == nil {
6161
o.HTTPClient = http.DefaultClient
62-
} else if opts.HTTPClient.Timeout > 0 {
63-
ctx, cancel = context.WithTimeout(ctx, opts.HTTPClient.Timeout)
62+
}
63+
if o.HTTPClient.Timeout > 0 {
64+
ctx, cancel = context.WithTimeout(ctx, o.HTTPClient.Timeout)
6465

65-
newClient := *opts.HTTPClient
66+
newClient := *o.HTTPClient
6667
newClient.Timeout = 0
67-
opts.HTTPClient = &newClient
68+
o.HTTPClient = &newClient
6869
}
6970
if o.HTTPHeader == nil {
7071
o.HTTPHeader = http.Header{}

0 commit comments

Comments
 (0)