#!/bin/bash
##
## @brief @(#) See README. Test second cache db, test service dissapearance after shutdown
##   have cached results when service is back.
##
## @file 02_run.sh
##
## -----------------------------------------------------------------------------
## Enduro/X Middleware Platform for Distributed Transaction Processing
## Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
## Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
## This software is released under one of the following licenses:
## AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
## See LICENSE file for full text.
## -----------------------------------------------------------------------------
## AGPL license:
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU Affero General Public License, version 3 as published
## by the Free Software Foundation;
##
## 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 Affero General Public License, version 3
## for more details.
##
## You should have received a copy of the GNU Affero 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
##
## -----------------------------------------------------------------------------
## A commercial use license is available from Mavimax, Ltd
## contact@mavimax.com
## -----------------------------------------------------------------------------
##

export TESTNAME="test048_cache"

PWD=`pwd`
if [ `echo $PWD | grep $TESTNAME ` ]; then
    # Do nothing 
    echo > /dev/null
else
    # started from parent folder
    pushd .
    echo "Doing cd"
    cd $TESTNAME
fi;

export NDRX_CCONFIG=`pwd`
. ../testenv.sh

export TESTDIR="$NDRX_APPHOME/atmitest/$TESTNAME"
export PATH=$PATH:$TESTDIR
export NDRX_TOUT=10
export NDRX_ULOG=$TESTDIR
export TESTDIR_DB=$TESTDIR
export TESTDIR_SHM=$TESTDIR

source ./test-func-include.sh


#
# Domain 1 - here client will live
#
set_dom1() {
    echo "Setting domain 1"
    . ../dom1.sh
    export NDRX_CONFIG=$TESTDIR/ndrxconfig-dom1.xml
    export NDRX_DMNLOG=$TESTDIR/ndrxd-dom1.log
    export NDRX_LOG=$TESTDIR/ndrx-dom1.log
    export NDRX_CCTAG=dom1
}

#
# Generic exit function
#
function go_out {
    echo "Test exiting with: $1"
    
    set_dom1;
    xadmin stop -y
    xadmin down -y



    # If some alive stuff left...
    xadmin killall atmiclt48

    popd 2>/dev/null
    exit $1
}

rm *dom*.log
# Any bridges that are live must be killed!
xadmin killall tpbridge

set_dom1;
xadmin down -y
xadmin start -y || go_out 1



RET=0

xadmin psc
xadmin ppm
echo "Running off client"

set_dom1;

(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY1","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123,"T_CHAR_FLD":"A"}' \
    -m '{"T_STRING_FLD":"KEY1","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123}' \
    -cY -n100 -fY 2>&1) > ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "testtool48 failed (1)"
    go_out 1
fi

(time ./testtool48 -sTESTSV02_2 -b '{"T_STRING_FLD":"KEY4","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123,"T_CHAR_FLD":"A"}' \
    -m '{"T_STRING_FLD":"KEY4","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123}' \
    -cY -n100 -fY 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "testtool48 failed (1.2)"
    go_out 101
fi


echo "Show cache... "

#
# Must be empty
#
xadmin cs -d db02_1

if [ $? -ne 0 ]; then
    echo "xadmin cs failed"
    go_out 2
fi

#
# Must have some 1 record
#
xadmin cs -d db02_2

if [ $? -ne 0 ]; then
    echo "xadmin cs failed"
    go_out 2
fi

ensure_keys db02_2 1

(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY2","T_FLOAT_FLD":"1.2","T_SHORT_FLD":44,"T_CHAR_FLD":"B"}' \
    -m '{"T_STRING_FLD":"KEY2","T_FLOAT_FLD":"1.2","T_SHORT_FLD":44}' \
    -cY -n100 -fY 2>&1) >> ./02_testtool48.log


if [ $? -ne 0 ]; then
    echo "testtool48 failed (2)"
    go_out 3
fi

#
# Test the message dump for saved fields
#
ensure_field db02_2 SV2KEY2 T_STRING_FLD KEY2 1
ensure_field db02_2 SV2KEY2 T_FLOAT_FLD 1.2 1
ensure_field db02_2 SV2KEY2 T_SHORT_FLD 44 1
ensure_field db02_2 SV2KEY2 T_CHAR_FLD B 0

#
# Must have some 2 records
#
xadmin cs -d db02_2

if [ $? -ne 0 ]; then
    echo "xadmin cs failed"
    go_out 2
fi

# DB1 is empty
ensure_keys db02_1 1
ensure_keys db02_2 2

xadmin stop -s atmi.sv48

#
# Now service call shall fail
#

(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY2","T_FLOAT_FLD":"1.3","T_CHAR_FLD":"C"}' \
    -cY -n1 -fY -e6 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "Failed to validate tpcall error 6"
    go_out 5
fi

#
# This still responds with cached data
#
(time ./testtool48 -sTESTSV02_2 -b '{"T_STRING_FLD":"KEY4","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123,"T_CHAR_FLD":"A"}' \
    -m '{"T_STRING_FLD":"KEY4","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123}' \
    -cY -n100 -fN 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "testtool48 failed (5.1)"
    go_out 101

fi

#
# We shall get error 6 for nosvcok, but no data available...
#
(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY5","T_FLOAT_FLD":"1.3","T_CHAR_FLD":"C"}' \
    -cY -n1 -fY -e6 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "Failed to validate tpcall error 6 (5.2)"
    go_out 102
fi

xadmin start -s atmi.sv48


#
# now we shall get cached results...
#

(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY1","T_FLOAT_FLD":"1.4","T_CHAR_FLD":"D"}' \
    -m '{"T_STRING_FLD":"KEY1","T_FLOAT_FLD":"1.1","T_SHORT_FLD":123}' \
    -cY -n100 -fN 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "testtool48 failed (3)"
    go_out 6
fi


(time ./testtool48 -sTESTSV02 -b '{"T_STRING_FLD":"KEY2","T_FLOAT_FLD":"1.5","T_CHAR_FLD":"E"}' \
    -m '{"T_STRING_FLD":"KEY2","T_FLOAT_FLD":"1.2","T_SHORT_FLD":44}' \
    -cY -n100 -fN 2>&1) >> ./02_testtool48.log

if [ $? -ne 0 ]; then
    echo "testtool48 failed (4)"
    go_out 7
fi


go_out $RET

# vim: set ts=4 sw=4 et smartindent:
