#!/bin/bash
set +x

echo "jdbc 8 tls test start"

TLS_TIDB_LOG="tls_tidb_jdbc8.log"

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

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

# start test
mvn '-Dtest=TLS*' '-DTIDBPORT=4003' test
EXIT_CODE=$?

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

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