-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from kuno1/issue/3
Add tests Close #3.
- Loading branch information
Showing
6 changed files
with
158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
/immortalize | ||
/work/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,9 @@ schema: | |
commands: | ||
build: | ||
status: true | ||
args: | ||
- -o | ||
- work/immortalize | ||
watcher: | ||
extensions: | ||
- go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/bash -eu | ||
|
||
# https://stackoverflow.com/questions/9256644 | ||
trap_with_arg() { | ||
func="$1" ; shift | ||
for sig ; do | ||
# shellcheck disable=SC2064 | ||
trap "$func $sig" "$sig" | ||
done | ||
} | ||
|
||
func_trap() { | ||
>&2 echo "Trapped: $1" | ||
exit 1 | ||
} | ||
|
||
trap_with_arg func_trap INT TERM EXIT | ||
|
||
for i in {1..8}; do | ||
sleep 0.5 | ||
>&2 echo "$i / 8" | ||
done | ||
|
||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#!/bin/bash -eu | ||
|
||
# https://stackoverflow.com/questions/9256644 | ||
trap_with_arg() { | ||
func="$1" ; shift | ||
for sig ; do | ||
# shellcheck disable=SC2064 | ||
trap "$func $sig" "$sig" | ||
done | ||
} | ||
|
||
func_trap() { | ||
echo "Trapped: $1" | ||
exit 0 | ||
} | ||
|
||
trap_with_arg func_trap INT TERM EXIT | ||
|
||
for i in {1..8}; do | ||
sleep 0.5 | ||
>&2 echo "$i / 8" | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env bats | ||
|
||
result () { | ||
>&3 cat work/time | ||
# Floor the time | ||
awk '{split($0,a,"."); print a[1]}' < work/time | ||
} | ||
|
||
time_cmd= | ||
|
||
measure () { | ||
if [ "$time_cmd" == '' ]; then | ||
if [ -f /usr/bin/time ]; then | ||
time_cmd=/usr/bin/time | ||
elif [ -f work/cache/time ]; then | ||
time_cmd=work/cache/time | ||
else | ||
>&3 echo 'Error: time command does not exist.' | ||
exit 1 | ||
fi | ||
fi | ||
|
||
# Measure immortalize and redirect stdout/stderr to file descriptor 3. | ||
# Details: | ||
# https://github.com/bats-core/bats-core#file-descriptor-3-read-this-if-bats-hangs | ||
"$time_cmd" -f '%e' -o work/time ./work/immortalize "$@" >&3 2>&1 & | ||
# Store time's PID | ||
echo "$!" > work/pid | ||
} | ||
|
||
sigterm () { | ||
# Send SIGTERM to immortalize by using time's PID | ||
pkill -P "$(< work/pid)" | ||
} | ||
|
||
# MIN -> COM | ||
@test "MIN -> COM" { | ||
measure -min-lifetime 2 -command test-bin/command-zero | ||
wait | ||
[ "$(result)" == 4 ] | ||
} | ||
|
||
# COM -> MIN | ||
@test "COM -> MIN" { | ||
measure -min-lifetime 6 -command test-bin/command-zero | ||
wait | ||
[ "$(result)" == 4 ] | ||
} | ||
|
||
# SIG -> MIN | ||
@test "SIG -> MIN" { | ||
measure -min-lifetime 2 -command test-bin/command-zero | ||
sleep 1 | ||
sigterm | ||
wait | ||
[ "$(result)" == 2 ] | ||
} | ||
|
||
# MIN -> SIG | ||
@test "MIN -> SIG" { | ||
measure -min-lifetime 2 -command test-bin/command-zero | ||
sleep 3 | ||
sigterm | ||
wait | ||
[ "$(result)" == 3 ] | ||
} |