Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for handshake deadline #385

Merged
merged 2 commits into from May 29, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions client_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,63 @@ func TestDialTimeout(t *testing.T) {
}
}

// netConnDeadlineObserver fails test if read or write called without deadline.
type netConnDeadlineObserver struct {
t *testing.T
c net.Conn
read, write bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only qualm is these variable names are not descriptive of their purpose, making it a bit confusing to understand how this test works. Maybe rename them to readDeadlineUsed and writeDeadlineUsed ?

}

func (c *netConnDeadlineObserver) SetDeadline(t time.Time) error {
c.write = !t.Equal(time.Time{})
c.read = c.write
return c.c.SetDeadline(t)
}

func (c *netConnDeadlineObserver) SetReadDeadline(t time.Time) error {
c.read = !t.Equal(time.Time{})
return c.c.SetDeadline(t)
}

func (c *netConnDeadlineObserver) SetWriteDeadline(t time.Time) error {
c.write = !t.Equal(time.Time{})
return c.c.SetDeadline(t)
}

func (c *netConnDeadlineObserver) Write(p []byte) (int, error) {
if !c.write {
c.t.Fatalf("write with no deadline")
}
return c.c.Write(p)
}

func (c *netConnDeadlineObserver) Read(p []byte) (int, error) {
if !c.read {
c.t.Fatalf("read with no deadline")
}
return c.c.Read(p)
}

func (c *netConnDeadlineObserver) Close() error { return c.c.Close() }
func (c *netConnDeadlineObserver) LocalAddr() net.Addr { return c.c.LocalAddr() }
func (c *netConnDeadlineObserver) RemoteAddr() net.Addr { return c.c.RemoteAddr() }

func TestHandshakeTimeout(t *testing.T) {
s := newServer(t)
defer s.Close()

d := cstDialer
d.NetDial = func(n, a string) (net.Conn, error) {
c, err := net.Dial(n, a)
return &netConnDeadlineObserver{c: c, t: t}, err
}
ws, _, err := d.Dial(s.URL, nil)
if err != nil {
t.Fatal("Dial:", err)
}
ws.Close()
}

func TestDialBadScheme(t *testing.T) {
s := newServer(t)
defer s.Close()
Expand Down