natstest

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package natstest starts an embedded, in-process nats-server with JetStream enabled for use in tests. It is a real server (no client mocks): packtrail's correctness is about NATS semantics, so every test runs against the genuine article. Restart supports fault-injection tests that bounce the server while keeping its storage.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cluster added in v1.0.0

type Cluster struct {
	NC *nats.Conn
	JS jetstream.JetStream
	// contains filtered or unexported fields
}

Cluster is a running embedded JetStream cluster.

func StartCluster added in v1.0.0

func StartCluster(t testing.TB, n int) *Cluster

StartCluster launches an n-node JetStream cluster (for replication tests) and waits until it has a meta leader.

func (*Cluster) Connect added in v1.0.0

func (c *Cluster) Connect(t testing.TB, i int) *nats.Conn

Connect returns a new connection to node i (modulo the cluster size), closed at cleanup: spreading connections over the nodes spreads the load.

type Server

type Server struct {
	NC *nats.Conn
	JS jetstream.JetStream
	// contains filtered or unexported fields
}

Server is a running embedded nats-server with a connected client.

func Start

func Start(t testing.TB) *Server

Start launches an embedded JetStream-enabled nats-server on a random port, returns a connected client and a JetStream context, and registers cleanup with the test.

func (*Server) Connect added in v0.2.0

func (s *Server) Connect(t testing.TB) *nats.Conn

Connect opens a second, independent connection to the same server, closed when the test ends. Use it where the point under test is that two separate processes share only a NATS cluster. The connection reconnects forever, so it survives Restart.

func (*Server) HasSubscription added in v1.0.0

func (s *Server) HasSubscription(subject string) bool

HasSubscription reports whether some client is subscribed to subject (in the global account).

func (*Server) Restart added in v1.0.0

func (s *Server) Restart(t testing.TB)

Restart shuts the server down and boots it again on the same port with the same storage directory: JetStream state survives, connections reconnect.

func (*Server) URL added in v1.0.0

func (s *Server) URL() string

URL returns the client URL of the server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL