#!/bin/sh
# $Id: t-restart-dep-0.sh,v 1.1 2007/09/29 02:15:44 ca Exp $
#
# ----------------------------------------
# test restart functionality of mcp: restart "server" process too
# ----------------------------------------

test -s stop && exit 1

# set for verbose mode to -V...
V=""
ERRS=0
OUT=mcp.out
PIDS=pids
CONF=restart.conf
PIDF1="t-restart1.pid"
PIDF2="t-restart2.pid"
L1=restart.log
L2=restart2.log
R1="restarts1"
R2="restarts2"
VERBOSE=false
SD=`dirname $0`
. ${SD}/../check2/user.sh
if test $# -ge 1
then
  if test "X"$1 = "X-v"
  then
    VERBOSE=true;
  fi
fi

rm -f ${PIDS} ${OUT} ${PIDF1} ${PIDF2} ${L1} ${L2}

cat > ${CONF} <<EOF
restart {
	start_action = wait;
	min_processes = 1;
	max_processes = 1;
	user = ${USER};
	restart_dependencies = {restart2};
	path = ./t-restart;
	arguments = "t-restart -p ${PIDF1} -f ${R1} -n 3 -VV";
}
restart2 {
	start_action = wait;
	min_processes = 1;
	max_processes = 1;
	user = ${USER};
	path = ./t-restart;
	arguments = "t-restart -p ${PIDF2} -f ${R2} -n 2 -VV -R -s 2";
}
EOF

echo 0 > ${R1}
echo 0 > ${R2}
./mcp -D -t -dddd ${CONF} > ${OUT} 2>&1 &
echo $! >> ${PIDS}

i=0
while test ! -s ${PIDF1}
do
  sleep 3
  i=`expr ${i} + 1 `
  if test ${i} -ge 4
  then
    echo "$0: ERROR: missing ${PIDF1}"
    ERRS=`expr ${ERRS} + 1 `
    break
  fi
done

i=0
while test ! -s ${PIDF2}
do
  sleep 3
  i=`expr ${i} + 1 `
  if test ${i} -ge 4
  then
    echo "$0: ERROR: missing ${PIDF2}"
    ERRS=`expr ${ERRS} + 1 `
    break
  fi
done

i=0
while test ${i} -le 2
do
  if grep 'asked' ${OUT} >/dev/null
  then
     break
  fi
  sleep 3
  i=`expr ${i} + 1 `
done

# give it time to restart
sleep 3

CNT=2
for F in ${R1} ${R2}
do
if test -s ${F}
then
  C=`cat ${F}`
  if test ${C} -ne ${CNT}
  then
    ${VERBOSE} && echo "counter ${F} wrong: got ${C}, expected ${CNT}"
    ERRS=`expr ${ERRS} + 1 `
  fi
else
  ${VERBOSE} && echo "missing ${F}"
  ERRS=`expr ${ERRS} + 1 `
fi
done

for i in `cat ${PIDS}`
do
  kill ${i}
done

exit ${ERRS}


syntax highlighted by Code2HTML, v. 0.9.1