Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions pkg/events/nats/nats_producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
logger_wrapper "github.com/evolution-foundation/evolution-go/pkg/logger"
"github.com/gomessguii/logger"
"github.com/nats-io/nats.go"
"strings"
)

type natsProducer struct {
Expand All @@ -20,6 +21,15 @@ func NewNatsProducer(
natsGlobalEvents []string,
loggerWrapper *logger_wrapper.LoggerManager,
) producer_interfaces.Producer {
if strings.TrimSpace(url) == "" {
return &natsProducer{
conn: nil,
natsGlobalEnabled: false,
natsGlobalEvents: nil,
loggerWrapper: loggerWrapper,
}
}

conn, err := nats.Connect(url)
if err != nil {
logger.LogError("Failed to connect to NATS: %v", err)
Expand Down
31 changes: 31 additions & 0 deletions pkg/events/nats/nats_producer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nats_producer

import "testing"

func TestNewNatsProducerDoesNotConnectWithoutURL(t *testing.T) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Cover both empty and whitespace-only URLs, ideally via table-driven subtests

This test only covers the whitespace-only case (" "). Since the behavior should be the same for both empty and whitespace-only URLs, please either add a separate test for "" or convert this into a table-driven test with subtests for "" and " " (and any other relevant variants) to ensure we’re protected against regressions for all "empty" URL forms.

tests := []struct {
name string
url string
}{
{"Empty URL", ""},
{"Whitespace URL", " "},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
producer, ok := NewNatsProducer(tt.url, true, []string{"event1"}, nil).(*natsProducer)
if !ok {
t.Fatal("constructor returned an unexpected producer type")
}
if producer.conn != nil {
t.Fatal("NATS connection must stay nil when NATS_URL is empty")
}
if producer.natsGlobalEnabled {
t.Fatal("NATS global publishing must stay disabled without a URL")
}
if producer.natsGlobalEvents != nil {
t.Fatal("natsGlobalEvents must be nil when NATS_URL is empty")
}
})
}
}