From 11c35dc964bf89032e4d3f15d65369619e4c203d Mon Sep 17 00:00:00 2001
From: Fabian Seidl <fabian.seidl@h-da.de>
Date: Wed, 17 Apr 2024 10:53:15 +0000
Subject: [PATCH] Attempt to solve http server unit test fails

See merge request danet/gosdn!839
---
 controller/controller.go      |  7 +++++--
 controller/http.go            | 21 +++++++++++----------
 controller/http_test.go       | 12 ++++++++----
 controller/initialise_test.go |  2 ++
 4 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/controller/controller.go b/controller/controller.go
index 7a1adbc81..d0d6ceee1 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 1261577ac..9270eebce 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 06398ea89..6d2455936 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 d76a591b9..adbc51ce6 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())
 }
-- 
GitLab