From 887d1a5ab339bd29b1bc03316549b762db0eda40 Mon Sep 17 00:00:00 2001 From: Fabrizio Sestito Date: Sun, 3 Nov 2024 16:39:27 +0100 Subject: [PATCH] test: add messaging.Publisher integration test Signed-off-by: Fabrizio Sestito --- internal/messaging/publisher_test.go | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 internal/messaging/publisher_test.go diff --git a/internal/messaging/publisher_test.go b/internal/messaging/publisher_test.go new file mode 100644 index 0000000..3db4d4b --- /dev/null +++ b/internal/messaging/publisher_test.go @@ -0,0 +1,53 @@ +package messaging + +import ( + "encoding/json" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type testMessage struct { + Data string `json:"data"` +} + +func (m testMessage) MessageType() string { + return "test-type" +} + +func TestPublisher_Publish(t *testing.T) { + ns, err := NewServer() + require.NoError(t, err) + defer ns.Shutdown() + + js, err := NewJetStreamContext(ns) + require.NoError(t, err) + + publisher := NewPublisher(js) + + msg := testMessage{ + Data: "test data", + } + + err = publisher.Publish(msg) + require.NoError(t, err) + + sub, err := js.SubscribeSync(SbombasticSubject) + require.NoError(t, err) + defer func() { + err := sub.Unsubscribe() + require.NoError(t, err) + }() + + receivedMsg, err := sub.NextMsg(2 * time.Second) + require.NoError(t, err) + + assert.Equal(t, msg.MessageType(), receivedMsg.Header.Get(MessageTypeHeader)) + + var receivedData testMessage + err = json.Unmarshal(receivedMsg.Data, &receivedData) + require.NoError(t, err) + assert.Equal(t, msg.Data, receivedData.Data) +}