From 1687c801220cdbdf99c06315fdb85dc7b8d02cd2 Mon Sep 17 00:00:00 2001 From: Joachim Bauch Date: Fri, 4 Jun 2021 15:30:55 +0200 Subject: [PATCH] Add more tests. --- natsclient_loopback_test.go | 24 +++++++++++++++ natsclient_test.go | 58 +++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/natsclient_loopback_test.go b/natsclient_loopback_test.go index 5f5911f..865498a 100644 --- a/natsclient_loopback_test.go +++ b/natsclient_loopback_test.go @@ -63,3 +63,27 @@ func TestLoopbackNatsClient_Subscribe(t *testing.T) { testNatsClient_Subscribe(t, client) }) } + +func TestLoopbackClient_PublishAfterClose(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client := CreateLoopbackNatsClientForTest(t) + + testNatsClient_PublishAfterClose(t, client) + }) +} + +func TestLoopbackClient_SubscribeAfterClose(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client := CreateLoopbackNatsClientForTest(t) + + testNatsClient_SubscribeAfterClose(t, client) + }) +} + +func TestLoopbackClient_BadSubjects(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client := CreateLoopbackNatsClientForTest(t) + + testNatsClient_BadSubjects(t, client) + }) +} diff --git a/natsclient_test.go b/natsclient_test.go index 7879048..7afe06c 100644 --- a/natsclient_test.go +++ b/natsclient_test.go @@ -109,3 +109,61 @@ func TestNatsClient_Subscribe(t *testing.T) { testNatsClient_Subscribe(t, client) }) } + +func testNatsClient_PublishAfterClose(t *testing.T, client NatsClient) { + client.Close() + + if err := client.Publish("foo", "bar"); err != nats.ErrConnectionClosed { + t.Errorf("Expected %v, got %v", nats.ErrConnectionClosed, err) + } +} + +func TestNatsClient_PublishAfterClose(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client, shutdown := CreateLocalNatsClientForTest(t) + defer shutdown() + + testNatsClient_PublishAfterClose(t, client) + }) +} + +func testNatsClient_SubscribeAfterClose(t *testing.T, client NatsClient) { + client.Close() + + ch := make(chan *nats.Msg) + if _, err := client.Subscribe("foo", ch); err != nats.ErrConnectionClosed { + t.Errorf("Expected %v, got %v", nats.ErrConnectionClosed, err) + } +} + +func TestNatsClient_SubscribeAfterClose(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client, shutdown := CreateLocalNatsClientForTest(t) + defer shutdown() + + testNatsClient_SubscribeAfterClose(t, client) + }) +} + +func testNatsClient_BadSubjects(t *testing.T, client NatsClient) { + subjects := []string{ + "foo bar", + "foo.", + } + + ch := make(chan *nats.Msg) + for _, s := range subjects { + if _, err := client.Subscribe(s, ch); err != nats.ErrBadSubject { + t.Errorf("Expected %v for subject %s, got %v", nats.ErrBadSubject, s, err) + } + } +} + +func TestNatsClient_BadSubjects(t *testing.T) { + ensureNoGoroutinesLeak(t, func() { + client, shutdown := CreateLocalNatsClientForTest(t) + defer shutdown() + + testNatsClient_BadSubjects(t, client) + }) +}