package link import ( "context" "fmt" "time" "github.com/nats-io/nats.go" ) // TestDial dials a second connection to the bus as login with password, on the pinned certificate and the node's one inbox, // and answers whether it authenticated (novox/hq ADR 0583 ยง3): the connection is closed at once, nothing is subscribed or said. func TestDial(ctx context.Context, m Membership, login, password string, timeout time.Duration) error { config, err := PinnedConfig(m.Fingerprint) if err != nil { return err } conn, err := nats.Connect(natsURL(m.Broker), nats.Secure(config), nats.UserInfo(login, password), nats.CustomInboxPrefix("_INBOX.node."+m.Node), nats.Name("mesh-host/"+m.Node+"/test-dial"), nats.Timeout(timeout), nats.NoReconnect()) if err != nil { return fmt.Errorf("cannot dial as %s: %w", login, err) } defer conn.Close() flush, cancel := context.WithTimeout(ctx, timeout) defer cancel() if err := conn.FlushWithContext(flush); err != nil { return fmt.Errorf("the bus did not answer as %s: %w", login, err) } return nil }