@@ -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
4356kill_port_3000
4457
@@ -71,7 +84,7 @@ pass "flox install go"
7184# Run app with flox and test endpoints
7285flox activate -- go run main.go quotes.json &
7386APP_PID=$!
74- sleep 3
87+ wait_for_server || fail " Server failed to start "
7588
7689# Test endpoints
7790curl -s http://localhost:3000/ | grep -q " GET /quotes" && pass " GET / returns endpoints" || fail " GET / failed"
@@ -110,13 +123,30 @@ pass "Added redis service config"
110123cat > test_redis.sh << 'TESTSCRIPT '
111124#!/usr/bin/env bash
112125set -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
114144redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
115145echo "Quotes loaded into Redis"
116146
117147go run main.go redis &
118148APP_PID=$!
119- sleep 3
149+ wait_for_server
120150RESULT=$(curl -s http://localhost:3000/quotes)
121151kill $APP_PID 2>/dev/null || true
122152wait $APP_PID 2>/dev/null || true
@@ -150,11 +180,29 @@ pass "Replaced manifest with includes"
150180# Create helper script to test composed environment
151181cat > 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
154202redis-cli -p $REDISPORT SET quotesjson "$(cat quotes.json)"
155203go run main.go redis &
156204APP_PID=$!
157- sleep 3
205+ wait_for_server
158206RESULT=$(curl -s http://localhost:3000/quotes)
159207kill $APP_PID 2>/dev/null || true
160208echo "$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 &
191239APP_PID=$!
192- sleep 2
240+ wait_for_server || fail " Built binary failed to start "
193241
194242curl -s http://localhost:3000/quotes | grep -q " Steve Jobs" && pass " Built binary serves quotes" || fail " Built binary failed"
195243
0 commit comments