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