#!/bin/bash
#
# Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../../

TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"

SESSION_NAME="my_session"
SESSION_OUTPUT_PATH=$(mktemp --tmpdir -d tmp.test_notif_rotation_trace_path.XXXXXX)
EVENT_NAME="tp:tptest"

PAGE_SIZE=$(getconf PAGE_SIZE)
SUBBUF_SIZE=$(expr $PAGE_SIZE \* 8)

FILE_SYNC_AFTER_FIRST_EVENT=$(mktemp --tmpdir -u tmp.test_rotation_sync_after_first.XXXXXX)

NR_ITER=-1
NR_USEC_WAIT=5

DIR=$(readlink -f $TESTDIR)

source $TESTDIR/utils/utils.sh
start_lttng_sessiond_notap

create_lttng_session_notap $SESSION_NAME $SESSION_OUTPUT_PATH

enable_ust_lttng_channel_notap $SESSION_NAME $CHANNEL_NAME --subbuf-size=$SUBBUF_SIZE
enable_ust_lttng_event_notap $SESSION_NAME $EVENT_NAME $CHANNEL_NAME

start_lttng_tracing_notap $SESSION_NAME

$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT --sync-after-first-event $FILE_SYNC_AFTER_FIRST_EVENT &
APP_PID=$!
while [ ! -f "${FILE_SYNC_AFTER_FIRST_EVENT}" ]; do
	sleep 0.5
done

# The rotation application handles the actual testing once the tracing session
# has been setup.
$CURDIR/rotation $SESSION_NAME $SESSION_OUTPUT_PATH
if [ $? -ne 0 ]; then
	diag "Failed to run rotation notification client"
fi

stop_lttng_tracing_notap $SESSION_NAME

stop_lttng_sessiond_notap

# On ungraceful kill the app is cleaned up via the full_cleanup call
# Suppress kill message
kill -9 $APP_PID
wait $APP_PID 2> /dev/null

rm -rf $SESSION_OUTPUT_PATH
rm -f $FILE_SYNC_AFTER_FIRST_EVENT
