Skip to content

nats.ConnectedHandler is not being called #1870

@mojixcoder

Description

@mojixcoder

Observed behavior

After running a nats test server using this package github.com/nats-io/nats-server/v2/test and connecting to the server in tests using a code like this I saw that nats.ConnectHandler is not called after the initial connection is established. I expected to panic but the test doesn't panic

opts := []nats.Option{
	nats.PingInterval(5*time.Second),
	nats.MaxPingsOutstanding(5),
	nats.MaxReconnects(-1),
	nats.RetryOnFailedConnect(true),
	nats.ReconnectHandler(func(c *nats.Conn) {
		slog.Info("successfully reconnected to NATS")
	}),
	nats.ConnectHandler(func(c *nats.Conn) {
		panic("called")
	}),
	nats.ReconnectErrHandler(func(c *nats.Conn, err error) {
		slog.Error("error in reconnecting to NATS", slog.String("error", err.Error()))
	}),
	nats.DisconnectHandler(func(c *nats.Conn) {
		slog.Info("successfully disconnected from NATS")
	}),
}

conn, err := nats.Connect(url, opts...)

Expected behavior

Test should panic

Server and client version

Server: github.com/nats-io/nats-server/v2 v2.11.3
Client: github.com/nats-io/nats.go v1.41.2

Host environment

No response

Steps to reproduce

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectSuspected defect such as a bug or regression

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions