Skip to content

Commit c67e612

Browse files
committed
Add a test case for usage of closed connection
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 <[email protected]>
1 parent 2d55559 commit c67e612

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

client_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,41 @@ func TestOpen(t *testing.T) {
288288
if c, err := Open(rwc, defaultConfig()); err != nil {
289289
t.Fatalf("could not create connection: %v (%s)", c, err)
290290
}
291+
}
292+
293+
func TestOpenClose_ShouldNotPanic(t *testing.T) {
294+
rwc, srv := newSession(t)
295+
t.Cleanup(func() {
296+
_ = rwc.Close()
297+
})
298+
299+
go func() {
300+
srv.connectionOpen()
301+
srv.connectionClose()
302+
}()
303+
304+
c, err := Open(rwc, defaultConfig())
305+
if err != nil {
306+
t.Fatalf("could not create connection: %v (%s)", c, err)
307+
}
308+
309+
if err := c.Close(); err != nil {
310+
t.Fatalf("could not close connection: %s", err)
311+
}
291312

313+
defer func() {
314+
if r := recover(); r != nil {
315+
t.Fatalf("creating a channel on a closed connection should not panic: %s", r)
316+
}
317+
}()
318+
319+
ch, err := c.Channel()
320+
if ch != nil {
321+
t.Fatalf("creating a channel on a closed connection should not succeed: %v, (%s)", ch, err)
322+
}
323+
if err != ErrClosed {
324+
t.Fatalf("error should be closed: %s", err)
325+
}
292326
}
293327

294328
func TestChannelOpen(t *testing.T) {

0 commit comments

Comments
 (0)