Skip to content

fix: skip NATS connection when URL is empty - #143

Open
joldmarfilho wants to merge 2 commits into
evolution-foundation:mainfrom
joldmarfilho:fix-skip-NATS-connection-when-URL-is-empty
Open

fix: skip NATS connection when URL is empty#143
joldmarfilho wants to merge 2 commits into
evolution-foundation:mainfrom
joldmarfilho:fix-skip-NATS-connection-when-URL-is-empty

Conversation

@joldmarfilho

@joldmarfilho joldmarfilho commented Jul 29, 2026

Copy link
Copy Markdown

Description

Related Issue

Closes #(issue_number)

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement

Testing

  • Manual testing completed
  • Functionality verified in development environment
  • No breaking changes introduced

Screenshots (if applicable)

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have tested my changes thoroughly
  • Any dependent changes have been merged and published

Additional Notes

Summary by Sourcery

Skip establishing a NATS connection when no URL is provided and ensure producer is disabled in that case.

Bug Fixes:

  • Avoid creating a NATS connection when the configured URL is empty or whitespace-only.

Tests:

  • Add a unit test verifying that the NATS producer does not connect and remains disabled when initialized without a URL.

@sourcery-ai

sourcery-ai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Reviewer's Guide

Ensure the NATS producer constructor safely no-ops when given an empty URL, and add a regression test to verify it does not establish a NATS connection or enable global publishing in that case.

Sequence diagram for NewNatsProducer behavior with empty URL

sequenceDiagram
    participant Caller
    participant NewNatsProducer
    participant NATS as nats

    Caller->>NewNatsProducer: NewNatsProducer(url, natsGlobalEnabled, natsGlobalEvents, loggerWrapper)
    alt [strings.TrimSpace(url) == ""]
        NewNatsProducer-->>Caller: &natsProducer{conn:nil, natsGlobalEnabled:false, natsGlobalEvents:nil}
    else [strings.TrimSpace(url) != ""]
        NewNatsProducer->>NATS: Connect(url)
        NATS-->>NewNatsProducer: *nats.Conn
        NewNatsProducer-->>Caller: &natsProducer{conn:*nats.Conn, natsGlobalEnabled:natsGlobalEnabled}
    end
Loading

File-Level Changes

Change Details Files
Short‑circuit NATS producer construction when the URL is empty or whitespace-only.
  • Import the strings package to allow trimming of the URL input.
  • Add an early return in the NatsProducer constructor that checks for an empty or whitespace-only URL and returns a producer with nil connection, global publishing disabled, and propagated logger wrapper.
  • Keep the existing NATS connection logic for non-empty URLs unchanged.
pkg/events/nats/nats_producer.go
Add unit test to verify that no NATS connection is created when the URL is empty.
  • Create a new test file for the NATS producer constructor.
  • Add a test that calls the constructor with a whitespace-only URL and asserts that the returned type is natsProducer, the connection is nil, and global publishing is disabled.
pkg/events/nats/nats_producer_test.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 2 issues, and left some high level feedback:

  • In the empty-URL branch, consider retaining the provided natsGlobalEvents slice instead of forcing it to nil to avoid surprising callers that may rely on the configured event list even when the producer is disabled.
  • It may be helpful to log a clear warning when the NATS URL is empty so that operators can distinguish between an intentional configuration (disabled producer) and a misconfiguration.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the empty-URL branch, consider retaining the provided natsGlobalEvents slice instead of forcing it to nil to avoid surprising callers that may rely on the configured event list even when the producer is disabled.
- It may be helpful to log a clear warning when the NATS URL is empty so that operators can distinguish between an intentional configuration (disabled producer) and a misconfiguration.

## Individual Comments

### Comment 1
<location path="pkg/events/nats/nats_producer_test.go" line_range="5" />
<code_context>
+
+import "testing"
+
+func TestNewNatsProducerDoesNotConnectWithoutURL(t *testing.T) {
+	producer, ok := NewNatsProducer("  ", false, nil, nil).(*natsProducer)
+	if !ok {
</code_context>
<issue_to_address>
**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.
</issue_to_address>

### Comment 2
<location path="pkg/events/nats/nats_producer_test.go" line_range="10-13" />
<code_context>
+	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")
+	}
</code_context>
<issue_to_address>
**suggestion (testing):** Also assert on `natsGlobalEvents` to fully document the constructor behavior when URL is empty

Since the constructor also sets `natsGlobalEvents` to nil when no URL is provided, add an assertion like `if producer.natsGlobalEvents != nil { t.Fatal("natsGlobalEvents must be nil when NATS_URL is empty") }` so the test fully captures the producer’s expected state in this scenario.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.


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.

Comment thread pkg/events/nats/nats_producer_test.go Outdated
Comment on lines +10 to +13
if producer.conn != nil {
t.Fatal("NATS connection must stay nil when NATS_URL is empty")
}
if producer.natsGlobalEnabled {

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): Also assert on natsGlobalEvents to fully document the constructor behavior when URL is empty

Since the constructor also sets natsGlobalEvents to nil when no URL is provided, add an assertion like if producer.natsGlobalEvents != nil { t.Fatal("natsGlobalEvents must be nil when NATS_URL is empty") } so the test fully captures the producer’s expected state in this scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant