@@ -15,22 +15,33 @@ TEST_SERVER_PORT="8081"
15
15
TEST_DB_NAME=" test_api_${UNIQUE_ID} .db"
16
16
TEST_DB_PATH=" ${TEST_DB_NAME} "
17
17
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
+ }
26
40
27
41
setup () {
28
42
echo -e " ${YELLOW} Setting up test environment...${NC} "
29
43
echo -e " ${YELLOW} Current directory: $( pwd) ${NC} "
30
44
31
- TEST_DB_NAME=" test_api_${UNIQUE_ID} .db"
32
- TEST_DB_PATH=" ${TEST_DB_NAME} "
33
-
34
45
touch " ${TEST_DB_PATH} "
35
46
36
47
echo -e " ${YELLOW} Starting server with test database at ${TEST_DB_PATH} ...${NC} "
@@ -45,13 +56,16 @@ setup() {
45
56
cargo run &
46
57
SERVER_PID=$!
47
58
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
51
60
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
55
69
else
56
70
echo -e " ${RED} Failed to start server. Check logs for errors.${NC} "
57
71
exit 1
@@ -119,7 +133,6 @@ run_tests() {
119
133
fi
120
134
121
135
echo -e " ${GREEN} Successfully created URL with short ID: $SHORT_ID ${NC} "
122
- sleep 1
123
136
124
137
# Test 2: Create URL with custom ID
125
138
echo -e " \n${YELLOW} Test 2: Creating a shortened URL with custom ID...${NC} "
@@ -145,7 +158,6 @@ run_tests() {
145
158
fi
146
159
147
160
echo -e " ${GREEN} Successfully created URL with custom ID: $CUSTOM_ID ${NC} "
148
- sleep 1
149
161
150
162
# Test 3: Get stats for first URL
151
163
echo -e " \n${YELLOW} Test 3: Getting statistics for first URL...${NC} "
@@ -191,7 +203,6 @@ run_tests() {
191
203
fi
192
204
193
205
echo -e " ${GREEN} Redirect successful with status code: ${REDIRECT_STATUS}${NC} "
194
- sleep 1
195
206
196
207
# Test 6: Verify visit count
197
208
echo -e " \n${YELLOW} Test 6: Verifying visit count increased...${NC} "
0 commit comments