(t *testing.T)
| 162 | } |
| 163 | |
| 164 | func TestTokenHandlerReconnect(t *testing.T) { |
| 165 | var servers = []string{ |
| 166 | "nats://127.0.0.1:8232", |
| 167 | "nats://127.0.0.1:8233", |
| 168 | } |
| 169 | |
| 170 | ts := RunServerOnPort(8232) |
| 171 | defer ts.Shutdown() |
| 172 | |
| 173 | opts2 := test.DefaultTestOptions |
| 174 | opts2.Port = 8233 |
| 175 | secret := "S3Cr3T0k3n!" |
| 176 | opts2.Authorization = secret |
| 177 | ts2 := RunServerWithOptions(&opts2) |
| 178 | defer ts2.Shutdown() |
| 179 | |
| 180 | reconnectch := make(chan bool) |
| 181 | defer close(reconnectch) |
| 182 | |
| 183 | copts := nats.GetDefaultOptions() |
| 184 | copts.Servers = servers |
| 185 | copts.AllowReconnect = true |
| 186 | copts.NoRandomize = true |
| 187 | copts.MaxReconnect = 10 |
| 188 | copts.ReconnectWait = 100 * time.Millisecond |
| 189 | nats.ReconnectJitter(0, 0)(&copts) |
| 190 | |
| 191 | copts.TokenHandler = func() string { |
| 192 | return secret |
| 193 | } |
| 194 | |
| 195 | copts.ReconnectedCB = func(_ *nats.Conn) { |
| 196 | reconnectch <- true |
| 197 | } |
| 198 | |
| 199 | // Connect |
| 200 | nc, err := copts.Connect() |
| 201 | if err != nil { |
| 202 | t.Fatalf("Should have connected ok: %v", err) |
| 203 | } |
| 204 | defer nc.Close() |
| 205 | |
| 206 | // Stop the server |
| 207 | ts.Shutdown() |
| 208 | |
| 209 | // The client will try to connect to the second server and succeed. |
| 210 | |
| 211 | // Wait for the reconnect CB. |
| 212 | if e := Wait(reconnectch); e != nil { |
| 213 | t.Fatal("Reconnect callback should have been triggered") |
| 214 | } |
| 215 | |
| 216 | if nc.IsClosed() { |
| 217 | t.Fatal("Should have reconnected") |
| 218 | } |
| 219 | |
| 220 | if nc.ConnectedUrl() != servers[1] { |
| 221 | t.Fatalf("Should have reconnected to %s, reconnected to %s instead", servers[1], nc.ConnectedUrl()) |
nothing calls this directly
no test coverage detected