#!/bin/bash
set -e
. tests/lib

t-restrict x-dgit-skip-suite,buster
t-dependencies autopkgtest procps

# Here we test the Oracle qua daemon -- forking, signals etc..
# The real tests of its functionality are in tests/tests/t2u*.
# This test is deliberately not named tests/tests/t2u-oracled because
# it isn't likely to be helpful when hacking on actual tag2upload
# functionality, and because it tests the daemon qua daemon, it is
# named after, precisely, the tested daemon.

t-drs

t-t2u-start-t2u-oracled

# (1)  Test it wants a proper greeting.

echo "t2u-manager-not-ready" >&5
read response <&6
test "${response:0:19}" = "protocol-violation "

# (2)  Test it restarts the worker that complained about our greeting.

sleep 0.1
kill -s SIGALRM $t2u_oracled_pid
sleep 0.1

t-t2u-o2m-expect-fresh-connection

echo "t2u-manager-ready" >&5
read version <&6
test "$version" = "t2u-oracle-version 2"

# (3)  Check ayt/ack works.

read worker_id <&6
echo ayt >&5
read response <&6
test "$response" = ack

# (4)  Test it propagates fatal signals.

pidwait --parent $t2u_oracled_pid &
pidwait_pid=$!
sleep 0.1
kill -s SIGINT $t2u_oracled_pid
wait $pidwait_pid

exec 5>&- 6<&-

t-ok
