#! /usr/bin/env bash


dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd "$dir"

set -eu

export TIMEOUT_ACTION="WAIT_STARTED"
if orderly -check-delay 60 -max-restart-tokens 1 -restart-tokens-per-second 0 -- \
  -name sv -all-commands ./sv -wait-started-timeout 2 > test1.out
then
  echo "expected failure"
  exit 1
fi

diff -u <(grep "^sv" test1.out) test1.expected


export TIMEOUT_ACTION="CHECK"
if orderly -check-delay 60 -max-restart-tokens 1 -restart-tokens-per-second 0 -- \
  -name sv -all-commands ./sv -check-timeout 2  > test2.out
then
  echo "expected failure"
  exit 1
fi

diff -u <(grep "^sv" test2.out) test2.expected


export TIMEOUT_ACTION="CLEANUP"
if orderly -check-delay 60 -max-restart-tokens 1 -restart-tokens-per-second 0 -- \
  -name sv -all-commands ./sv -cleanup-timeout 2  > test3.out
then
  echo "expected failure"
  exit 1
fi

diff -u <(grep "^sv" test3.out) test3.expected


export TIMEOUT_ACTION="SHUTDOWN"
orderly -check-delay 60 -max-restart-tokens 1 -restart-tokens-per-second 0 -- \
  -name sv -all-commands ./sv -shutdown-timeout 2 > test4.out &

pid="$!"
sleep 1
kill -SIGINT "$pid"
wait

diff -u <(grep "^sv" test4.out) test4.expected

export SHUTDOWN_NOOP="yes"
export TIMEOUT_ACTION="SHUTDOWN"
orderly -check-delay 60 -max-restart-tokens 1 -restart-tokens-per-second 0 -- \
  -name sv -all-commands ./sv -shutdown-timeout 2 > test5.out &

pid="$!"
sleep 1
kill -SIGINT "$pid"
wait

diff -u <(grep "^sv" test5.out) test5.expected
