Skip to content

Commit df07962

Browse files
committed
test: improve Postman test script with retry logic and better error handling for Rails server startup
1 parent dfd9d9a commit df07962

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

travis/postman.sh

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,46 @@ set -ex
55
# Set up fresh db
66
bundle exec rake db:setup db:populate
77

8-
bundle exec rails server &
8+
# Start Rails server in background
9+
bundle exec rails server -b 0.0.0.0 &
910
rails_pid=$!
10-
sleep 10 # Make sure server is up before continuing
1111

1212
# Kill Rails process on exit
13-
trap "kill $rails_pid" EXIT
13+
trap "kill $rails_pid 2>/dev/null || true" EXIT
14+
15+
# Wait for Rails server to be ready with retry logic
16+
echo "Waiting for Rails server to start..."
17+
max_attempts=30
18+
attempt=0
19+
while [ $attempt -lt $max_attempts ]; do
20+
if curl -f -s http://localhost:3000/resources?category_id=1 >/dev/null 2>&1; then
21+
echo "Rails server is ready!"
22+
break
23+
fi
24+
25+
# Check if the server process is still running
26+
if ! kill -0 $rails_pid 2>/dev/null; then
27+
echo "ERROR: Rails server process died unexpectedly"
28+
wait $rails_pid || true
29+
exit 1
30+
fi
31+
32+
attempt=$((attempt + 1))
33+
echo "Server not ready yet, waiting... (attempt $attempt/$max_attempts)"
34+
sleep 2
35+
done
36+
37+
if [ $attempt -eq $max_attempts ]; then
38+
echo "ERROR: Rails server did not become ready after $max_attempts attempts"
39+
echo "Checking server logs..."
40+
kill $rails_pid 2>/dev/null || true
41+
wait $rails_pid || true
42+
exit 1
43+
fi
1444

1545
# Hit a page to force Rails to cache the loading of modules, avoiding a timeout
1646
# failure on the first Postman test.
17-
curl -v http://localhost:3000/resources?category_id=1 >/dev/null
47+
curl -f -v http://localhost:3000/resources?category_id=1 >/dev/null
1848

1949
newman run postman/AskDarcel%20API.postman_collection.json -g postman/globals.postman_globals.json
2050
newman run postman/AskDarcel%20Admin%20API.postman_collection.json -g postman/globals.postman_globals.json

0 commit comments

Comments
 (0)