#!/bin/bash
set +x

echo "mybatis test start"

MYBATIS_TIDB_LOG="tidb_mybatis.log"

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

# when exit clean

trap 'kill ${SERVER_PID}' EXIT

mysql -u root -h 127.0.0.1 -P 4001 -e "SET GLOBAL tidb_enable_noop_functions = 1"

# start test
mvn test
EXIT_CODE=$?

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