Skip to content

Commit d556d22

Browse files
committed
perf(test): mock measure_ms in assert_duration failure paths
The three failure-path tests only exercise the threshold comparison and the failure message, yet each paid a real 'sleep 1' — 3s of the file's 3.3s runtime. Mock bashunit::duration::measure_ms (heredoc form, so call args are not appended to the fake reading) and assert against fake commands instead: ~3.3s -> ~0.4s. One success path keeps a real short sleep so measure_ms integration stays covered end-to-end, guarded to skip under the last-resort date-seconds clock whose 1s resolution would read 'sleep 0.2' as 0ms. Closes #826
1 parent e3e982f commit d556d22

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

tests/unit/assert_duration_test.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,51 @@ function test_successful_assert_duration_within_fast_command() {
99
assert_empty "$(assert_duration "echo hello" 5000)"
1010
}
1111

12+
# Failure paths mock bashunit::duration::measure_ms: they exercise the
13+
# threshold comparison and failure message, not the timing itself, so a real
14+
# `sleep 1` per test only slowed the suite down (#826).
1215
function test_unsuccessful_assert_duration_exceeds_threshold() {
16+
bashunit::mock bashunit::duration::measure_ms <<<"2000"
17+
1318
assert_same \
1419
"$(bashunit::console_results::print_failed_test \
1520
"Unsuccessful assert duration exceeds threshold" \
16-
"1000" "to complete within (ms)" "sleep 1")" \
17-
"$(assert_duration "sleep 1" 1000)"
21+
"1000" "to complete within (ms)" "fake_slow_command")" \
22+
"$(assert_duration "fake_slow_command" 1000)"
1823
}
1924

2025
function test_successful_assert_duration_less_than() {
2126
assert_empty "$(assert_duration_less_than "sleep 0" 5000)"
2227
}
2328

2429
function test_unsuccessful_assert_duration_less_than() {
30+
bashunit::mock bashunit::duration::measure_ms <<<"2000"
31+
2532
assert_same \
2633
"$(bashunit::console_results::print_failed_test \
2734
"Unsuccessful assert duration less than" \
28-
"100" "to complete within (ms)" "sleep 1")" \
29-
"$(assert_duration_less_than "sleep 1" 100)"
35+
"100" "to complete within (ms)" "fake_slow_command")" \
36+
"$(assert_duration_less_than "fake_slow_command" 100)"
3037
}
3138

39+
# Keep one success path on a real (short) sleep so measure_ms integration
40+
# stays covered end-to-end. The last-resort date-seconds clock has 1s
41+
# resolution and would measure a sub-second sleep as 0ms, so skip there.
3242
function test_successful_assert_duration_greater_than() {
33-
assert_empty "$(assert_duration_greater_than "sleep 1" 500)"
43+
bashunit::clock::now_to_slot >/dev/null 2>&1 || true
44+
if [ "${_BASHUNIT_CLOCK_NOW_IMPL:-}" = "date-seconds" ]; then
45+
bashunit::skip "clock has 1s resolution" && return
46+
fi
47+
48+
assert_empty "$(assert_duration_greater_than "sleep 0.2" 100)"
3449
}
3550

3651
function test_unsuccessful_assert_duration_greater_than() {
52+
bashunit::mock bashunit::duration::measure_ms <<<"3"
53+
3754
assert_same \
3855
"$(bashunit::console_results::print_failed_test \
3956
"Unsuccessful assert duration greater than" \
40-
"5000" "to take at least (ms)" "echo hello")" \
41-
"$(assert_duration_greater_than "echo hello" 5000)"
57+
"5000" "to take at least (ms)" "fake_fast_command")" \
58+
"$(assert_duration_greater_than "fake_fast_command" 5000)"
4259
}

0 commit comments

Comments
 (0)