#!/bin/bash
###############################################################################
##
## Copyright (c) INRIA, 2007
##
## 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
##
###############################################################################
#
# Description:  Test program for Kerrighed checkpoint/restart a JVM
#
# Author:       Matthieu Fertré, matthieu.fertre@irisa.fr
#

source `dirname $0`/lib_cr.sh

description="Run, C, K, R, C, K, R, R (EBUSY), K, R - THREAD"

# multi-threaded application
TESTCMD="java"
TESTCMD_OPTIONS="Bi -q"

export CLASSPATH=`dirname $0`

cr_thread_java01()
{
    TCID="$FUNCNAME"               # Identifier of this testcase.
    TST_COUNT=$[$TST_COUNT+1]      # Test case number.

    # look for Java
    which java > /dev/null
    if [ $? -ne 0 ]; then
	tst_resm TWARN "Can not run this test if Java is not installed"
	return 0 # to not stop krgltp
    fi

    # does Bi.class exist ?
    if  [ ! -e "$CLASSPATH/Bi.class" ]; then
	tst_resm TWARN "Can not run this test if $CLASSPATH/Bi.class is not installed"
	return 0 # to not stop krgltp
    fi

    runcommand +CHECKPOINTABLE 1 nosync || return $?

    # let the process really begin
    sleep 5

    freeze_process $PID $TESTCMD || return $?

    checkpoint_frozen_process $PID $TESTCMD || return $?

    cp /tmp/hsperfdata_$USER/$PID /tmp/java_tmp_${USER}_${PID}

    unfreeze_process $PID $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    restart_process_must_fail $PID 1 $TESTCMD || return $?

    mv /tmp/java_tmp_${USER}_${PID} /tmp/hsperfdata_$USER/$PID

    restart_process $PID 1 $TESTCMD || return $?

    freeze_process $PID $TESTCMD || return $?

    checkpoint_frozen_process $PID $TESTCMD || return $?

    cp /tmp/hsperfdata_$USER/$PID /tmp/java_tmp_${USER}_${PID}_v2

    unfreeze_process $PID $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    cp /tmp/java_tmp_${USER}_${PID}_v2 /tmp/hsperfdata_$USER/$PID

    restart_process $PID 2 $TESTCMD || return $?

    restart_process_must_fail $PID 1 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    cp /tmp/java_tmp_${USER}_${PID}_v2 /tmp/hsperfdata_$USER/$PID

    restart_process $PID 2 $TESTCMD || return $?

    kill_group $PID $TESTCMD || return $?

    rm -rf "/tmp/java_tmp_${USER}_${PID}*"

    print_success $?
    return $?
}

CR_setup $@ || exit $?

cr_thread_java01 || exit $?
