Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions tests/unit/assert_duration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,51 @@ function test_successful_assert_duration_within_fast_command() {
assert_empty "$(assert_duration "echo hello" 5000)"
}

# Failure paths mock bashunit::duration::measure_ms: they exercise the
# threshold comparison and failure message, not the timing itself, so a real
# `sleep 1` per test only slowed the suite down (#826).
function test_unsuccessful_assert_duration_exceeds_threshold() {
bashunit::mock bashunit::duration::measure_ms <<<"2000"

assert_same \
"$(bashunit::console_results::print_failed_test \
"Unsuccessful assert duration exceeds threshold" \
"1000" "to complete within (ms)" "sleep 1")" \
"$(assert_duration "sleep 1" 1000)"
"1000" "to complete within (ms)" "fake_slow_command")" \
"$(assert_duration "fake_slow_command" 1000)"
}

function test_successful_assert_duration_less_than() {
assert_empty "$(assert_duration_less_than "sleep 0" 5000)"
}

function test_unsuccessful_assert_duration_less_than() {
bashunit::mock bashunit::duration::measure_ms <<<"2000"

assert_same \
"$(bashunit::console_results::print_failed_test \
"Unsuccessful assert duration less than" \
"100" "to complete within (ms)" "sleep 1")" \
"$(assert_duration_less_than "sleep 1" 100)"
"100" "to complete within (ms)" "fake_slow_command")" \
"$(assert_duration_less_than "fake_slow_command" 100)"
}

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

assert_empty "$(assert_duration_greater_than "sleep 0.2" 100)"
}

function test_unsuccessful_assert_duration_greater_than() {
bashunit::mock bashunit::duration::measure_ms <<<"3"

assert_same \
"$(bashunit::console_results::print_failed_test \
"Unsuccessful assert duration greater than" \
"5000" "to take at least (ms)" "echo hello")" \
"$(assert_duration_greater_than "echo hello" 5000)"
"5000" "to take at least (ms)" "fake_fast_command")" \
"$(assert_duration_greater_than "fake_fast_command" 5000)"
}
Loading