Skip to content
Snippets Groups Projects
Commit a31d9051 authored by Malte Bauch's avatar Malte Bauch Committed by Fabian Seidl
Browse files

Fix: HTTP API test did not wait for server to start properly

parent dcc6af04
No related branches found
No related tags found
1 merge request!238Stfaseid http refactor
package gosdn
import (
"errors"
"net"
"net/http"
"testing"
"time"
)
func Test_httpApi(t *testing.T) {
......@@ -34,6 +37,13 @@ func Test_httpApi(t *testing.T) {
coreLock.Lock()
startHttpServer()
coreLock.Unlock()
err := waitForHTTPServer()
if err != nil {
t.Errorf("httpApi() error = %v", err)
return
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := http.Get(tt.request)
......@@ -47,3 +57,20 @@ func Test_httpApi(t *testing.T) {
})
}
}
// see: https://stackoverflow.com/a/56865986
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)
continue
}
err = conn.Close()
if err != nil {
return err
}
return nil
}
return errors.New("HTTP server could not be reached.")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment