-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
53 lines (44 loc) · 1.11 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"context"
"fmt"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestMain(t *testing.T) {
_, cancel := context.WithCancel(context.Background())
t.Log("Starting server...")
go func() {
main()
}()
defer cancel()
// give it time to start
time.Sleep(1 * time.Second)
// but retry a few times if we fail
retries := 5
wait := 2 * time.Second
for i := 0; i < retries; i++ {
t.Logf("Attempt %d/%d", i+1, retries)
// send a curl request to the server
url := fmt.Sprintf("http://localhost:%s", portDefault)
req, err := http.NewRequest("GET", url, nil)
require.NoError(t, err)
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
defer resp.Body.Close()
if resp.StatusCode == http.StatusServiceUnavailable {
t.Logf("Server not ready, retrying in %v...", wait)
time.Sleep(wait)
continue
}
t.Logf("Status code: %v", resp.StatusCode)
require.Equal(t, http.StatusOK, resp.StatusCode)
// body, err := io.ReadAll(resp.Body)
// require.NoError(t, err)
// t.Logf("Request received:\n%s", string(body))
break
}
}