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

Function TestRetryOnFailedConnect

test/conn_test.go:2940–3052  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2938}
2939
2940func TestRetryOnFailedConnect(t *testing.T) {
2941 nc, err := nats.Connect(nats.DefaultURL)
2942 if err == nil {
2943 nc.Close()
2944 t.Fatal("Expected error, did not get one")
2945 }
2946 reconnectedCh := make(chan bool, 1)
2947 connectedCh := make(chan bool, 1)
2948 dch := make(chan bool, 1)
2949 nc, err = nats.Connect(nats.DefaultURL,
2950 nats.RetryOnFailedConnect(true),
2951 nats.MaxReconnects(-1),
2952 nats.ReconnectWait(15*time.Millisecond),
2953 nats.DisconnectErrHandler(func(_ *nats.Conn, _ error) {
2954 dch <- true
2955 }),
2956 nats.ConnectHandler(func(_ *nats.Conn) {
2957 connectedCh <- true
2958 }),
2959 nats.ReconnectHandler(func(_ *nats.Conn) {
2960 reconnectedCh <- true
2961 }),
2962 nats.NoCallbacksAfterClientClose())
2963 if err != nil {
2964 t.Fatalf("Unexpected error: %v", err)
2965 }
2966 sub, err := nc.SubscribeSync("foo")
2967 if err != nil {
2968 t.Fatalf("Error on subscribe: %v", err)
2969 }
2970 if err := nc.Publish("foo", []byte("msg")); err != nil {
2971 t.Fatalf("Error on publish: %v", err)
2972 }
2973 for i := 0; i < 2; i++ {
2974 // Start server now
2975 s := RunDefaultServer()
2976 defer s.Shutdown()
2977
2978 switch i {
2979 case 0:
2980 select {
2981 case <-connectedCh:
2982 case <-time.After(2 * time.Second):
2983 t.Fatal("Should have connected")
2984 }
2985 case 1:
2986 select {
2987 case <-reconnectedCh:
2988 case <-time.After(2 * time.Second):
2989 t.Fatal("Should have reconnected")
2990 }
2991 }
2992
2993 // Now make sure that the pub worked and sub worked.
2994 // We should receive the message we have published.
2995 if _, err := sub.NextMsg(time.Second); err != nil {
2996 t.Fatalf("Iter=%v - did not receive message: %v", i, err)
2997 }

Callers

nothing calls this directly

Calls 11

ConnectMethod · 0.80
DisconnectErrHandlerMethod · 0.80
ReconnectHandlerMethod · 0.80
FatalfMethod · 0.80
NextMsgMethod · 0.80
ClosedHandlerMethod · 0.80
RunDefaultServerFunction · 0.70
RunServerWithOptionsFunction · 0.70
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected