Skip to content
Snippets Groups Projects
Commit 11c35dc9 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

Attempt to solve http server unit test fails

See merge request !839
parent e14f8dcd
Branches
Tags
1 merge request!839Attempt to solve http server unit test fails
Pipeline #189182 passed
......@@ -173,8 +173,11 @@ func initialize() error {
}
coreLock.Lock()
startHttpServer()
coreLock.Unlock()
defer coreLock.Unlock()
err = startHttpServer()
if err != nil {
return err
}
return nil
}
......
......@@ -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 {
......
......@@ -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()
......
......@@ -21,6 +21,8 @@ func TestMain(m *testing.M) {
log.SetReportCaller(true)
log.SetLevel(config.LogLevel)
c = &Core{}
readTestUUIDs()
os.Exit(m.Run())
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment