diff --git a/app/main_test.go b/app/main_test.go index f6963e5..3c269bf 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -219,7 +219,16 @@ func Test_activateServerOnly(t *testing.T) { assert.NoError(t, err) close(done) }() - time.Sleep(time.Millisecond * 100) + + // Wait for server to be ready + require.Eventually(t, func() bool { + resp, err := http.Get("http://localhost:9988/ping") + if err != nil { + return false + } + defer resp.Body.Close() + return resp.StatusCode == http.StatusOK + }, time.Second*2, time.Millisecond*50, "server did not start") resp, err := http.Get("http://localhost:9988/ping") require.NoError(t, err) diff --git a/app/webapi/webapi_test.go b/app/webapi/webapi_test.go index 5625f15..c575ecd 100644 --- a/app/webapi/webapi_test.go +++ b/app/webapi/webapi_test.go @@ -69,7 +69,6 @@ func TestServer_RunAuth(t *testing.T) { require.NoError(t, err) t.Logf("hashed password: %s", string(hashedPassword)) - // run two servers - one with plain password and one with bcrypt hash tests := []struct { name string srv *Server @@ -117,14 +116,23 @@ func TestServer_RunAuth(t *testing.T) { assert.NoError(t, err) close(done) }() - time.Sleep(100 * time.Millisecond) + + // Wait for server to be ready + require.Eventually(t, func() bool { + resp, err := http.Get(fmt.Sprintf("http://localhost:%s/ping", tc.port)) + if err != nil { + return false + } + defer resp.Body.Close() + return resp.StatusCode == http.StatusOK + }, time.Second*2, time.Millisecond*50, "server did not start") t.Run("ping", func(t *testing.T) { resp, err := http.Get(fmt.Sprintf("http://localhost:%s/ping", tc.port)) assert.NoError(t, err) t.Log(resp) defer resp.Body.Close() - assert.Equal(t, http.StatusOK, resp.StatusCode) // no auth on ping + assert.Equal(t, http.StatusOK, resp.StatusCode) }) t.Run("check unauthorized, no basic auth", func(t *testing.T) { @@ -175,7 +183,6 @@ func TestServer_RunAuth(t *testing.T) { }) } cancel() - // wait for all servers to complete for _, done := range doneChannels { <-done }