diff --git a/controller/controller.go b/controller/controller.go index 7a1adbc815701addecd675dafed863cd843c6aa1..d0d6ceee1862d3e9342210cd967a77f8697fb81f 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -173,8 +173,11 @@ func initialize() error { } coreLock.Lock() - startHttpServer() - coreLock.Unlock() + defer coreLock.Unlock() + err = startHttpServer() + if err != nil { + return err + } return nil } diff --git a/controller/http.go b/controller/http.go index 1261577acfdd0a70e1e6053acfaebda154467d64..9270eebceb61ebeae7dcaa1b468f7f06e3b6f21b 100644 --- a/controller/http.go +++ b/controller/http.go @@ -35,7 +35,7 @@ func stopHttpServer() error { return err } -func run() error { +func setupHttpServer() error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() @@ -47,7 +47,6 @@ func run() error { ) err := registerHttpHandler(mux) - if err != nil { return err } @@ -90,18 +89,20 @@ func run() error { // Set the HTTP server of core to the new server c.httpServer = &http.Server{Addr: ":8080", Handler: mux} - // Start HTTP server (and proxy calls to gRPC server endpoint) - return c.httpServer.ListenAndServe() + return nil } -func startHttpServer() { - go func() { - if err := run(); err != nil { - log.Info(err) - } - }() +func startHttpServer() error { + if err := setupHttpServer(); err != nil { + log.Info(err) + return err + } + + // Start HTTP server (and proxy calls to gRPC server endpoint) + go c.httpServer.ListenAndServe() //nolint:errcheck log.Info("Server exiting") + return nil } func registerHttpHandler(mux *runtime.ServeMux) error { diff --git a/controller/http_test.go b/controller/http_test.go index 06398ea890832f6eba316d38bc65d7e5465db64a..6d2455936bf2a14a5a0531447f2ac1afa6c4ca35 100644 --- a/controller/http_test.go +++ b/controller/http_test.go @@ -6,6 +6,8 @@ import ( "net/http" "testing" "time" + + "github.com/stretchr/testify/assert" ) func Test_httpApi(t *testing.T) { @@ -34,11 +36,13 @@ func Test_httpApi(t *testing.T) { wantErr: false, }, } + coreLock.Lock() - startHttpServer() - coreLock.Unlock() + defer coreLock.Unlock() + err := startHttpServer() + assert.NoError(t, err) - err := waitForHTTPServer() + err = waitForHTTPServer() if err != nil { t.Errorf("httpApi() error = %v", err) return @@ -63,7 +67,7 @@ func waitForHTTPServer() error { for i := 0; i < 10; i++ { conn, err := net.DialTimeout("tcp", ":8080", 1*time.Second) if err != nil { - time.Sleep(50 * time.Millisecond) + time.Sleep(2 * time.Second) continue } err = conn.Close() diff --git a/controller/initialise_test.go b/controller/initialise_test.go index d76a591b9e9b67bf0c809d7ea96c7b3ca3613720..adbc51ce6b535f87f6c31c355a2c003c71de0fc2 100644 --- a/controller/initialise_test.go +++ b/controller/initialise_test.go @@ -21,6 +21,8 @@ func TestMain(m *testing.M) { log.SetReportCaller(true) log.SetLevel(config.LogLevel) + c = &Core{} + readTestUUIDs() os.Exit(m.Run()) }