| 126 | } |
| 127 | |
| 128 | func TestConnCloseBreaksReconnectLoop(t *testing.T) { |
| 129 | ts := startReconnectServer(t) |
| 130 | defer ts.Shutdown() |
| 131 | |
| 132 | cch := make(chan bool) |
| 133 | |
| 134 | opts := reconnectOpts |
| 135 | // Bump the max reconnect attempts |
| 136 | opts.MaxReconnect = 100 |
| 137 | opts.ClosedCB = func(_ *nats.Conn) { |
| 138 | cch <- true |
| 139 | } |
| 140 | nc, err := opts.Connect() |
| 141 | if err != nil { |
| 142 | t.Fatalf("Should have connected ok: %v", err) |
| 143 | } |
| 144 | defer nc.Close() |
| 145 | nc.Flush() |
| 146 | |
| 147 | // Shutdown the server |
| 148 | ts.Shutdown() |
| 149 | |
| 150 | // Wait a second, then close the connection |
| 151 | time.Sleep(time.Second) |
| 152 | |
| 153 | // Close the connection, this should break the reconnect loop. |
| 154 | // Do this in a go routine since the issue was that Close() |
| 155 | // would block until the reconnect loop is done. |
| 156 | go nc.Close() |
| 157 | |
| 158 | // Even on Windows (where a createConn takes more than a second) |
| 159 | // we should be able to break the reconnect loop with the following |
| 160 | // timeout. |
| 161 | if err := WaitTime(cch, 3*time.Second); err != nil { |
| 162 | t.Fatal("Did not get a closed callback") |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestBasicReconnectFunctionality(t *testing.T) { |
| 167 | ts := startReconnectServer(t) |