fix: skip NATS connection when URL is empty
(cherry picked from commit a3c05b41717bc652ef7736cc2c2b22c4eaed0f5d)
This commit is contained in:
1 parent
3cc5192b7b
commit
a4add7cc5e
2 files changed
+26
No files matched your search
@@ -5,6 +5,7 @@ import (
|
|||||||
logger_wrapper "github.com/evolution-foundation/evolution-go/pkg/logger"
|
logger_wrapper "github.com/evolution-foundation/evolution-go/pkg/logger"
|
||||||
"github.com/gomessguii/logger"
|
"github.com/gomessguii/logger"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type natsProducer struct {
|
type natsProducer struct {
|
||||||
@@ -20,6 +21,15 @@ func NewNatsProducer(
|
|||||||
natsGlobalEvents []string,
|
natsGlobalEvents []string,
|
||||||
loggerWrapper *logger_wrapper.LoggerManager,
|
loggerWrapper *logger_wrapper.LoggerManager,
|
||||||
) producer_interfaces.Producer {
|
) producer_interfaces.Producer {
|
||||||
|
if strings.TrimSpace(url) == "" {
|
||||||
|
return &natsProducer{
|
||||||
|
conn: nil,
|
||||||
|
natsGlobalEnabled: false,
|
||||||
|
natsGlobalEvents: nil,
|
||||||
|
loggerWrapper: loggerWrapper,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
conn, err := nats.Connect(url)
|
conn, err := nats.Connect(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.LogError("Failed to connect to NATS: %v", err)
|
logger.LogError("Failed to connect to NATS: %v", err)
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package nats_producer
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNewNatsProducerDoesNotConnectWithoutURL(t *testing.T) {
|
||||||
|
producer, ok := NewNatsProducer(" ", false, nil, 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user