| 404 | } |
| 405 | |
| 406 | func TestProperFalloutAfterMaxAttempts(t *testing.T) { |
| 407 | s1 := RunServerOnPort(1222) |
| 408 | defer s1.Shutdown() |
| 409 | |
| 410 | opts := nats.GetDefaultOptions() |
| 411 | // Reduce the list of servers for Windows tests |
| 412 | if runtime.GOOS == "windows" { |
| 413 | opts.Servers = testServers[:2] |
| 414 | opts.MaxReconnect = 2 |
| 415 | opts.Timeout = 100 * time.Millisecond |
| 416 | } else { |
| 417 | opts.Servers = testServers |
| 418 | opts.MaxReconnect = 5 |
| 419 | } |
| 420 | opts.NoRandomize = true |
| 421 | opts.ReconnectWait = (25 * time.Millisecond) |
| 422 | nats.ReconnectJitter(0, 0)(&opts) |
| 423 | |
| 424 | dch := make(chan bool) |
| 425 | opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) { |
| 426 | dch <- true |
| 427 | } |
| 428 | |
| 429 | closedCbCalled := false |
| 430 | cch := make(chan bool) |
| 431 | |
| 432 | opts.ClosedCB = func(_ *nats.Conn) { |
| 433 | closedCbCalled = true |
| 434 | cch <- true |
| 435 | } |
| 436 | |
| 437 | nc, err := opts.Connect() |
| 438 | if err != nil { |
| 439 | t.Fatalf("Expected to connect, got err: %v\n", err) |
| 440 | } |
| 441 | defer nc.Close() |
| 442 | |
| 443 | s1.Shutdown() |
| 444 | |
| 445 | // On Windows, creating a TCP connection to a server not running takes more than |
| 446 | // a second. So be generous with the WaitTime. |
| 447 | |
| 448 | // wait for disconnect |
| 449 | if e := WaitTime(dch, 5*time.Second); e != nil { |
| 450 | t.Fatal("Did not receive a disconnect callback message") |
| 451 | } |
| 452 | |
| 453 | // Wait for ClosedCB |
| 454 | if e := WaitTime(cch, 5*time.Second); e != nil { |
| 455 | t.Fatal("Did not receive a closed callback message") |
| 456 | } |
| 457 | |
| 458 | // Make sure we are not still reconnecting.. |
| 459 | if !closedCbCalled { |
| 460 | t.Logf("%+v\n", nc) |
| 461 | t.Fatal("Closed CB was not triggered, should have been.") |
| 462 | } |
| 463 | |