Skip to content

Commit 7d3204d

Browse files
authored
Improve alloc tests failure messages (#3173)
This PR improves the messaging when tests fail so it's easier to troubleshoot failures.
1 parent b3704d7 commit 7d3204d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

IntegrationTests/test_functions.sh

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function fail() {
1919

2020
function assert_equal() {
2121
if [[ "$1" != "$2" ]]; then
22-
fail "expected '$1', got '$2' ${3-}"
22+
fail "expected '$1', got '$2'${3+: }${3-}"
2323
fi
2424
}
2525

@@ -33,31 +33,38 @@ function assert_equal_files() {
3333
echo "--- SNIP ($2, size=$(wc "$2"), SHA=$(shasum "$2")) ---"
3434
cat "$2"
3535
echo "--- SNAP ($2) ---"
36-
fail "file '$1' not equal to '$2'"
36+
fail "file '$1' not equal to '$2'${3+: }${3-}"
3737
fi
3838
}
3939

4040
function assert_less_than() {
4141
if [[ ! "$1" -lt "$2" ]]; then
42-
fail "assertion '$1' < '$2' failed"
42+
fail "assertion '$1' < '$2' failed${3+: }${3-}"
4343
fi
4444
}
4545

4646
function assert_less_than_or_equal() {
4747
if [[ ! "$1" -le "$2" ]]; then
48-
fail "assertion '$1' <= '$2' failed"
48+
fail "assertion '$1' <= '$2' failed${3+: }${3-}"
4949
fi
5050
}
5151

5252
function assert_greater_than() {
5353
if [[ ! "$1" -gt "$2" ]]; then
54-
fail "assertion '$1' > '$2' failed"
54+
fail "assertion '$1' > '$2' failed${3+: }${3-}"
5555
fi
5656
}
5757

5858
function assert_greater_than_or_equal() {
5959
if [[ ! "$1" -ge "$2" ]]; then
60-
fail "assertion '$1' >= '$2' failed"
60+
fail "assertion '$1' >= '$2' failed${3+: }${3-}"
61+
fi
62+
}
63+
64+
function assert_is_number() {
65+
re='^[0-9]+$'
66+
if ! [[ $1 =~ $re ]]; then
67+
fail "$1 is not a number${2+: }${2-}"
6168
fi
6269
}
6370

IntegrationTests/tests_04_performance/test_01_allocation_counts.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,16 @@ for test in "${all_tests[@]}"; do
5555
leaked_fds=$(grep "^test_$test_case.leaked_fds:" "$tmp/output" | cut -d: -f2 | sed 's/ //g')
5656
max_allowed_env_name="MAX_ALLOCS_ALLOWED_$test_case"
5757
max_allowed=$(jq '.'\""$test_case"\" "$here/Thresholds/$SWIFT_VERSION.json")
58+
59+
assert_is_number "$max_allowed" "Malformed or nonexistent ${SWIFT_VERSION}.json thresholds file"
5860

5961
info "$test_case: allocations not freed: $not_freed_allocations"
6062
info "$test_case: total number of mallocs: $total_allocations"
6163
info "$test_case: leaked fds: $leaked_fds"
6264

63-
assert_less_than "$not_freed_allocations" 5 # allow some slack
64-
assert_greater_than "$not_freed_allocations" -5 # allow some slack
65-
assert_less_than "$leaked_fds" 1 # No slack allowed here though
65+
assert_less_than "$not_freed_allocations" 5 "Allocations not freed are greater than expected" # allow some slack
66+
assert_greater_than "$not_freed_allocations" -5 "Allocations not freed are less than expected" # allow some slack
67+
assert_less_than "$leaked_fds" 1 "There are leaked file descriptors" # No slack allowed here though
6668
if [[ -z "${!max_allowed_env_name+x}" ]] && [ -z "${max_allowed}" ]; then
6769
if [[ -z "${!max_allowed_env_name+x}" ]]; then
6870
warn "no reference number of allocations set (set to \$$max_allowed_env_name)"
@@ -74,8 +76,8 @@ for test in "${all_tests[@]}"; do
7476
if [ -z "${max_allowed}" ]; then
7577
max_allowed=${!max_allowed_env_name}
7678
fi
77-
assert_less_than_or_equal "$total_allocations" "$max_allowed"
78-
assert_greater_than "$total_allocations" "$(( max_allowed - 1000))"
79+
assert_less_than_or_equal "$total_allocations" "$max_allowed" "Total allocations exceed the max allowed"
80+
assert_greater_than "$total_allocations" "$(( max_allowed - 1000))" "Total allocations are less than expected"
7981
fi
8082
done < <(grep "^test_${test}[^\W]*.total_allocations:" "$tmp/output" | cut -d: -f1 | cut -d. -f1 | sort | uniq)
8183
done

0 commit comments

Comments
 (0)