#!/bin/sh
#
# Copyright 2004 MySQL AB
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

echo "######################################################################"
echo "#"
echo "# Test BUG#2408 - Multiple threads altering MERGE table UNIONs hang/crash"
echo "#"

#######################################################################
#
# Settings. CAUTION: Paths are not space safe.
#

# DBROOT="install directory"
RUNDIR="$DBROOT/var"

MYSQLD="$DBROOT/libexec/mysqld"
MYSQLC="$DBROOT/bin/mysql"
MYSQLA="$DBROOT/bin/mysqladmin"
MYSQLT="$DBROOT/bin/mysqltest"

PORT_1="${MYSQL_TCP_PORT:+--port=$MYSQL_TCP_PORT}"
SOCK_1="${MYSQL_UNIX_PORT:+--socket=$MYSQL_UNIX_PORT}"
USER_1="-u root -D test"
CLNT_1="-v -f"
DATA_1="--datadir=$RUNDIR"
SERV_1="--log-error --core"
DBUG_1= #"--debug=t:d:i:O,$RUNDIR/mysqld.trace"

cd $RUNDIR || exit $?
#rm -f *.trace *.err core* test/#sql*

echo "######################################################################"
echo "#"
echo "# Starting database server."
"$MYSQLD" $PORT_1 $SOCK_1 $DATA_1 $SERV_1 $DBUG_1 &
SERV_1_PID=$!
echo "Process_id $SERV_1_PID"
echo
sleep 3


echo "######################################################################"
echo "#"
echo "# Preparing database."
"$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1 <<EOF
    DROP DATABASE IF EXISTS test_mrg;
    CREATE DATABASE test_mrg;
EOF
echo


echo "######################################################################"
echo "#"
echo "# Preparing perl script."
cat > bug2408.pl <<'EOF'
#!/usr/bin/perl -w

my $basetable = 't1';

print <<_EOT_;
CREATE DATABASE IF NOT EXISTS test_mrg;
use test_mrg;
CREATE TABLE IF NOT EXISTS ${basetable}(id INT NOT NULL PRIMARY KEY) TYPE=MERGE;
_EOT_

my @union;

for (my $n=1; $n; $n++) {
        my $table = "${basetable}_$n";

        push @union, $table;

        my $drop = @union > 4 ? shift @union : undef;

        my $union = join(',', @union);

        print <<_EOT_;
CREATE TABLE IF NOT EXISTS $table(id INT NOT NULL PRIMARY KEY);
FLUSH TABLES;
ALTER TABLE $basetable UNION=($union);
FLUSH TABLES;
INSERT INTO $table(id) VALUES($n);
SELECT * FROM $basetable;
_EOT_

        print <<_EOT_ if $drop;
DROP TABLE $drop;
_EOT_
}
EOF
echo


echo "######################################################################"
echo "#"
echo "# Starting client 1."
(
    perl bug2408.pl | "$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1
)&
CLNT_1_PID=$!
echo "Process_id $CLNT_1_PID"
echo
sleep 5


echo "######################################################################"
echo "#"
echo "# Starting client 2."
(
    perl bug2408.pl | "$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1
)&
CLNT_2_PID=$!
echo "Process_id $CLNT_2_PID"
echo
sleep 5


echo "######################################################################"
echo "#"
echo "# Starting client 3."
(
    perl bug2408.pl | "$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1
)&
CLNT_3_PID=$!
echo "Process_id $CLNT_3_PID"
echo
sleep 5


echo "######################################################################"
echo "#"
echo "# Starting client 4."
(
    perl bug2408.pl | "$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1
)&
CLNT_4_PID=$!
echo "Process_id $CLNT_4_PID"
echo
sleep 5


echo "Waiting for completion..."
wait $CLNT_1_PID
wait $CLNT_2_PID
wait $CLNT_3_PID
wait $CLNT_4_PID

echo "######################################################################"
echo "#"
echo "# Stopping database server."
"$MYSQLA" $PORT_1 $SOCK_1 -u root shutdown
sleep 3


echo "# End of Test."
echo "#"
echo "######################################################################"

