From 58540ba7aa7131fb05eae3e80e296ccdca5aa93b Mon Sep 17 00:00:00 2001 From: Gabriel Comte Date: Mon, 30 Mar 2026 09:47:15 +0200 Subject: [PATCH] Replace absolute timing assertion with relative comparison The caching test asserted the first API call takes >1ms (absolute), which is fragile on fast networks. Now asserts the cached call is at least 10x faster than the first. --- src/currency/fiat.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/currency/fiat.rs b/src/currency/fiat.rs index 7c758da..d9b9886 100644 --- a/src/currency/fiat.rs +++ b/src/currency/fiat.rs @@ -98,15 +98,19 @@ mod tests { fn test_exchange_rate_caching() { let start = Instant::now(); - // First call should take a while. + // First call fetches from the API. let btc_value = Fiat::USD.btc_value(); - let elapsed_first_call = start.elapsed().as_micros(); + let elapsed_first_call = start.elapsed(); assert!(btc_value > 0.0); - assert!(elapsed_first_call > 1000); - // Second call should be fast (even when looking up another fiat currency). + // Second call should use cached data and be much faster. + let start2 = Instant::now(); let btc_value = Fiat::EUR.btc_value(); + let elapsed_second_call = start2.elapsed(); assert!(btc_value > 0.0); - assert!(start.elapsed().as_micros() - elapsed_first_call < 1000); + assert!( + elapsed_second_call < elapsed_first_call / 10, + "Second call ({elapsed_second_call:?}) should be much faster than first ({elapsed_first_call:?})" + ); } }