MCPcopy
hub / github.com/nats-io/nats.go / TestOptions

Function TestOptions

test/basic_test.go:1002–1059  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1000}
1001
1002func TestOptions(t *testing.T) {
1003 s := RunDefaultServer()
1004 defer s.Shutdown()
1005
1006 nc, err := nats.Connect(nats.DefaultURL,
1007 nats.Name("myName"),
1008 nats.MaxReconnects(2),
1009 nats.ReconnectWait(50*time.Millisecond),
1010 nats.ReconnectJitter(0, 0),
1011 nats.PingInterval(20*time.Millisecond))
1012 if err != nil {
1013 t.Fatalf("Failed to connect: %v", err)
1014 }
1015 defer nc.Close()
1016
1017 rch := make(chan bool)
1018 cch := make(chan bool)
1019
1020 nc.SetReconnectHandler(func(_ *nats.Conn) { rch <- true })
1021 nc.SetClosedHandler(func(_ *nats.Conn) { cch <- true })
1022
1023 s.Shutdown()
1024
1025 s = RunDefaultServer()
1026 defer s.Shutdown()
1027
1028 if err := Wait(rch); err != nil {
1029 t.Fatal("Failed getting reconnected cb")
1030 }
1031
1032 nc.Close()
1033
1034 if err := Wait(cch); err != nil {
1035 t.Fatal("Failed getting closed cb")
1036 }
1037
1038 nc, err = nats.Connect(nats.DefaultURL, nats.NoReconnect())
1039 if err != nil {
1040 t.Fatalf("Failed to connect: %v", err)
1041 }
1042 defer nc.Close()
1043
1044 nc.SetReconnectHandler(func(_ *nats.Conn) { rch <- true })
1045 nc.SetClosedHandler(func(_ *nats.Conn) { cch <- true })
1046
1047 s.Shutdown()
1048
1049 // We should not get a reconnect cb this time
1050 if err := WaitTime(rch, time.Second); err == nil {
1051 t.Fatal("Unexpected reconnect cb")
1052 }
1053
1054 nc.Close()
1055
1056 if err := Wait(cch); err != nil {
1057 t.Fatal("Failed getting closed cb")
1058 }
1059}

Callers

nothing calls this directly

Calls 9

ConnectMethod · 0.80
FatalfMethod · 0.80
SetReconnectHandlerMethod · 0.80
RunDefaultServerFunction · 0.70
WaitFunction · 0.70
WaitTimeFunction · 0.70
NameMethod · 0.65
CloseMethod · 0.45
SetClosedHandlerMethod · 0.45

Tested by

no test coverage detected