#!/bin/bash
set +x

echo "jdbc fast test start"

FAST_TIDB_LOG="fast_tidb.log"

# start tidb servers
echo "starting tidb-servers, log file: ${FAST_TIDB_LOG}"
${TIDB_SERVER_PATH} -config fast_config.toml -store unistore -path "" -lease 0s > ${FAST_TIDB_LOG} 2>&1 &
FAST_SERVER_PID=$!
sleep 5
echo "tidb-server(PID: ${FAST_SERVER_PID}) started"

echo "loading init file"
unset GOPATH
unset GOROOT
GOPATH=`realpath ../../../../..` SERVER="root@tcp(127.0.0.1:4001)/" go run init.go

# start test
mvn '-Dtest=!Statement*, !ResultSet*' '-DTIDBPORT=4001' test
EXIT_CODE=$?

# handle test results
if [ ${EXIT_CODE} -ne 0 ]
then
	echo "ERROR: jdbc test failed!"
	echo "=========== ERROR EXIT [${EXIT_CODE}]: FULL tidb.log BEGIN ============"
	cat ${FAST_TIDB_LOG}
	echo "=========== ERROR EXIT [${EXIT_CODE}]: FULL tidb.log END =============="
	exit 1
fi

# handle race
RACE_RET=`grep 'DATA RACE' ${FAST_TIDB_LOG} || true`
if [ -n "${RACE_RET}" ]
then
	echo "tidb-server DATA RACE!"
	cat ${FAST_TIDB_LOG}
	exit 1
fi
