From 945fb0997ab92134ddd229973f7b1351671a16b5 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Mon, 8 Feb 2016 09:30:44 -0700 Subject: [PATCH 1/2] Fix missing return after Fatalf() calls. Due to clash on monitor ports, running parallel tests could cause failure to create monitor listener. The missing return would then cause a nil pointer error when starting to server http requests. --- server/server.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/server.go b/server/server.go index 127bbba7..c4ef1506 100644 --- a/server/server.go +++ b/server/server.go @@ -142,6 +142,7 @@ func (s *Server) generateServerInfoJSON() { b, err := json.Marshal(s.info) if err != nil { Fatalf("Error marshalling INFO JSON: %+v\n", err) + return } s.infoJSON = []byte(fmt.Sprintf("INFO %s %s", b, CR_LF)) } @@ -210,6 +211,7 @@ func (s *Server) Start() { if s.opts.HTTPSPort != 0 { if s.opts.TLSConfig == nil { Fatalf("TLS cert and key required for HTTPS") + return } s.StartHTTPSMonitoring() } @@ -420,6 +422,7 @@ func (s *Server) startMonitoring(secure bool) { if err != nil { Fatalf("Can't listen to the monitor port: %v", err) + return } mux := http.NewServeMux() From b6292d6d6de0c1c332590356beceb4269ea74c01 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Mon, 8 Feb 2016 09:33:39 -0700 Subject: [PATCH 2/2] Fix monitor port usage for parallel tests. Use a different MONITOR_PORT for different test packages. --- server/monitor_test.go | 94 ++++++++++++++++++++++-------------------- test/monitor_test.go | 44 ++++++++++++-------- 2 files changed, 77 insertions(+), 61 deletions(-) diff --git a/server/monitor_test.go b/server/monitor_test.go index ec31278f..a1caefc8 100644 --- a/server/monitor_test.go +++ b/server/monitor_test.go @@ -29,10 +29,16 @@ var DefaultMonitorOptions = Options{ NoSigs: true, } -func runMonitorServer(monitorPort int) *Server { +func runMonitorServer() *Server { resetPreviousHTTPConnections() opts := DefaultMonitorOptions - opts.HTTPPort = monitorPort + return RunServer(&opts) +} + +func runMonitorServerNoHttpPort() *Server { + resetPreviousHTTPConnections() + opts := DefaultMonitorOptions + opts.HTTPPort = 0 return RunServer(&opts) } @@ -74,10 +80,10 @@ func TestMyUptime(t *testing.T) { // Make sure that we do not run the http server for monitoring unless asked. func TestNoMonitorPort(t *testing.T) { - s := runMonitorServer(0) + s := runMonitorServerNoHttpPort() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) if resp, err := http.Get(url + "varz"); err == nil { t.Fatalf("Expected error: Got %+v\n", resp) } @@ -90,10 +96,10 @@ func TestNoMonitorPort(t *testing.T) { } func TestVarz(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "varz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -162,7 +168,7 @@ func TestVarz(t *testing.T) { } // Test JSONP - respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "varz?callback=callback") + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) + "varz?callback=callback") if errj != nil { t.Fatalf("Expected no error: Got %v\n", err) } @@ -174,10 +180,10 @@ func TestVarz(t *testing.T) { } func TestConnz(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -291,7 +297,7 @@ func TestConnz(t *testing.T) { } // Test JSONP - respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "connz?callback=callback") + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) + "connz?callback=callback") if errj != nil { t.Fatalf("Expected no error: Got %v\n", err) } @@ -303,13 +309,13 @@ func TestConnz(t *testing.T) { } func TestConnzWithSubs(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() nc := createClientConnSubscribeAndPublish(t) defer nc.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?subs=1") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -336,7 +342,7 @@ func TestConnzWithSubs(t *testing.T) { } func TestConnzLastActivity(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() nc := createClientConnSubscribeAndPublish(t) @@ -346,7 +352,7 @@ func TestConnzLastActivity(t *testing.T) { defer nc2.Close() pollConz := func() *Connz { - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?subs=1") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -404,10 +410,10 @@ func TestConnzLastActivity(t *testing.T) { } func TestConnzWithOffsetAndLimit(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) // Test that offset and limit ok when not enough connections resp, err := http.Get(url + "connz?offset=1&limit=1") @@ -500,7 +506,7 @@ func TestConnzWithOffsetAndLimit(t *testing.T) { } func TestConnzDefaultSorted(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() clients := make([]*nats.Conn, 4) @@ -509,7 +515,7 @@ func TestConnzDefaultSorted(t *testing.T) { defer clients[i].Close() } - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -536,7 +542,7 @@ func TestConnzDefaultSorted(t *testing.T) { } func TestConnzSortedByCid(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() clients := make([]*nats.Conn, 4) @@ -545,7 +551,7 @@ func TestConnzSortedByCid(t *testing.T) { defer clients[i].Close() } - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=cid") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -572,7 +578,7 @@ func TestConnzSortedByCid(t *testing.T) { } func TestConnzSortedByBytesAndMsgs(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() // Create a connection and make it send more messages than others @@ -589,7 +595,7 @@ func TestConnzSortedByBytesAndMsgs(t *testing.T) { defer clients[i].Close() } - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=bytes_to") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -615,7 +621,7 @@ func TestConnzSortedByBytesAndMsgs(t *testing.T) { c.Conns[0].OutBytes, c.Conns[1].OutBytes, c.Conns[2].OutBytes, c.Conns[3].OutBytes) } - url = fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url = fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err = http.Get(url + "connz?sort=msgs_to") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -641,7 +647,7 @@ func TestConnzSortedByBytesAndMsgs(t *testing.T) { c.Conns[0].OutMsgs, c.Conns[1].OutMsgs, c.Conns[2].OutMsgs, c.Conns[3].OutMsgs) } - url = fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url = fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err = http.Get(url + "connz?sort=bytes_from") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -667,7 +673,7 @@ func TestConnzSortedByBytesAndMsgs(t *testing.T) { c.Conns[0].InBytes, c.Conns[1].InBytes, c.Conns[2].InBytes, c.Conns[3].InBytes) } - url = fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url = fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err = http.Get(url + "connz?sort=msgs_from") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -695,7 +701,7 @@ func TestConnzSortedByBytesAndMsgs(t *testing.T) { } func TestConnzSortedByPending(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() firstClient := createClientConnSubscribeAndPublish(t) @@ -707,7 +713,7 @@ func TestConnzSortedByPending(t *testing.T) { } defer firstClient.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=pending") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -735,7 +741,7 @@ func TestConnzSortedByPending(t *testing.T) { } func TestConnzSortedBySubs(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() firstClient := createClientConnSubscribeAndPublish(t) @@ -747,7 +753,7 @@ func TestConnzSortedBySubs(t *testing.T) { } defer firstClient.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=subs") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -775,7 +781,7 @@ func TestConnzSortedBySubs(t *testing.T) { } func TestConnzSortedByLast(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() firstClient := createClientConnSubscribeAndPublish(t) @@ -790,7 +796,7 @@ func TestConnzSortedByLast(t *testing.T) { clients[i].Flush() } - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=last") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -818,7 +824,7 @@ func TestConnzSortedByLast(t *testing.T) { } func TestConnzSortBadRequest(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() firstClient := createClientConnSubscribeAndPublish(t) @@ -830,7 +836,7 @@ func TestConnzSortBadRequest(t *testing.T) { } defer firstClient.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?sort=foo") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -842,7 +848,7 @@ func TestConnzSortBadRequest(t *testing.T) { } func TestConnzWithRoutes(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() var opts = Options{ @@ -861,7 +867,7 @@ func TestConnzWithRoutes(t *testing.T) { time.Sleep(time.Second) - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -894,7 +900,7 @@ func TestConnzWithRoutes(t *testing.T) { } // Now check routez - url = fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url = fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err = http.Get(url + "routez") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -928,7 +934,7 @@ func TestConnzWithRoutes(t *testing.T) { } // Test JSONP - respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "routez?callback=callback") + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) + "routez?callback=callback") if errj != nil { t.Fatalf("Expected no error: Got %v\n", err) } @@ -940,13 +946,13 @@ func TestConnzWithRoutes(t *testing.T) { } func TestSubsz(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() nc := createClientConnSubscribeAndPublish(t) defer nc.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "subscriptionsz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -979,7 +985,7 @@ func TestSubsz(t *testing.T) { } // Test JSONP - respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + "subscriptionsz?callback=callback") + respj, errj := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) + "subscriptionsz?callback=callback") ct = respj.Header.Get("Content-Type") if errj != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -992,13 +998,13 @@ func TestSubsz(t *testing.T) { // Tests handle root func TestHandleRoot(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() nc := createClientConnSubscribeAndPublish(t) defer nc.Close() - resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT)) + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", MONITOR_PORT)) if err != nil { t.Fatalf("Expected no error: Got %v\n", err) } @@ -1013,14 +1019,14 @@ func TestHandleRoot(t *testing.T) { } func TestConnzWithNamedClient(t *testing.T) { - s := runMonitorServer(DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() clientName := "test-client" nc := createClientConnWithName(t, clientName) defer nc.Close() - url := fmt.Sprintf("http://localhost:%d/", DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) diff --git a/test/monitor_test.go b/test/monitor_test.go index 3c0913ac..b72e4f24 100644 --- a/test/monitor_test.go +++ b/test/monitor_test.go @@ -14,13 +14,23 @@ import ( "github.com/nats-io/gnatsd/server" ) -const MONITOR_PORT = 11422 +const CLIENT_PORT = 11422 +const MONITOR_PORT = 11522 -func runMonitorServer(monitorPort int) *server.Server { +func runMonitorServer() *server.Server { resetPreviousHTTPConnections() opts := DefaultTestOptions - opts.Port = MONITOR_PORT - opts.HTTPPort = monitorPort + opts.Port = CLIENT_PORT + opts.HTTPPort = MONITOR_PORT + + return RunServer(&opts) +} + +func runMonitorServerNoHttpPort() *server.Server { + resetPreviousHTTPConnections() + opts := DefaultTestOptions + opts.Port = CLIENT_PORT + opts.HTTPPort = 0 return RunServer(&opts) } @@ -31,10 +41,10 @@ func resetPreviousHTTPConnections() { // Make sure that we do not run the http server for monitoring unless asked. func TestNoMonitorPort(t *testing.T) { - s := runMonitorServer(0) + s := runMonitorServerNoHttpPort() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) if resp, err := http.Get(url + "varz"); err == nil { t.Fatalf("Expected error: Got %+v\n", resp) } @@ -47,10 +57,10 @@ func TestNoMonitorPort(t *testing.T) { } func TestVarz(t *testing.T) { - s := runMonitorServer(server.DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "varz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -113,10 +123,10 @@ func TestVarz(t *testing.T) { } func TestConnz(t *testing.T) { - s := runMonitorServer(server.DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -210,13 +220,13 @@ func TestConnz(t *testing.T) { } func TestConnzWithSubs(t *testing.T) { - s := runMonitorServer(server.DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() cl := createClientConnSubscribeAndPublish(t) defer cl.Close() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?subs=1") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -243,7 +253,7 @@ func TestConnzWithSubs(t *testing.T) { } func TestConnzWithOffsetAndLimit(t *testing.T) { - s := runMonitorServer(server.DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() cl1 := createClientConnSubscribeAndPublish(t) @@ -252,7 +262,7 @@ func TestConnzWithOffsetAndLimit(t *testing.T) { cl2 := createClientConnSubscribeAndPublish(t) defer cl2.Close() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "connz?offset=1&limit=1") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -285,13 +295,13 @@ func TestConnzWithOffsetAndLimit(t *testing.T) { } func TestSubsz(t *testing.T) { - s := runMonitorServer(server.DEFAULT_HTTP_PORT) + s := runMonitorServer() defer s.Shutdown() cl := createClientConnSubscribeAndPublish(t) defer cl.Close() - url := fmt.Sprintf("http://localhost:%d/", server.DEFAULT_HTTP_PORT) + url := fmt.Sprintf("http://localhost:%d/", MONITOR_PORT) resp, err := http.Get(url + "subscriptionsz") if err != nil { t.Fatalf("Expected no error: Got %v\n", err) @@ -318,7 +328,7 @@ func TestSubsz(t *testing.T) { // Create a connection to test ConnInfo func createClientConnSubscribeAndPublish(t *testing.T) net.Conn { - cl := createClientConn(t, "localhost", MONITOR_PORT) + cl := createClientConn(t, "localhost", CLIENT_PORT) send := sendCommand(t, cl) send, expect := setupConn(t, cl)