Skip to content
This repository has been archived by the owner on Oct 13, 2020. It is now read-only.

Commit

Permalink
Merge branch 'robot'
Browse files Browse the repository at this point in the history
Hanging scripts don't stick the robot anymore (timeout).
  • Loading branch information
ptitfred committed Feb 13, 2012
2 parents 4896d98 + 93ab1ae commit 297b7b4
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 18 deletions.
14 changes: 13 additions & 1 deletion robot/run.sh
@@ -1,5 +1,7 @@
#!/bin/bash

source utils/robot

function info {
echo "[INFO]" $*
}
Expand All @@ -18,6 +20,13 @@ function lsTestCases {
find . -maxdepth 1 -regex "\./t[0-9]*" -type d
}

function killChildren {
children=$(ps h --ppid $1 -o "%p")
if [ "$children" != "" ]; then
safeKill $children
fi
}

function runTestCase {
local tc=$1
cd $tc
Expand All @@ -30,10 +39,13 @@ function runTestCase {
echo "/-- $tc --------------------------------------------------------------------------\\"
ts1=$(date +%s)
ns1=$(date +%N)
PATH=${BASEDIR}/${install_dir}/scripts:$PATH bash run.sh
PATH=${BASEDIR}/${install_dir}/scripts:${BASEDIR}/utils:$PATH bash run.sh &
testPid=$!
wait $testPid
ec=$?
ts2=$(date +%s)
ns2=$(date +%N)
killChildren $testPid
echo "\\--$padding----------------------------------------------------------------------------/"
sec=$(echo "scale=3; ($ts2 - $ts1) * 1 + $ns2 / 1000000000 - $ns1 / 1000000000" | bc -l)
info "Took ${sec} sec"
Expand Down
20 changes: 3 additions & 17 deletions robot/t1/run.sh
@@ -1,26 +1,12 @@
#!/bin/bash

function check {
if [ $1 -gt 0 ]; then
exit $1
fi
}
source robot

(
runOrDie -t 3 -l output.log \
magrit build-log -3
check $?
) | tee output.log
check $?

lines=$(wc -l <output.log)

function assert {
test $1 -eq $2
local ec=$?
if [ $ec -gt 0 ]; then
echo "{assert} Expected $1 but was $2"
exit $ec
fi
}

assert 4 $lines

119 changes: 119 additions & 0 deletions robot/utils/robot
@@ -0,0 +1,119 @@
function check {
if [ $1 -gt 0 ]; then
exit $1
fi
}

function assert {
test $1 -eq $2
local ec=$?
if [ $ec -gt 0 ]; then
echo "{assert} Expected $1 but was $2"
exit $ec
fi
}

function isAlive {
ps | grep $1 | wc -l
}

function now {
date +%s
}

function checkTimeout {
local timeout=$1
local startDate=$2
local elapsed=$(($(now) - $startDate))
if [ $timeout -le $elapsed ]; then
echo 1
else
echo 0
fi
}

function safeKill {
local PID=$*
echo "Killing $PID" >&2
kill -15 $PID
# Give process time to terminate
if [ $(isAlive $PID) -eq 1 ]; then
(sleep 2; kill -1 $PID)
else
echo "Soft kill of $PID" >&2
return 0
fi
if [ $(isAlive $PID) -eq 1 ]; then
(sleep 2; kill -9 $PID)
else
echo "Insisting kill of $PID" >&2
return 0
fi
if [ $(isAlive $PID) -eq 1 ]; then
echo "Failed to kill $PID" >&2
return 255
else
echo "Hard kill of $PID" >&2
return 0
fi
}

function listChildren {
if [ $# -eq 0 ]
then
ppid=$$
else
ppid=$1
fi
children=$(ps h --ppid $ppid -o "%p")
for child in $children; do
if [ "$2" = "--" ]; then
echo $(listChildren $child)
fi
echo $child
done
}

function retain {
largeList=( $1 )
excludedList=( $2 )
OLDIFS="$IFS"
IFS=$'\n'
SET3=( $(grep -Fxv "${excludedList[*]}" <<< "${largeList[*]}") )
IFS="$OLDIFS"
echo "${SET3[*]}"
}

function runOrDie {
local timeout=0
if [ "$1" = "-t" ]; then
timeout=$2
shift 2
fi
local logs="/dev/null"
if [ "$1" = "-l" ]; then
logs=$2
shift 2
fi
local executable=$1
shift 1
elder=$(listChildren)
$executable $* | tee $logs &
PID=$!
local startDate=$(now)
while [ $(isAlive $PID) -eq 1 ]; do
hasTimeOut=$(checkTimeout $timeout $startDate)
if [ $hasTimeOut -eq 1 ]; then
echo "TIMEOUT $executable" >&2
newborns=$(listChildren $$ --)
newChildren=$(retain "$newborns" "$elder")
safeKill "$newChildren"
return 255
else
sleep 1s
fi
done
# exit with the error code of the spawned process
return $(wait $PID)
}

0 comments on commit 297b7b4

Please sign in to comment.