#!/bin/sh
# This regression test is a part of SIPp.
# With -ringbuffer_files, turning a log off and on again with the
# "trace" control command rotates it with no file open: SIPp carries
# on, rather than fclose() the null stream.
. "`dirname "$0"`/../functions"; init

command -v python3 >/dev/null || skip "no python3"

sippfg -sf uac.xml -i 127.0.0.1 -p 25871 -cp 8897 -m 1 127.0.0.1:25870 \
    -trace_logs -ringbuffer_files 2 -timeout 10 >/dev/null 2>&1 &
sipp=$!
sleep 0.5
python3 -c '
import socket, time
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b"ctrace logs off", ("127.0.0.1", 8897))
time.sleep(0.2)
s.sendto(b"ctrace logs on", ("127.0.0.1", 8897))'
wait $sipp
status=$?
logs=`cat uac_*_logs*.log 2>/dev/null`
rm -f uac_*_logs*.log

if test $status -ne 0; then
    fail "exit status $status"
elif ! echo "$logs" | grep -q after; then
    fail "nothing logged after the log was turned on again"
else
    ok
fi
