Skip to content

Commit 44cca87

Browse files
authored
Merge pull request #2 from flox/fix-ci
Fix CI: add retry loops for server readiness
2 parents 9a9fece + 86c3157 commit 44cca87

1 file changed

Lines changed: 54 additions & 6 deletions

File tree

test_workshop.sh

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ kill_port_3000() {
3939
sleep 1
4040
}
4141

42+
wait_for_server() {
43+
local retries=30
44+
local i=0
45+
while [[ $i -lt $retries ]]; do
46+
if curl -s http://localhost:3000/ > /dev/null 2>&1; then
47+
return 0
48+
fi
49+
sleep 1
50+
((i++))
51+
done
52+
return 1
53+
}
54+
4255
# Kill any existing process on port 3000
4356
kill_port_3000
4457

@@ -71,7 +84,7 @@ pass "flox install go"
7184
# Run app with flox and test endpoints
7285
flox activate -- go run main.go quotes.json &
7386
APP_PID=$!
74-
sleep 3
87+
wait_for_server || fail "Server failed to start"
7588

7689
# Test endpoints
7790
curl -s http://localhost:3000/ | grep -q "GET /quotes" && pass "GET / returns endpoints" || fail "GET / failed"
@@ -110,13 +123,30 @@ pass "Added redis service config"
110123
cat > test_redis.sh << 'TESTSCRIPT'
111124
#!/usr/bin/env bash
112125
set -e
113-
sleep 2
126+
127+
wait_for_redis() {
128+
for i in {1..30}; do
129+
redis-cli -p $REDISPORT ping > /dev/null 2>&1 && return 0
130+
sleep 1
131+
done
132+
return 1
133+
}
134+
135+
wait_for_server() {
136+
for i in {1..30}; do
137+
curl -s http://localhost:3000/ > /dev/null 2>&1 && return 0
138+
sleep 1
139+
done
140+
return 1
141+
}
142+
143+
wait_for_redis
114144
redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
115145
echo "Quotes loaded into Redis"
116146
117147
go run main.go redis &
118148
APP_PID=$!
119-
sleep 3
149+
wait_for_server
120150
RESULT=$(curl -s http://localhost:3000/quotes)
121151
kill $APP_PID 2>/dev/null || true
122152
wait $APP_PID 2>/dev/null || true
@@ -150,11 +180,29 @@ pass "Replaced manifest with includes"
150180
# Create helper script to test composed environment
151181
cat > test_composed.sh << 'TESTSCRIPT'
152182
#!/usr/bin/env bash
153-
sleep 2
183+
set -e
184+
185+
wait_for_redis() {
186+
for i in {1..30}; do
187+
redis-cli -p $REDISPORT ping > /dev/null 2>&1 && return 0
188+
sleep 1
189+
done
190+
return 1
191+
}
192+
193+
wait_for_server() {
194+
for i in {1..30}; do
195+
curl -s http://localhost:3000/ > /dev/null 2>&1 && return 0
196+
sleep 1
197+
done
198+
return 1
199+
}
200+
201+
wait_for_redis
154202
redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
155203
go run main.go redis &
156204
APP_PID=$!
157-
sleep 3
205+
wait_for_server
158206
RESULT=$(curl -s http://localhost:3000/quotes)
159207
kill $APP_PID 2>/dev/null || true
160208
echo "$RESULT" | grep -q "Steve Jobs"
@@ -189,7 +237,7 @@ pass "flox build succeeded"
189237

190238
./result-quotes-app/bin/quotes-app ./result-quotes-app/share/quotes.json &
191239
APP_PID=$!
192-
sleep 2
240+
wait_for_server || fail "Built binary failed to start"
193241

194242
curl -s http://localhost:3000/quotes | grep -q "Steve Jobs" && pass "Built binary serves quotes" || fail "Built binary failed"
195243

0 commit comments

Comments
 (0)