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
Branches
No related tags found
1 merge request!238Stfaseid http refactor
This commit is part of merge request !238. Comments created here will be created in the context of that merge request.
package gosdn package gosdn
import ( import (
"errors"
"net"
"net/http" "net/http"
"testing" "testing"
"time"
) )
func Test_httpApi(t *testing.T) { func Test_httpApi(t *testing.T) {
...@@ -34,6 +37,13 @@ func Test_httpApi(t *testing.T) { ...@@ -34,6 +37,13 @@ func Test_httpApi(t *testing.T) {
coreLock.Lock() coreLock.Lock()
startHttpServer() startHttpServer()
coreLock.Unlock() coreLock.Unlock()
err := waitForHTTPServer()
if err != nil {
t.Errorf("httpApi() error = %v", err)
return
}
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := http.Get(tt.request) got, err := http.Get(tt.request)
...@@ -47,3 +57,20 @@ func Test_httpApi(t *testing.T) { ...@@ -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