From eb0eabff04d41c1eda823dcfcb9fbd66ee80b76c Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Wed, 3 Feb 2016 18:58:42 -0700 Subject: [PATCH] Replace all changes with simple 25ms delay after connection close. --- server/monitor_test.go | 11 --------- server/server_test.go | 36 +++++------------------------ test/test.go | 52 ++++++++++++------------------------------ 3 files changed, 21 insertions(+), 78 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index 52309de4..4c13e018 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -177,17 +177,6 @@ func TestConnz(t *testing.T) { s := runMonitorServer(DEFAULT_HTTP_PORT) defer s.Shutdown() - // Wait a bit to make sure that the "client" that was created - // by the connection checking that the server is started is gone. - end := time.Now().Add(5 * time.Second) - for time.Now().Before(end) { - if s.NumClients() > 0 { - time.Sleep(10 * time.Millisecond) - } else { - break - } - } - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) resp, err := http.Get(url + "connz") if err != nil { diff --git a/server/server_test.go b/server/server_test.go index ab79f314..b23f3e74 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -3,7 +3,6 @@ package server import ( - "bufio" "net" "testing" "time" @@ -19,32 +18,6 @@ var DefaultOptions = Options{ NoSigs: true, } -// completeConnection ensures that the server has fully processed (and assigned -// a client id) to the connection created to check that the server has started. -// This is important for tests that expect connections to have a predictable -// value. -func completeConnection(conn net.Conn) error { - // Close the connection on exit - defer conn.Close() - - buf := bufio.NewReader(conn) - - // Consume the INFO protocol - _, err := buf.ReadString('\n') - if err == nil { - // Send a PING - _, err = conn.Write([]byte("PING\r\n")) - } - if err == nil { - // Expect a PONG, but could be -ERR. We don't really care, - // the point is that if we received something, the client - // is initialized. - _, err = buf.ReadString('\n') - } - - return err -} - // New Go Routine based server func RunServer(opts *Options) *Server { if opts == nil { @@ -72,9 +45,12 @@ func RunServer(opts *Options) *Server { time.Sleep(50 * time.Millisecond) continue } - if err := completeConnection(conn); err != nil { - break - } + conn.Close() + // Wait a bit to give a chance to the server to remove this + // "client" from its state, which may otherwise interfere with + // some tests. + time.Sleep(25 * time.Millisecond) + return s } panic("Unable to start NATS Server in Go Routine") diff --git a/test/test.go b/test/test.go index 0bb4b2b4..b0effa8a 100644 --- a/test/test.go +++ b/test/test.go @@ -3,7 +3,6 @@ package test import ( - "bufio" "crypto/rand" "encoding/hex" "encoding/json" @@ -73,32 +72,6 @@ func RunServerWithConfig(configFile string) (srv *server.Server, opts *server.Op return } -// completeConnection ensures that the server has fully processed (and assigned -// a client id) to the connection created to check that the server has started. -// This is important for tests that expect connections to have a predictable -// value. -func completeConnection(conn net.Conn) error { - // Close the connection on exit - defer conn.Close() - - buf := bufio.NewReader(conn) - - // Consume the INFO protocol - _, err := buf.ReadString('\n') - if err == nil { - // Send a PING - _, err = conn.Write([]byte("PING\r\n")) - } - if err == nil { - // Expect a PONG, but could be -ERR. We don't really care, - // the point is that if we received something, the client - // is initialized. - _, err = buf.ReadString('\n') - } - - return err -} - // New Go Routine based server with auth func RunServerWithAuth(opts *server.Options, auth server.Auth) *server.Server { if opts == nil { @@ -130,9 +103,11 @@ func RunServerWithAuth(opts *server.Options, auth server.Auth) *server.Server { time.Sleep(50 * time.Millisecond) continue } - if err := completeConnection(conn); err != nil { - break - } + conn.Close() + // Wait a bit to give a chance to the server to remove this + // "client" from its state, which may otherwise interfere with + // some tests. + time.Sleep(25 * time.Millisecond) return s } panic("Unable to start NATS Server in Go Routine") @@ -161,10 +136,11 @@ func startServer(t tLogger, port int, other string) *natsServer { return nil } } else { - if err := completeConnection(c); err != nil { - t.Fatalf("Error trying to connect to %s: %v", natsServerExe, err) - return nil - } + c.Close() + // Wait a bit to give a chance to the server to remove this + // "client" from its state, which may otherwise interfere with + // some tests. + time.Sleep(25 * time.Millisecond) break } } @@ -242,9 +218,11 @@ func checkSocket(t tLogger, addr string, wait time.Duration) { time.Sleep(50 * time.Millisecond) continue } - if err := completeConnection(conn); err != nil { - break - } + conn.Close() + // Wait a bit to give a chance to the server to remove this + // "client" from its state, which may otherwise interfere with + // some tests. + time.Sleep(25 * time.Millisecond) return } // We have failed to bind the socket in the time allowed.