@@ -15,22 +15,33 @@ TEST_SERVER_PORT="8081"
1515TEST_DB_NAME=" test_api_${UNIQUE_ID} .db"
1616TEST_DB_PATH=" ${TEST_DB_NAME} "
1717
18- # CI environment detection
19- if [ -n " $CI " ]; then
20- STARTUP_WAIT=5
21- READY_WAIT=5
22- else
23- STARTUP_WAIT=2
24- READY_WAIT=2
25- fi
18+ wait_for_server () {
19+ local max_attempts=50
20+ local wait_seconds=2
21+ local attempt=1
22+ local base_url=" http://${TEST_SERVER_HOST} :${TEST_SERVER_PORT} "
23+
24+ echo -e " ${YELLOW} Waiting for server to be ready at ${base_url} ...${NC} "
25+
26+ while [ $attempt -le $max_attempts ]; do
27+ if curl -s --head --fail " ${base_url} /health" > /dev/null 2>&1 ; then
28+ echo -e " ${GREEN} Server is up and running after $(( attempt * wait_seconds )) seconds!${NC} "
29+ return 0
30+ fi
31+
32+ echo -e " ${YELLOW} Attempt ${attempt} /${max_attempts} : Server not ready yet, waiting ${wait_seconds} s...${NC} "
33+ sleep $wait_seconds
34+ attempt=$(( attempt + 1 ))
35+ done
36+
37+ echo -e " ${RED} Server failed to start after $(( max_attempts * wait_seconds )) seconds!${NC} "
38+ return 1
39+ }
2640
2741setup () {
2842 echo -e " ${YELLOW} Setting up test environment...${NC} "
2943 echo -e " ${YELLOW} Current directory: $( pwd) ${NC} "
3044
31- TEST_DB_NAME=" test_api_${UNIQUE_ID} .db"
32- TEST_DB_PATH=" ${TEST_DB_NAME} "
33-
3445 touch " ${TEST_DB_PATH} "
3546
3647 echo -e " ${YELLOW} Starting server with test database at ${TEST_DB_PATH} ...${NC} "
@@ -45,13 +56,16 @@ setup() {
4556 cargo run &
4657 SERVER_PID=$!
4758
48- echo -e " ${YELLOW} Waiting for server to start (PID: ${SERVER_PID} )...${NC} "
49- sleep $STARTUP_WAIT
50-
59+ # Check if server process is running
5160 if kill -0 $SERVER_PID 2> /dev/null; then
52- echo -e " ${GREEN} Server started successfully with PID: ${SERVER_PID}${NC} "
53- echo -e " ${YELLOW} Waiting for server to fully initialize...${NC} "
54- sleep $READY_WAIT
61+ echo -e " ${GREEN} Server process started with PID: ${SERVER_PID}${NC} "
62+ # Wait for server to be ready to accept connections
63+ if wait_for_server; then
64+ echo -e " ${GREEN} Server is ready to accept connections${NC} "
65+ else
66+ echo -e " ${RED} Server didn't start properly${NC} "
67+ exit 1
68+ fi
5569 else
5670 echo -e " ${RED} Failed to start server. Check logs for errors.${NC} "
5771 exit 1
@@ -119,7 +133,6 @@ run_tests() {
119133 fi
120134
121135 echo -e " ${GREEN} Successfully created URL with short ID: $SHORT_ID ${NC} "
122- sleep 1
123136
124137 # Test 2: Create URL with custom ID
125138 echo -e " \n${YELLOW} Test 2: Creating a shortened URL with custom ID...${NC} "
@@ -145,7 +158,6 @@ run_tests() {
145158 fi
146159
147160 echo -e " ${GREEN} Successfully created URL with custom ID: $CUSTOM_ID ${NC} "
148- sleep 1
149161
150162 # Test 3: Get stats for first URL
151163 echo -e " \n${YELLOW} Test 3: Getting statistics for first URL...${NC} "
@@ -191,7 +203,6 @@ run_tests() {
191203 fi
192204
193205 echo -e " ${GREEN} Redirect successful with status code: ${REDIRECT_STATUS}${NC} "
194- sleep 1
195206
196207 # Test 6: Verify visit count
197208 echo -e " \n${YELLOW} Test 6: Verifying visit count increased...${NC} "
0 commit comments