Skip to content

Commit

Permalink
add the first allocation tests for nio-http2 (apple#122)
Browse files Browse the repository at this point in the history
Motivation:

Allocation tests are good :).

Modifications:

Add two allocations tests and NIO's integration test framework.

Result:

Hopefully fewer allocations in the future. And better ways to check what
allocations are happening.
  • Loading branch information
weissi authored and Lukasa committed May 17, 2019
1 parent 6ab1903 commit 16fae8a
Show file tree
Hide file tree
Showing 12 changed files with 682 additions and 0 deletions.
58 changes: 58 additions & 0 deletions IntegrationTests/plugin_echo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

function plugin_echo_test_suite_begin() {
echo "Running test suite '$1'"
}

function plugin_echo_test_suite_end() {
true
}

# test_name
function plugin_echo_test_begin() {
echo -n "Running test '$1'... "
}

function plugin_echo_test_skip() {
echo "Skipping test '$1'"
}

function plugin_echo_test_ok() {
echo "OK (${1}s)"
}

function plugin_echo_test_fail() {
echo "FAILURE ($1)"
echo "--- OUTPUT BEGIN ---"
cat "$2"
echo "--- OUTPUT END ---"
}

function plugin_echo_test_end() {
true
}

function plugin_echo_summary_ok() {
echo "OK (ran $1 tests successfully)"
}

function plugin_echo_summary_fail() {
echo "FAILURE (oks: $1, failures: $2)"
}

function plugin_echo_init() {
true
}
119 changes: 119 additions & 0 deletions IntegrationTests/plugin_junit_xml.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

junit_testsuite_time=0

function junit_output_write() {
extra_flags=""
if [[ "$1" == "-n" ]]; then
extra_flags="-n"
shift
fi
test -n "$junit_xml_output"
echo $extra_flags "$*" >> "$junit_xml_output"
}

function junit_output_cat() {
cat "$@" >> "$junit_xml_output"
}

# search, replace
function junit_output_replace() {
test -n "$junit_xml_output"
case "$(uname -s)" in
Linux)
sed -i "s/$1/$2/g" "$junit_xml_output"
;;
*)
sed -i "" "s/$1/$2/g" "$junit_xml_output"
;;
esac
}

function plugin_junit_xml_test_suite_begin() {
junit_testsuite_time=0
junit_output_write "<testsuite name='$1' hostname='$(hostname)' "\
"timestamp='$(date -u +"%Y-%m-%dT%H:%M:%S")' tests='XXX-TESTS-XXX' "\
"failures='XXX-FAILURES-XXX' time='XXX-TIME-XXX' errors='0' id='$(date +%s)'"\
" package='NIOIntegrationTests.$1'>"
}

function plugin_junit_xml_test_suite_end() {
junit_repl_success_and_fail "$1" "$2"
junit_output_write "</testsuite>"
}

# test_name
function plugin_junit_xml_test_begin() {
junit_output_write -n " <testcase classname='NIOIntegrationTests.$2' name='$1'"
}

function plugin_junit_xml_test_skip() {
true
}

function plugin_junit_xml_test_ok() {
time_ms=$1
junit_output_write " time='$time_ms'>"
junit_testsuite_time=$((junit_testsuite_time + time_ms))
}

function plugin_junit_xml_test_fail() {
time_ms=$1
junit_output_write " time='$time_ms'>"
junit_output_write " <failure type='test_fail'>"
junit_output_write " <system-out>"
junit_output_write ' <![CDATA['
junit_output_cat "$2"
junit_output_write ' ]]>'
junit_output_write " </system-out>"
junit_output_write " </failure>"
}

function plugin_junit_xml_test_end() {
junit_output_write " </testcase>"
}

function junit_repl_success_and_fail() {
junit_output_replace XXX-TESTS-XXX "$(($1 + $2))"
junit_output_replace XXX-FAILURES-XXX "$2"
junit_output_replace XXX-TIME-XXX "$junit_testsuite_time"
}

function plugin_junit_xml_summary_ok() {
junit_output_write "</testsuites>"
}

function plugin_junit_xml_summary_fail() {
junit_output_write "</testsuites>"
}

