#!/bin/sh
#
# Concurrency test
#
# Since it tests asynchronous behavior, the output of this test is more 
# difficult to diff against a standard.  Areas of discrepency 
# will be noted in the output.
#
# Usage:     concur [options]
# Options:      -index  create and use indexes
#               -hier   target table will be in a hierarchy

TABLE=auctionitems
NOINDEX="-noindex"
ORIGINALFILE=originals/auctionitems

for i in $*
do
  if [ $i = "-index" ]; then
    NOINDEX=""
  elif [ $i = "-hier" ]; then
    TABLE=hiertest/auctionitems
  else
    echo "$i - unrecognized option - quitting."
    exit
  fi
done

SHSQL="shsql -echo -n $NOINDEX"
SHSQL2="shsql -echo $NOINDEX"

echo "Starting test.. concur $*" >&2
echo "Starting test.. concur $*" 

echo "*************************************************************"
echo "0. drop old tables that may be lying around.. and set up a fresh sequences table"
$SHSQL2 "drop table $TABLE"  2>/dev/null
$SHSQL2 "drop table _sequences"    2>/dev/null
$SHSQL2 "select * into _sequences table from originals/_sequences" 2>&1

echo "*************************************************************"
echo "1. create sequence on auctionitems and set sequence number to an initial value of 39000"
$SHSQL "create sequence on auctionitems" 2>&1
$SHSQL2 "update _sequences set value = 39000 where tablename = '$TABLE' " 2>&1

echo "*************************************************************"
echo "2. populate the auctionitems table by selecting into from an original copy.."
$SHSQL2 "select * into $TABLE table from originals/auctionitems" 2>&1

echo "*************************************************************"
echo "3. use alter to add _lockowner and _locktime fields.."
$SHSQL2 "alter table auctionitems add _lockowner (nomaintain)" 2>&1
$SHSQL2 "alter table auctionitems add _locktime (nomaintain)" 2>&1

if [ "$NOINDEX" = "" ]; then
  echo "*************************************************************"
  echo "4. create index.."
  shsql "create index on auctionitems ( id )" 2>&1
fi

echo "*************************************************************"
echo "5. record count"
$SHSQL "select count(*) from $TABLE" 2>&1

echo "**************************************************************"
echo "6. now start the concurrency test.."

./concur_sub $TABLE fred > concurtmp/fred 2> concurtmp/fred.err &
./concur_sub $TABLE jon > concurtmp/jon 2> concurtmp/jon.err &
./concur_sub $TABLE ron > concurtmp/ron 2> concurtmp/ron.err &
./concur_sub $TABLE kim > concurtmp/kim 2> concurtmp/kim.err &
./concur_sub $TABLE ken > concurtmp/ken 2> concurtmp/ken.err &
./concur_sub $TABLE liz > concurtmp/liz 2> concurtmp/liz.err &
./concur_sub $TABLE tim > concurtmp/tim 2> concurtmp/tim.err &

echo "**************************************************************"
echo "7. sleep 5 to give plenty of time for the above to finish.. "
sleep 5

echo "**************************************************************"
echo "8. concurrency test done.. 
Now list the result rows.. ids and lockowners will vary across tests
but status column should read 'MODIFIED' and all records should be well-formed.."
$SHSQL "select * from auctionitems where id > 39000"

echo "**************************************************************"
echo "9. list the record that everyone tried to lock.. lockowner will 
vary across tests but record should be well-formed.."
$SHSQL "select * from auctionitems where id = 37012"

echo "Finished." >&2
