Use "T.Cleanup()" instead of manually deferred shutdown function.

This commit is contained in:
Joachim Bauch
2022-04-05 12:57:40 +02:00
parent 81a52389aa
commit 2a491ec3f0
9 changed files with 99 additions and 166 deletions
+8 -11
View File
@@ -48,20 +48,20 @@ func (c *LoopbackNatsClient) waitForSubscriptionsEmpty(ctx context.Context, t *t
}
}
func CreateLoopbackNatsClientForTest(t *testing.T) (NatsClient, func()) {
func CreateLoopbackNatsClientForTest(t *testing.T) NatsClient {
result, err := NewLoopbackNatsClient()
if err != nil {
t.Fatal(err)
}
return result, func() {
t.Cleanup(func() {
result.Close()
}
})
return result
}
func TestLoopbackNatsClient_Subscribe(t *testing.T) {
ensureNoGoroutinesLeak(t, func() {
client, shutdown := CreateLoopbackNatsClientForTest(t)
defer shutdown()
client := CreateLoopbackNatsClientForTest(t)
testNatsClient_Subscribe(t, client)
})
@@ -69,8 +69,7 @@ func TestLoopbackNatsClient_Subscribe(t *testing.T) {
func TestLoopbackClient_PublishAfterClose(t *testing.T) {
ensureNoGoroutinesLeak(t, func() {
client, shutdown := CreateLoopbackNatsClientForTest(t)
defer shutdown()
client := CreateLoopbackNatsClientForTest(t)
testNatsClient_PublishAfterClose(t, client)
})
@@ -78,8 +77,7 @@ func TestLoopbackClient_PublishAfterClose(t *testing.T) {
func TestLoopbackClient_SubscribeAfterClose(t *testing.T) {
ensureNoGoroutinesLeak(t, func() {
client, shutdown := CreateLoopbackNatsClientForTest(t)
defer shutdown()
client := CreateLoopbackNatsClientForTest(t)
testNatsClient_SubscribeAfterClose(t, client)
})
@@ -87,8 +85,7 @@ func TestLoopbackClient_SubscribeAfterClose(t *testing.T) {
func TestLoopbackClient_BadSubjects(t *testing.T) {
ensureNoGoroutinesLeak(t, func() {
client, shutdown := CreateLoopbackNatsClientForTest(t)
defer shutdown()
client := CreateLoopbackNatsClientForTest(t)
testNatsClient_BadSubjects(t, client)
})