Skip to content

Commit 9992d17

Browse files
committed
Add readiness wait for TiDB cluster before tests
Add wait_for_tidb function that waits for TiDB to be ready before tests start. Uses extended timeout (240s) for 6.1.x and 6.5.x versions which need longer startup time, matching the behavior in tidb-playground.sh. This fixes test timeouts where tests were trying to connect before TiDB was ready to accept connections.
1 parent affb8c9 commit 9992d17

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

scripts/tidb-test-cluster.sh

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,39 @@ function run_tidb() {
170170
fi
171171
}
172172

173+
function wait_for_tidb() {
174+
local _mysql_port=$1
175+
local _version=$2
176+
echo "==> Waiting for TiDB to be ready..."
177+
178+
# Determine timeout based on TiDB version
179+
# Versions 6.1.x and 6.5.x need longer startup time
180+
if [[ "${_version}" == 6.1.* ]] || [[ "${_version}" == 6.5.* ]]; then
181+
TIMEOUT=240 # 4 minutes for older versions
182+
echo "Using extended timeout (240s) for TiDB ${_version}"
183+
else
184+
TIMEOUT=120 # 2 minutes for newer versions
185+
fi
186+
187+
# Wait for TiDB to be ready
188+
for i in $(seq 1 ${TIMEOUT}); do
189+
if mysql -h 127.0.0.1 -P ${_mysql_port} -u root -e 'SELECT 1' >/dev/null 2>&1; then
190+
echo "TiDB is ready!"
191+
return 0
192+
fi
193+
sleep 1
194+
if [ $((i % 10)) -eq 0 ]; then
195+
printf "."
196+
fi
197+
done
198+
199+
echo ""
200+
echo "ERROR: TiDB failed to start within ${TIMEOUT} seconds"
201+
echo "Checking container logs..."
202+
docker logs tidb 2>&1 | tail -20 || true
203+
return 1
204+
}
205+
173206
function main() {
174207
parse_params "$@"
175208
if [[ "$SCRIPT_INIT" = "true" ]]; then
@@ -178,7 +211,8 @@ function main() {
178211
${DOCKER} network create ${DOCKER_NETWORK} && \
179212
run_pd && \
180213
run_tikv && \
181-
run_tidb $MYSQL_PORT
214+
run_tidb $MYSQL_PORT && \
215+
wait_for_tidb $MYSQL_PORT $MYSQL_VERSION
182216

183217
else
184218
script_usage

0 commit comments

Comments
 (0)