chore(test): fix integration test 'waitForReady'
This commit is contained in:
32
main_test.go
32
main_test.go
@@ -11,7 +11,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"me-fit/log"
|
|
||||||
"me-fit/service"
|
"me-fit/service"
|
||||||
"me-fit/types"
|
"me-fit/types"
|
||||||
|
|
||||||
@@ -119,10 +118,8 @@ func setupIntegrationTest(t *testing.T) (db *sql.DB, basePath string, ctx contex
|
|||||||
|
|
||||||
basePath = "http://localhost:" + fmt.Sprint(testPort)
|
basePath = "http://localhost:" + fmt.Sprint(testPort)
|
||||||
|
|
||||||
err = waitForReady(ctx, 5*time.Second, basePath)
|
err = waitForReady(ctx, 5*time.Second, basePath, t)
|
||||||
if err != nil {
|
assert.Nil(t, err)
|
||||||
t.Fatalf("Failed to start server: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return db, basePath, ctx
|
return db, basePath, ctx
|
||||||
}
|
}
|
||||||
@@ -152,39 +149,28 @@ func waitForReady(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
timeout time.Duration,
|
timeout time.Duration,
|
||||||
endpoint string,
|
endpoint string,
|
||||||
|
t *testing.T,
|
||||||
) error {
|
) error {
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
for {
|
for {
|
||||||
req, err := http.NewRequestWithContext(
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
||||||
ctx,
|
assert.Nil(t, err)
|
||||||
http.MethodGet,
|
|
||||||
endpoint,
|
|
||||||
nil,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
log.Error("failed to create request: %v", err)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err == nil && resp.StatusCode == http.StatusOK {
|
||||||
log.Info("Error making request: %v", err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if resp.StatusCode == http.StatusOK {
|
|
||||||
log.Info("Endpoint is ready!")
|
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
return nil
|
return nil
|
||||||
}
|
} else if err == nil {
|
||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
default:
|
default:
|
||||||
if time.Since(startTime) >= timeout {
|
if time.Since(startTime) >= timeout {
|
||||||
log.Error("timeout reached while waiting for endpoint")
|
t.Fatal("timeout reached while waiting for endpoint")
|
||||||
return types.ErrInternal
|
return types.ErrInternal
|
||||||
}
|
}
|
||||||
// wait a little while between checks
|
// wait a little while between checks
|
||||||
|
|||||||
Reference in New Issue
Block a user