(t *testing.T)
| 2938 | } |
| 2939 | |
| 2940 | func 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 | } |
nothing calls this directly
no test coverage detected