Skip to content

Use t.Cleanup instead of defer in Golang code tests #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
8 changes: 4 additions & 4 deletions jsonrpc2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestClientServer(t *testing.T) {
if err != nil {
t.Fatal("Listen:", err)
}
defer func() {
t.Cleanup(func() {
if lis == nil {
return // already closed
}
Expand All @@ -130,7 +130,7 @@ func TestClientServer(t *testing.T) {
t.Fatal(err)
}
}
}()
})

ha := testHandlerA{t: t}
go func() {
Expand Down Expand Up @@ -184,11 +184,11 @@ func TestClientServer(t *testing.T) {
func testClientServer(ctx context.Context, t *testing.T, stream jsonrpc2.ObjectStream) {
hb := testHandlerB{t: t}
cc := jsonrpc2.NewConn(ctx, stream, &hb)
defer func() {
t.Cleanup(func() {
if err := cc.Close(); err != nil {
t.Fatal(err)
}
}()
})

// Simple
const n = 100
Expand Down