Skip to content

Commit 638b527

Browse files
committed
Fix race conditions
Signed-off-by: Ulysses Souza <[email protected]>
1 parent f6d37d1 commit 638b527

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

compliance_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func TestUdpPort(t *testing.T) {
133133
buf := []byte(fmt.Sprintf("{\"request\":%q}", udpValue))
134134
_, err = Conn.Write(buf)
135135
assert.NilError(h.T, err)
136-
136+
time.Sleep(time.Second) // Wait for the registration
137137
actual := h.getHttpBody(udpEntrypoint)
138138
expected := jsonResponse(udpValue)
139139
h.Check(expected, actual)
@@ -148,10 +148,11 @@ func TestScaling(t *testing.T) {
148148
specRef: "Networks-top-level-element",
149149
}
150150
h.TestUpDown(func() {
151+
time.Sleep(2 * time.Second) // Wait so the clients can register
151152
actual := h.getHttpBody(scaleEntrypoint)
152153
responseArray := Response{}
153154
err := yaml.Unmarshal([]byte(actual), &responseArray)
154155
assert.NilError(h.T, err)
155-
h.Check(responseArray.Response, "3")
156+
h.Check("3", responseArray.Response)
156157
})
157158
}

tests_helper.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ import (
88
"path/filepath"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"gopkg.in/yaml.v2"
1314
"gotest.tools/v3/assert"
1415
"gotest.tools/v3/icmd"
1516
)
1617

1718
const (
18-
commandsDir = "commands"
19-
baseSpecReference = "https://github.com/compose-spec/compose-spec/blob/master/spec.md"
19+
commandsDir = "commands"
20+
baseSpecReference = "https://github.com/compose-spec/compose-spec/blob/master/spec.md"
21+
defaultHealthCheckUrl = "http://127.0.0.1:8080/ping"
2022
)
2123

2224
type Config struct {
@@ -61,13 +63,24 @@ func (h TestHelper) TestUpDown(fun func()) {
6163
}
6264
}
6365
h.executeUp(c)
66+
h.waitHttpReady(defaultHealthCheckUrl, 5*time.Second)
6467
fun()
6568
h.executeDown(c)
6669
h.checkCleanUp(c)
6770
})
6871
}
6972
}
7073

74+
func (h TestHelper) waitHttpReady(url string, timeout time.Duration) {
75+
limit := time.Now().Add(timeout)
76+
for limit.After(time.Now()) {
77+
resp, err := http.Get(url)
78+
if err == nil && resp.StatusCode == http.StatusOK {
79+
return
80+
}
81+
}
82+
}
83+
7184
func (h TestHelper) Check(expected, actual string) {
7285
assert.Check(h.T, expected == actual, h.assertSpecReferenceMessage(expected, actual))
7386
}

0 commit comments

Comments
 (0)