func TestConnClosedOnConnectFailure(t *testing.T) { s := natsd.RunDefaultServer() defer s.Shutdown() // Non-Existent or Unreachable connectTime := 25 * time.Millisecond if _, err := Connect("someNonExistentServerID", "myTestClient", ConnectWait(connectTime)); err != ErrConnectReqTimeout { t.Fatalf("Expected Unreachable err, got %v\n", err) } // Check that the underlying NATS connection has been closed. // We will first stop the server. If we have left the NATS connection // opened, it should be trying to reconnect. s.Shutdown() // Wait a bit time.Sleep(500 * time.Millisecond) // Inspecting go routines in search for a doReconnect buf := make([]byte, 10000) n := runtime.Stack(buf, true) if strings.Contains(string(buf[:n]), "doReconnect") { t.Fatal("NATS Connection suspected to not have been closed") } }
func TestUnreachable(t *testing.T) { s := natsd.RunDefaultServer() defer s.Shutdown() // Non-Existent or Unreachable connectTime := 25 * time.Millisecond start := time.Now() if _, err := Connect("someNonExistentServerID", "myTestClient", ConnectWait(connectTime)); err != ErrConnectReqTimeout { t.Fatalf("Expected Unreachable err, got %v\n", err) } if delta := time.Since(start); delta < connectTime { t.Fatalf("Expected to wait at least %v, but only waited %v\n", connectTime, delta) } }
// Create a local messaging broker, usually for testing purpose func RunDefaultServer() *server.Server { test.DefaultTestOptions.Port = 22222 return test.RunDefaultServer() }