Skip to content

Commit 4d3b3ec

Browse files
committed
chore: add debug mode to integration tests
1 parent 3aa382d commit 4d3b3ec

File tree

3 files changed

+113
-15
lines changed

3 files changed

+113
-15
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
"types": "index.d.ts",
77
"scripts": {
88
"test:unit": "mocha --check-leaks --recursive test/unit/*.spec.js",
9+
"test:unit:debug": "NODE_DEBUG=mysql,net,stream mocha --check-leaks --recursive test/unit/*.spec.js",
910
"test:integration": "mocha --check-leaks --recursive test/integration/*.spec.js",
11+
"test:integration:debug": "NODE_DEBUG=mysql,net,stream mocha --check-leaks --recursive test/integration/*.spec.js",
1012
"test:integration:docker": "./scripts/run-integration-tests.sh",
13+
"test:integration:docker:debug": "./scripts/run-integration-tests.sh debug",
1114
"test:docker": "./scripts/run-all-tests.sh",
15+
"test:docker:debug": "./scripts/run-all-tests.sh debug",
1216
"test": "mocha --check-leaks --recursive test/{unit,integration}/*.spec.js",
17+
"test:debug": "NODE_DEBUG=mysql,net,stream mocha --check-leaks --recursive test/{unit,integration}/*.spec.js",
1318
"test-cov": "nyc --reporter=html mocha --check-leaks --recursive test/{unit,integration}/*.spec.js",
19+
"test-cov:debug": "NODE_DEBUG=mysql,net,stream nyc --reporter=html mocha --check-leaks --recursive test/{unit,integration}/*.spec.js",
1420
"lint": "eslint .",
1521
"prepublishOnly": "npm run lint && npm run test:unit"
1622
},

scripts/run-all-tests.sh

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,63 @@
22

33
# Script to run all tests (unit and integration) locally
44

5+
# Check if debug mode is enabled
6+
DEBUG_ENV=""
7+
DEBUG_PARAM=""
8+
if [ "$1" = "debug" ]; then
9+
DEBUG_ENV="NODE_DEBUG=mysql,net,stream"
10+
DEBUG_PARAM="debug"
11+
echo "Debug mode enabled. Debug logs will be displayed."
12+
fi
13+
14+
# Set max retry count for integration tests
15+
MAX_RETRIES=2
16+
RETRY_COUNT=0
17+
518
# First run unit tests
619
echo "Running unit tests..."
7-
npm run test:unit
20+
env $DEBUG_ENV npm run test:unit
821

922
# Capture the unit tests exit code
1023
UNIT_EXIT_CODE=$?
1124

1225
if [ $UNIT_EXIT_CODE -ne 0 ]; then
1326
echo "Unit tests failed with exit code $UNIT_EXIT_CODE"
27+
if [ "$1" = "debug" ]; then
28+
echo "Debug information: Unit tests exited with code $UNIT_EXIT_CODE"
29+
fi
1430
exit $UNIT_EXIT_CODE
1531
fi
1632

17-
# Then run integration tests with Docker
33+
# Then run integration tests with Docker, with retry logic
1834
echo "Running integration tests with Docker..."
19-
./scripts/run-integration-tests.sh
2035

21-
# Capture the integration tests exit code
22-
INTEGRATION_EXIT_CODE=$?
36+
run_integration_tests() {
37+
./scripts/run-integration-tests.sh $DEBUG_PARAM
38+
return $?
39+
}
40+
41+
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
42+
run_integration_tests
43+
INTEGRATION_EXIT_CODE=$?
44+
45+
if [ $INTEGRATION_EXIT_CODE -eq 0 ]; then
46+
# Tests passed, exit the loop
47+
break
48+
else
49+
RETRY_COUNT=$((RETRY_COUNT + 1))
50+
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
51+
echo "Integration tests failed with exit code $INTEGRATION_EXIT_CODE. Retrying ($RETRY_COUNT/$MAX_RETRIES)..."
52+
# Wait a bit before retrying
53+
sleep 5
54+
else
55+
echo "Integration tests failed after $MAX_RETRIES attempts."
56+
if [ "$1" = "debug" ]; then
57+
echo "Debug information: Integration tests exited with code $INTEGRATION_EXIT_CODE"
58+
fi
59+
fi
60+
fi
61+
done
2362

2463
# Exit with the integration tests exit code
2564
exit $INTEGRATION_EXIT_CODE

scripts/run-integration-tests.sh

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
# Script to run integration tests locally
44

