Skip to content

Commit

Permalink
Add a test case for usage of closed connection
Browse files Browse the repository at this point in the history
After 2d55559, we are setting some fields to nil in the shutdown
sequence. We have to ensure that the library does not panic as a
consequence, after the connection is closed.

Signed-off-by: Aitor Perez Cedres <acedres@vmware.com>
  • Loading branch information
Zerpet committed Feb 9, 2023
1 parent 2d55559 commit c67e612
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,41 @@ func TestOpen(t *testing.T) {
if c, err := Open(rwc, defaultConfig()); err != nil {
t.Fatalf("could not create connection: %v (%s)", c, err)
}
}

func TestOpenClose_ShouldNotPanic(t *testing.T) {
rwc, srv := newSession(t)
t.Cleanup(func() {
_ = rwc.Close()
})

go func() {
srv.connectionOpen()
srv.connectionClose()
}()

c, err := Open(rwc, defaultConfig())
if err != nil {
t.Fatalf("could not create connection: %v (%s)", c, err)
}

if err := c.Close(); err != nil {
t.Fatalf("could not close connection: %s", err)
}

defer func() {
if r := recover(); r != nil {
t.Fatalf("creating a channel on a closed connection should not panic: %s", r)
}
}()

ch, err := c.Channel()
if ch != nil {
t.Fatalf("creating a channel on a closed connection should not succeed: %v, (%s)", ch, err)
}
if err != ErrClosed {
t.Fatalf("error should be closed: %s", err)
}
}

func TestChannelOpen(t *testing.T) {
Expand Down

0 comments on commit c67e612

Please sign in to comment.