#!/usr/bin/env bash
trap 'killm f' EXIT
# Bug 0015 tcpclo and bpcp signal handling and semaphore signal interruptions
# Samuel Jero
# March 5, 2013
# Update May 25, 2022
# Jay Gao
# The issue-358-cfdp-inactivity and issue-352-bpcp tests are sufficient
# to validate that the tcpclo and bpcp/bpcpd signal handlers work correctly.
# This test ensures that ION correctly handles semaphore operations that
# get interrupted by a signal. We setup a 2-node TCP transfer and
# run a simple program that constantly attempts to perform a transaction.
# We then fire a bunch of signals at this simple program. We then check
# ion.log for failed assertions which would indicate a hijacked transaction.


echo "########################################"
echo
pwd | sed "s/\/.*\///" | xargs echo "NAME: "
echo
echo "PURPOSE: To ensure that ION correctly handles semaphore operations that"
echo "         get interrupted by a signal. We setup a 2-node tcp transfer"
echo "         and run a simple program that constantly attempts to perform a"
echo "         transaction. We then fire a bunch of signals at this simple"
echo "         program. We then check ion.log for failed assertions which would"
echo "         indicate a hijacked transaction."
echo ""
echo "OUTPUT: Terminal messages will relay results."
echo
echo "########################################"

#Check for test program
if ! [ -e ./test ]; then
    echo ""
    echo "Test program not available! Make sure to run make test to build it."
    echo "Skipping this test"
    echo ""
    exit 2
fi

# Starting ION
./cleanup
sleep 1
echo "Starting ION..."
export ION_NODE_LIST_DIR=$PWD
RETVAL=0


env >> ~/postscript
# Start nodes.
# Setup node 1
echo "Starting Node 1..."
cd 1.ipn.tcp/
./ionstart
../../../system_up -i "p 30" -b "p 30"

if [ "$?" -eq 2 ]
then
    echo ""
else
    echo "Node 1 not started: Aborting Test"
    exit 1
fi

#Set up node 2
echo "Starting Node 2..."
cd ../2.ipn.tcp/
./ionstart
../../../system_up -i "p 30" -b "p 30"

if [ "$?" -eq 2 ]
then
    echo ""
else
    echo "Node 2 not started: Aborting Test"
    exit 1
fi

#Starting receiving data
echo "Starting bpcounter on node 2"
cd ../2.ipn.tcp/
bpcounter ipn:2.2 &
BPRCV=$!
sleep 5

#Starting sending data
echo "Starting bpdriver on node 1"
cd ../1.ipn.tcp/
bpdriver 100000 ipn:1.2 ipn:2.2 -1000 &
BPSND=$!

#Starting test program
echo "Starting test program..., attach to the sending node."
cd ../1.ipn.tcp/
export ION_NODE_WDNAME=$PWD
cd ../
./test &
TST=$!

#Fire signals
echo "Fire signals at test program..."
for i in {1..20}
do
    kill -SIGINT $TST 2>/dev/null
    kill -SIGINT $TST 2>/dev/null
    kill -SIGINT $TST 2>/dev/null
    kill -SIGINT $TST 2>/dev/null
    sleep 1
done

#Test
test1=$(grep "Assertion failed" ./2.ipn.tcp/ion.log 2>/dev/null)
echo $test1
if [ -z "$test1" ]; then
    echo "OK: Checked node 2, ION worked correctly when bombarded by signals. SUCCESS!!"
else
    echo "ERROR: ION crashed as a result of a signal. FAILED!!"
    RETVAL=1
fi

test2=$(grep "Assertion failed" ./1.ipn.tcp/ion.log 2>/dev/null)
echo $test2
if [ -z "$test2" ]; then
    echo "OK: Checked node 1, ION worked correctly when bombarded by signals. SUCCESS!!"
else
    echo "ERROR: ION crashed as a result of a signal. FAILED!!"
    RETVAL=1
fi

sleep 10

#End ION
echo "Killing ION..."
kill -SIGTERM $TST 2>/dev/null
sleep 1
exit $RETVAL
