source `dirname $0`/util.sh

SYNC_TOKEN=sleeping

record $TESTNAME &             # sleep "forever"

echo "Waiting for token '$SYNC_TOKEN' from tracee ..."
until grep -q $SYNC_TOKEN record.out; do
    sleep 0
done

rrpid=$(parent_pid_of $(pidof $TESTNAME-$nonce))

echo "  done.  Delivering SIGTERM to $rrpid ..."
kill -TERM $rrpid

echo "  done."

# Wait for 'record' to actually terminate. Otherwise we might start
# replaying before the trace file has been completely written, and we might
# fail to see the tracee write EXIT-SUCCESS.
wait

if [[ "record.out" == $(grep -l "EXIT-SUCCESS" record.out) ]]; then
    echo "Test '$TESTNAME' FAILED: error during recording: tracer not interrupted in time."
fi

echo "Replaying ..."
replay
check ""
