#!/bin/sh
#
# Copyright (c) 2006 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#16218 - Crash on insert delayed"
echo "# Test BUG#17294 - INSERT DELAYED puting an \\n before data"
echo "# Test BUG#16611 - INSERT DELAYED corrupts data"
echo "# Test BUG#13707 - Server crash with INSERT DELAYED on MyISAM table"
echo "#"
echo "# Part 2 - to be run on 5.0. Copy the tables from part 1."
echo "#"

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

# BASEDIR="install directory"
# DATADIR="databases directory e.g. $BASEDIR/var"
DATADIR="${DATADIR:-$BASEDIR/var}"
TREEROOT="${TREEROOT:-$HOME}"

MYSQLD="$BASEDIR/libexec/mysqld"
MYSQLC="$BASEDIR/bin/mysql"
MYSQLA="$BASEDIR/bin/mysqladmin"
MYSQLT="$BASEDIR/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="-A -v -f"
CLNT_1="-A -vvv -f"
DATA_1="--basedir=$BASEDIR --datadir=$DATADIR"
SERV_1="--log-error --core"
#SERV_1="$SERV_1 --skip-concurrent-insert"
DBUG_1=
#DBUG_1="--debug=d,query,enter:i:O,$DATADIR/mysqld.trace"
#DBUG_1="--debug=d,thrlock:i:O,$DATADIR/mysqld.trace"
#DBUG_1="--debug=t:d,query,send_data:i:O,$DATADIR/mysqld.trace"
DBUG_1="--debug=t:d:i:O,$DATADIR/mysqld.trace"
DDD=
#DDD=1

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

#######################################################################
#
# Functions.

sleep_until_file_created ()
{
  file=$1
  loop=$2
  org_time=$2
  while test $loop -gt 0
  do
    if test -r $file
    then
      return 0
    fi
    sleep 1
    loop=`expr $loop - 1`
  done
  echo "ERROR: $file was not created in $org_time seconds;  Aborting"
  exit 1;
}

sleep_until_file_deleted ()
{
  pid=$1;
  file=$2
  loop=$3
  while test $loop -gt 0 && test $pid -eq 0 || kill -0 $pid 2>/dev/null
  do
    if test ! -r $file
    then
      if test $pid -ne 0
      then
        wait_for_pid $pid
      fi
      return
    fi
    sleep 1
    loop=`expr $loop - 1`
  done
}

echo "######################################################################"
echo "#"
echo "# Copy tables from 4.1."
if [ $# -eq 1 ]
then
    PART1_DATADIR="$1"
else
    echo "Please enter the datadir from test part 1 [use existing tables]:"
    read PART1_DATADIR
fi
if [ -n "$PART1_DATADIR" ]
then
    if [ ! -d "$PART1_DATADIR" ]
    then
        echo "'$PART1_DATADIR' is not a directory."
        exit 1
    fi
    if [ ! -f "$PART1_DATADIR/test/t1.frm" -o \
         ! -f "$PART1_DATADIR/test/t1.MYI" -o \
         ! -f "$PART1_DATADIR/test/t1.MYD" -o \
         ! -f "$PART1_DATADIR/test/t2.frm" -o \
         ! -f "$PART1_DATADIR/test/t2.MYI" -o \
         ! -f "$PART1_DATADIR/test/t2.MYD" -o \
         ! -f "$PART1_DATADIR/test/t3.frm" -o \
         ! -f "$PART1_DATADIR/test/t3.MYI" -o \
         ! -f "$PART1_DATADIR/test/t3.MYD" -o \
         ! -f "$PART1_DATADIR/test/t4.frm" -o \
         ! -f "$PART1_DATADIR/test/t4.MYI" -o \
         ! -f "$PART1_DATADIR/test/t4.MYD" ]
    then
        echo "One or more of the table files .frm, .MYI, .MYD is missing in"
        echo "'$PART1_DATADIR'."
        exit 1
    fi
    cp -p "$PART1_DATADIR/test/t1.frm" "$DATADIR/test/t1.frm"
    cp -p "$PART1_DATADIR/test/t1.MYI" "$DATADIR/test/t1.MYI"
    cp -p "$PART1_DATADIR/test/t1.MYD" "$DATADIR/test/t1.MYD"
    cp -p "$PART1_DATADIR/test/t2.frm" "$DATADIR/test/t2.frm"
    cp -p "$PART1_DATADIR/test/t2.MYI" "$DATADIR/test/t2.MYI"
    cp -p "$PART1_DATADIR/test/t2.MYD" "$DATADIR/test/t2.MYD"
    cp -p "$PART1_DATADIR/test/t3.frm" "$DATADIR/test/t3.frm"
    cp -p "$PART1_DATADIR/test/t3.MYI" "$DATADIR/test/t3.MYI"
    cp -p "$PART1_DATADIR/test/t3.MYD" "$DATADIR/test/t3.MYD"
    cp -p "$PART1_DATADIR/test/t4.frm" "$DATADIR/test/t4.frm"
    cp -p "$PART1_DATADIR/test/t4.MYI" "$DATADIR/test/t4.MYI"
    cp -p "$PART1_DATADIR/test/t4.MYD" "$DATADIR/test/t4.MYD"
fi


echo "######################################################################"
echo "#"
echo "# Starting database server."
if [ -z "$DDD" ]
then
    "$MYSQLD" $PORT_1 $SOCK_1 $DATA_1 $SERV_1 $DBUG_1 &
else
    echo "set args $PORT_1 $SOCK_1 $DATA_1 $SERV_1 $DBUG_1" > bug16218-1.gdb
    ddd --debugger "gdb -x bug16218-1.gdb" \
        -dir="$SRCDIR/sql" -dir="$SRCDIR/myisam" -dir="$SRCDIR/mysys" \
        "$MYSQLD" &
fi
SERV_1_PID=$!
echo "Process_id $SERV_1_PID"
echo
sleep_until_file_created `uname -n`.pid 400


echo "######################################################################"
echo "#"
echo "# Inserting delayed."
"$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1 <<\EOF

    INSERT DELAYED INTO t1
        (ipa, gsku, name, pass, serial, sid, msg, time)
        VALUES (0,0,"","",NULL,0,"",0);
    system sleep 1;
    SELECT * from t1;

    INSERT DELAYED INTO t2 SET `from`='5555555555';
    system sleep 1;
    SELECT HEX(`from`), `from` from t2;

    INSERT DELAYED INTO t3 VALUES ('toto');
    system sleep 1;
    SELECT HEX(`c1`), `c1` from t3;

    INSERT DELAYED INTO t4  
        (RateDate, Rate, DataSource, UserSign, Currency, endDate)
        VALUES ('2005-01-01',0.58683,'I',1,'USD','9999-12-31');
    system sleep 1;
    SELECT * from t4;

EOF
echo
sleep 1


echo "######################################################################"
echo "#"
echo "# Stopping database server."
"$MYSQLA" $PORT_1 $SOCK_1 -u root shutdown
sleep_until_file_deleted "$SERV_1_PID" `uname -n`.pid 60


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