5+
# Check if debug mode is enabled
6+
DEBUG_ENV=""
7+
if [ "$1" = "debug" ]; then
8+
DEBUG_ENV="NODE_DEBUG=mysql,net,stream"
9+
echo "Debug mode enabled. Debug logs will be displayed."
10+
fi
11+
512
# Check if Docker is installed
613
if ! command -v docker &> /dev/null; then
714
echo "Docker is not installed. Please install Docker to run the integration tests."
@@ -38,17 +45,46 @@ for i in {1..30}; do
3845
fi
3946
done
4047

48+
# Add a more robust check to ensure MySQL is fully ready for connections
49+
echo "Verifying MySQL connection stability..."
50+
connection_success=false
51+
for i in {1..5}; do
52+
echo "Connection test $i/5..."
53+
if ! $DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SELECT 1;" &> /dev/null; then
54+
echo "MySQL connection test failed. Waiting a bit longer..."
55+
sleep 2
56+
else
57+
echo "Connection test successful."
58+
connection_success=true
59+
break
60+
fi
61+
done
62+
63+
if [ "$connection_success" = false ]; then
64+
echo "Warning: All connection tests failed. Proceeding anyway, but tests might fail."
65+
fi
66+
67+
# Final stabilization delay
68+
echo "Waiting for MySQL to stabilize..."
69+
sleep 5
70+
4171
# Show MySQL configuration for debugging
42-
echo "MySQL configuration:"
43-
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%timeout%';"
44-
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%max_connections%';"
45-
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%max_allowed_packet%';"
72+
if [ "$1" = "debug" ]; then
73+
echo "MySQL configuration:"
74+
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%timeout%';"
75+
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%max_connections%';"
76+
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "SHOW VARIABLES LIKE '%max_allowed_packet%';"
77+
fi
78+
79+
# Prepare the database for tests
80+
echo "Preparing test database..."
81+
$DOCKER_COMPOSE exec -T mysql mysql -uroot -ppassword -e "DROP DATABASE IF EXISTS serverless_mysql_test; CREATE DATABASE serverless_mysql_test;"
4682

4783
# Run the integration tests
4884
echo "Running integration tests..."
4985
(
5086
(
51-
sleep 60
87+
sleep 90 # Increased timeout for tests
5288
echo "Tests are taking too long, killing process..."
5389
pkill -P $$ || true
5490
for pid in $(ps -o pid= --ppid $$); do
@@ -58,13 +94,28 @@ echo "Running integration tests..."
5894
) &
5995
WATCHDOG_PID=$!
6096

61-
echo "Starting tests with process ID: $$"
62-
MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 MYSQL_DATABASE=serverless_mysql_test MYSQL_USER=root MYSQL_PASSWORD=password NODE_DEBUG=mysql,net,stream npm run test:integration
97+
if [ "$1" = "debug" ]; then
98+
echo "Starting tests with process ID: $$"
99+
fi
100+
101+
# Add connection retry parameters to MySQL connection
102+
MYSQL_HOST=127.0.0.1 \
103+
MYSQL_PORT=3306 \
104+
MYSQL_DATABASE=serverless_mysql_test \
105+
MYSQL_USER=root \
106+
MYSQL_PASSWORD=password \
107+
MYSQL_CONNECT_TIMEOUT=10000 \
108+
MYSQL_RETRY_COUNT=3 \
109+
env $DEBUG_ENV npm run test:integration
110+
63111
TEST_EXIT_CODE=$?
64112

65113
echo "Tests completed with exit code: $TEST_EXIT_CODE"
66114

67-
echo "Killing watchdog process: $WATCHDOG_PID"
115+
if [ "$1" = "debug" ]; then
116+
echo "Killing watchdog process: $WATCHDOG_PID"
117+
fi
118+
68119
kill $WATCHDOG_PID 2>/dev/null || true
69120

70121
exit $TEST_EXIT_CODE
@@ -75,7 +126,9 @@ echo "Cleaning up..."
75126
$DOCKER_COMPOSE down
76127

77128
# Make sure no node processes are left hanging
78-
echo "Checking for hanging Node.js processes..."
79-
ps aux | grep node | grep -v grep || true
129+
if [ "$1" = "debug" ]; then
130+
echo "Checking for hanging Node.js processes..."
131+
ps aux | grep node | grep -v grep || true
132+
fi
80133

81134
exit 0

0 commit comments

Comments
 (0)