From b152e28a6ca81eaa1ae6b79ca58aa412203f40c3 Mon Sep 17 00:00:00 2001 From: abdelhadi mohamed Date: Mon, 22 Jul 2024 16:06:48 +0400 Subject: [PATCH 1/3] fix TestHttpProbe/returns_the_status_code_if_the_request_is_successful --- pkg/protocols_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/protocols_test.go b/pkg/protocols_test.go index fa674c3..7b39b8b 100644 --- a/pkg/protocols_test.go +++ b/pkg/protocols_test.go @@ -25,6 +25,10 @@ func TestHttpProbe(t *testing.T) { tout := 1 * time.Second server := newTestHTTPServer(t) defer server.Close() + + // short delay to allow the server to start + time.Sleep(100 * time.Millisecond) + t.Run( "returns the status code if the request is successful", func(t *testing.T) { From 377aef666467cbf55f350420b2535a210432e38d Mon Sep 17 00:00:00 2001 From: abdelhadi mohamed Date: Mon, 22 Jul 2024 20:02:11 +0400 Subject: [PATCH 2/3] ensure the server is ready to accept connections before returning --- pkg/protocols_test.go | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/protocols_test.go b/pkg/protocols_test.go index 7b39b8b..884641a 100644 --- a/pkg/protocols_test.go +++ b/pkg/protocols_test.go @@ -25,10 +25,6 @@ func TestHttpProbe(t *testing.T) { tout := 1 * time.Second server := newTestHTTPServer(t) defer server.Close() - - // short delay to allow the server to start - time.Sleep(100 * time.Millisecond) - t.Run( "returns the status code if the request is successful", func(t *testing.T) { @@ -63,19 +59,30 @@ func TestHttpProbe(t *testing.T) { // Creates an HTTP server for testing. func newTestHTTPServer(t *testing.T) *http.Server { hostPort := net.JoinHostPort("127.0.0.1", "8080") - server := &http.Server{Addr: hostPort} - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + mux := http.NewServeMux() + server := &http.Server{ + Addr: hostPort, + Handler:mux, + } + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "pong\n") }) + + l, err := net.Listen("tcp", hostPort) + if err != nil { + t.Fatalf("create listener %v", err) + } + go func() { - err := server.ListenAndServe() - if err != nil && err != http.ErrServerClosed { + if err := server.Serve(l); err != nil && err != http.ErrServerClosed { t.Errorf("starting http server: %v", err) } }() return server } + func TestTcpProbe(t *testing.T) { tout := 1 * time.Second listen := newTestTCPServer(t) From e02129068c58147b5ef1700cb2fc67b73fb53b87 Mon Sep 17 00:00:00 2001 From: abdelhadi mohamed Date: Tue, 23 Jul 2024 17:11:47 +0400 Subject: [PATCH 3/3] cleaning up and formating --- pkg/protocols_test.go | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/pkg/protocols_test.go b/pkg/protocols_test.go index 884641a..15556ee 100644 --- a/pkg/protocols_test.go +++ b/pkg/protocols_test.go @@ -59,30 +59,23 @@ func TestHttpProbe(t *testing.T) { // Creates an HTTP server for testing. func newTestHTTPServer(t *testing.T) *http.Server { hostPort := net.JoinHostPort("127.0.0.1", "8080") - mux := http.NewServeMux() - server := &http.Server{ - Addr: hostPort, - Handler:mux, - } - - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + server := &http.Server{Addr: hostPort} + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "pong\n") }) - l, err := net.Listen("tcp", hostPort) if err != nil { t.Fatalf("create listener %v", err) } - go func() { - if err := server.Serve(l); err != nil && err != http.ErrServerClosed { + err := server.Serve(l) + if err != nil && err != http.ErrServerClosed { t.Errorf("starting http server: %v", err) } }() return server } - func TestTcpProbe(t *testing.T) { tout := 1 * time.Second listen := newTestTCPServer(t)