function plugin_junit_xml_init() {
junit_xml_output=""
for f in "$@"; do
if [[ "$junit_xml_output" = "PLACEHOLDER" ]]; then
junit_xml_output="$f"
fi
if [[ "$f" == "--junit-xml" && -z "$junit_xml_output" ]]; then
junit_xml_output="PLACEHOLDER"
fi
done

if [[ -z "$junit_xml_output" || "$junit_xml_output" = "PLACEHOLDER" ]]; then
echo >&2 "ERROR: you need to specify the output after the --junit-xml argument"
false
fi
echo "<testsuites>" > "$junit_xml_output"
}
33 changes: 33 additions & 0 deletions IntegrationTests/run-single-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

(
# this sub-shell is where the actual test is run
set -eu
set -x
set -o pipefail

test="$1"
tmp="$2"
root="$3"
g_show_info="$4"
here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source "$here/test_functions.sh"
source "$test"
wait
)
exit_code=$?
exit $exit_code
159 changes: 159 additions & 0 deletions IntegrationTests/run-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftNIO open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftNIO project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

set -eu

shopt -s nullglob

here="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
tmp=$(mktemp -d /tmp/.swift-nio-http1-server-sh-tests_XXXXXX)

# start_time
function time_diff_to_now() {
echo "$(( $(date +%s) - $1 ))"
}

function plugins_do() {
local method
method="$1"
shift
for plugin in $plugins; do
cd "$orig_cwd"
"plugin_${plugin}_${method}" "$@"
cd - > /dev/null
done
}

source "$here/plugin_echo.sh"
source "$here/plugin_junit_xml.sh"

plugins="echo"
plugin_opts_ind=0
if [[ "${1-default}" == "--junit-xml" ]]; then
plugins="echo junit_xml"
plugin_opts_ind=2
fi

function usage() {
echo >&2 "Usage: $0 [OPTIONS]"
echo >&2
echo >&2 "OPTIONS:"
echo >&2 " -f FILTER: Only run tests matching FILTER (regex)"
}

orig_cwd=$(pwd)
cd "$here"

plugins_do init "$@"
shift $plugin_opts_ind

filter="."
verbose=false
show_info=false
debug=false
while getopts "f:vid" opt; do
case $opt in
f)
filter="$OPTARG"
;;
v)
verbose=true
;;
i)
show_info=true
;;
d)
debug=true
;;
\?)
usage
exit 1
;;
esac
done

function run_test() {
if $verbose; then
"$@" 2>&1 | tee -a "$out"
# we need to return the return value of the first command
return ${PIPESTATUS[0]}
else
"$@" >> "$out" 2>&1
fi
}

exec 3>&1 4>&2 # copy stdout/err to fd 3/4 to we can output control messages
cnt_ok=0
cnt_fail=0
for f in tests_*; do
suite_ok=0
suite_fail=0
plugins_do test_suite_begin "$f"
start_suite=$(date +%s)
cd "$f"
for t in test_*.sh; do
if [[ ! "$f/$t" =~ $filter ]]; then
plugins_do test_skip "$t"
continue
fi
out=$(mktemp "$tmp/test.out_XXXXXX")
test_tmp=$(mktemp -d "$tmp/test.tmp_XXXXXX")
plugins_do test_begin "$t" "$f"
start=$(date +%s)
if run_test "$here/run-single-test.sh" "$here/$f/$t" "$test_tmp" "$here/.." "$show_info"; then
plugins_do test_ok "$(time_diff_to_now $start)"
suite_ok=$((suite_ok+1))
if $verbose; then
cat "$out"
fi
else
plugins_do test_fail "$(time_diff_to_now $start)" "$out"
suite_fail=$((suite_fail+1))
fi
if ! $debug; then
rm "$out"
rm -rf "$test_tmp"
fi
plugins_do test_end
done
cnt_ok=$((cnt_ok + suite_ok))
cnt_fail=$((cnt_fail + suite_fail))
cd ..
plugins_do test_suite_end "$(time_diff_to_now $start_suite)" "$suite_ok" "$suite_fail"
done

if ! $debug; then
rm -rf "$tmp"
else
echo >&2 "debug mode, not deleting '$tmp'"
fi


# report
if [[ $cnt_fail > 0 ]]; then
# kill leftovers (the whole process group)
trap '' TERM
kill 0

plugins_do summary_fail "$cnt_ok" "$cnt_fail"
else
plugins_do summary_ok "$cnt_ok" "$cnt_fail"
fi

if [[ $cnt_fail > 0 ]]; then
exit 1
else
exit 0
fi
Loading

0 comments on commit 16fae8a

Please sign in to comment.