Skip to content

Conversation

@jdinsel-xealth
Copy link
Contributor

Story

NATS support was broken with a newer version of NATS. An issue was opened describing the situation #1008

This pull request upgrades the NATS components and uses their migration guide to modify the code to support it.

@netlify
Copy link

netlify bot commented May 22, 2025

Deploy Preview for testcontainers-node ready!

Name Link
🔨 Latest commit ff4e44b
🔍 Latest deploy log https://app.netlify.com/projects/testcontainers-node/deploys/682f798b109ded000876dd0f
😎 Deploy Preview https://deploy-preview-1009--testcontainers-node.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@cristianrgreco cristianrgreco changed the title feat(nats): Upgrade NATS to v3.0.0 Upgrade NATS dev library v3.x May 22, 2025
@cristianrgreco cristianrgreco changed the title Upgrade NATS dev library v3.x Upgrade NATS container image and dev library May 22, 2025
@cristianrgreco cristianrgreco added the major An incompatible API change label May 22, 2025
@cristianrgreco
Copy link
Collaborator

This will have to be a breaking change because of the change to the default image.
Merge/release will have to wait a bit.

@cristianrgreco cristianrgreco added the maintenance Improvements that do not change functionality label May 22, 2025
Copy link
Contributor

@joebowbeer joebowbeer left a comment

Choose a reason for hiding this comment

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

Requested change: The new dependencies should be devDependencies

