Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f56add

Browse files
committedJun 1, 2019
Add test for c.Context method
1 parent c5e2428 commit 3f56add

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
 

‎websocket_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,51 @@ func TestHandshake(t *testing.T) {
455455
return nil
456456
},
457457
},
458+
{
459+
name: "context",
460+
server: func(w http.ResponseWriter, r *http.Request) error {
461+
c, err := websocket.Accept(w, r, websocket.AcceptOptions{})
462+
if err != nil {
463+
return err
464+
}
465+
defer c.Close(websocket.StatusInternalError, "")
466+
467+
ctx, cancel := context.WithTimeout(r.Context(), time.Second)
468+
defer cancel()
469+
470+
c.Context(ctx)
471+
472+
for r.Context().Err() == nil {
473+
err = c.Ping(ctx)
474+
if err != nil {
475+
return nil
476+
}
477+
}
478+
479+
return xerrors.Errorf("all pings succeeded")
480+
},
481+
client: func(ctx context.Context, u string) error {
482+
c, _, err := websocket.Dial(ctx, u, websocket.DialOptions{})
483+
if err != nil {
484+
return err
485+
}
486+
defer c.Close(websocket.StatusInternalError, "")
487+
488+
pctx := c.Context(ctx)
489+
490+
for ctx.Err() == nil {
491+
err = c.Ping(ctx)
492+
if err != nil {
493+
if pctx.Err() == nil {
494+
return xerrors.Errorf("context from c.Context not cancelled when connection broken")
495+
}
496+
return nil
497+
}
498+
}
499+
500+
return xerrors.Errorf("all pings succeeded")
501+
},
502+
},
458503
}
459504

460505
for _, tc := range testCases {

0 commit comments

Comments
 (0)
Please sign in to comment.