(async () => {
for await (const m of sub) {
const actual: string = sc.decode(m.data);
const actual: string = m.string();
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, Base64Codec static functions are provided by @nats-io/obj

Base64Codec.encode()
Base64Codec.decode()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The NATS examples in their own documentation are out of date, but I found test cases which use string(), so I picked those. I do not think this PR should deviate to use the suggested Base64Codec.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think toString is a suitable transport encoding to use even in an example.

private values = new Map<string, string | undefined>();

constructor(image = "nats:2.8.4-alpine") {
constructor(image = "nats:2.11.3-alpine") {
Copy link
Contributor

@joebowbeer joebowbeer May 22, 2025

Choose a reason for hiding this comment

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

@cristianrgreco I noticed that the other NATS Testcontainers (Go, .NET, Python) are defaulting to a scratch image (nats:2.9) which is actually smaller than the alpine image.

More importantly, the two images are not interchangeable. This Testcontainer's code requires the alpine base image, as explained below.

The standard image's Dockerfile provides nats-server in its ENTRYPOINT whereas the alpine Dockerfile combines nats-server with the rest of CMD. As a result, if the NodeJS user specifies NatsContainer("nats:2.11") instead of NatsContainer() or NatsContainer("nats:alpine") then the container will crash.

Would you be open to switching to the standard scratch nats image here, for consistency with other implementations?

Actually, I think best of both worlds can be achieved by simply omitting "nats-server" here:

const result: string[] = ["nats-server"];

Then the alpine entry point script below will add it, and then this Testcontainer will also work if the scratch "nats" image is used instead of alpine:

https://github.com/nats-io/nats-docker/blob/main/2.11.x/alpine3.21/docker-entrypoint.sh

In other words, I think the default image can still be alpine and this Testcontainer can also be compatible with the scratch images.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@joebowbeer Nice observation! Sounds good to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@joebowbeer Can that be done in a separate pull request? I need a working NATS testcontainer to be unblocked. An optimal NATS testcontainer would be a future goal.

Copy link
Collaborator

@cristianrgreco cristianrgreco May 22, 2025

Choose a reason for hiding this comment

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

@jdinsel-xealth I agree it can be done in a separate PR. What I don't understand is, this PR is now only changing a dev dependency used only by this repository's tests, and updating a test. What part of this is blocking you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See issue #1008. While using the example provided for this testcontainer, there was an unknown exception caught in the connect operation which returned a connection refused message, but this was crafted by the NATS code. The root cause is not fully known.

console.log
      testcontainers:containers [949c57f40de8] [1] 2025/05/22 19:11:45.548358 [INF] Server is ready +1ms

      at Logger.debug [as logger] (../node_modules/debug/src/common.js:113:10)

  console.log
    socket error AggregateError: 
        at internalConnectMultiple (node:net:1139:18)
        at afterConnectMultiple (node:net:1714:7)
        at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
      code: 'ECONNREFUSED',
      [errors]: [
        Error: connect ECONNREFUSED ::1:33412
            at createConnectionError (node:net:1677:14)
            at afterConnectMultiple (node:net:1707:16)
            at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
          errno: -61,
          code: 'ECONNREFUSED',
          syscall: 'connect',
          address: '::1',
          port: 33412
        },
        Error: connect ECONNREFUSED 127.0.0.1:33412
            at createConnectionError (node:net:1677:14)
            at afterConnectMultiple (node:net:1707:16)
            at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
          errno: -61,
          code: 'ECONNREFUSED',
          syscall: 'connect',
          address: '127.0.0.1',
          port: 33412
        }
      ]
    }

      at Socket.<anonymous> (../node_modules/@nats-io/transport-node/src/node_transport.ts:128:7)

  console.log
    socket close

      at Socket.<anonymous> (../node_modules/@nats-io/transport-node/src/node_transport.ts:132:15)

  console.error
    Error connecting to NATS: ConnectionError: connection refused
        at NodeTransport.connect (/Users/jeremy.dinsel/git/xealth-patient-engagement-service/node_modules/@nats-io/transport-node/src/node_transport.ts:113:11)
        at processTicksAndRejections (node:internal/process/task_queues:105:5)
        at ProtocolHandler.dial (/Users/jeremy.dinsel/git/xealth-patient-engagement-service/node_modules/@nats-io/nats-core/build/src/protocol.ts:552:7)
        at ProtocolHandler._doDial (/Users/jeremy.dinsel/git/xealth-patient-engagement-service/node_modules/@nats-io/nats-core/build/src/protocol.ts:600:9)
        at ProtocolHandler.dodialLoop (/Users/jeremy.dinsel/git/xealth-patient-engagement-service/node_modules/@nats-io/nats-core/build/src/protocol.ts:652:11) {
      [cause]: AggregateError: 
          at internalConnectMultiple (node:net:1139:18)
          at afterConnectMultiple (node:net:1714:7)
          at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
        code: 'ECONNREFUSED',
        [errors]: [ [Error], [Error] ]
      }
    }

Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume then you're having the same error after the changes made in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't explain the differences in the repositories. I've even created a separate repository with only vitest, @testcontainers/nats, and the two @nats-io dependencies. The only code in the repository is the example test from this repository for NATS. It fails with the same errors I've reported.

Copy link
Collaborator

@cristianrgreco cristianrgreco May 22, 2025

Choose a reason for hiding this comment

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

Right.. well I've no idea how this PR relates to #1008, but I guess thanks for updating the library used for our NATS tests! LMK if you're planning on making any additional changes, else LGTM.

@joebowbeer A separate PR with what you proposed would be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the .devcontainer from this repository to my test repository and the connection with NATS succeeds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I found a solution with some combination of information from https://node.testcontainers.org/supported-container-runtimes/#usage_2. I'll post the details in the related issue. Please decide if you want to keep this PR or close it.

@jdinsel-xealth
Copy link
Contributor Author

This will have to be a breaking change because of the change to the default image. Merge/release will have to wait a bit.

What would the timeline be? I'll revert the default container version as tests pass with it using either version.

@cristianrgreco cristianrgreco added patch Backward compatible bug fix and removed major An incompatible API change labels May 22, 2025
@cristianrgreco cristianrgreco changed the title Upgrade NATS container image and dev library Upgrade NATS library used for tests May 22, 2025
joebowbeer

This comment was marked as duplicate.

Copy link
Contributor

@joebowbeer joebowbeer left a comment

Choose a reason for hiding this comment

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

LGTM - I can add-back the (Base64)Codec in a subsequent PR

@cristianrgreco cristianrgreco merged commit b33050b into testcontainers:main May 22, 2025
263 checks passed
@joebowbeer
Copy link
Contributor

@cristianrgreco @jdinsel-xealth PR to add a codec to the "pubsub" sample code and add compatibility with the "nats" scratch images:

#1011

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

Labels

maintenance Improvements that do not change functionality patch Backward compatible bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants