|
| 1 | +# Tests for FAKETIME_FOLLOW_ABSOLUTE feature. |
| 2 | +# |
| 3 | +# When FAKETIME_FOLLOW_ABSOLUTE=1 is set alongside FAKETIME="%" and |
| 4 | +# FAKETIME_FOLLOW_FILE, time freezes at the follow file's mtime |
| 5 | +# and only advances when the file's mtime changes. |
| 6 | + |
| 7 | +FOLLOW_FILE=".follow_absolute_test_file" |
| 8 | + |
| 9 | +init() |
| 10 | +{ |
| 11 | + typeset testsuite="$1" |
| 12 | + PLATFORM=$(platform) |
| 13 | + if [ -z "$PLATFORM" ]; then |
| 14 | + echo "$testsuite: unknown platform! quitting" |
| 15 | + return 1 |
| 16 | + fi |
| 17 | + echo "# PLATFORM=$PLATFORM" |
| 18 | + return 0 |
| 19 | +} |
| 20 | + |
| 21 | +run() |
| 22 | +{ |
| 23 | + init |
| 24 | + |
| 25 | + run_testcase follow_absolute_basic |
| 26 | + run_testcase follow_absolute_freeze |
| 27 | + run_testcase follow_absolute_tracks_mtime |
| 28 | + |
| 29 | + rm -f "$FOLLOW_FILE" |
| 30 | +} |
| 31 | + |
| 32 | +# Helper to run a command with follow-absolute configuration |
| 33 | +follow_absolute_cmd() |
| 34 | +{ |
| 35 | + FAKETIME_FOLLOW_FILE="$FOLLOW_FILE" \ |
| 36 | + FAKETIME_FOLLOW_ABSOLUTE=1 \ |
| 37 | + fakecmd "%" "$@" |
| 38 | +} |
| 39 | + |
| 40 | +# Test that time matches the follow file's mtime |
| 41 | +follow_absolute_basic() |
| 42 | +{ |
| 43 | + touch -d "2020-03-15 10:30:00 UTC" "$FOLLOW_FILE" |
| 44 | + typeset actual |
| 45 | + actual=$(follow_absolute_cmd date -u +"%Y-%m-%d %H:%M:%S") |
| 46 | + asserteq "$actual" "2020-03-15 10:30:00" \ |
| 47 | + "time should match follow file mtime" |
| 48 | +} |
| 49 | + |
| 50 | +# Test that time stays frozen (does not advance with real time) |
| 51 | +follow_absolute_freeze() |
| 52 | +{ |
| 53 | + touch -d "2020-03-15 10:30:00 UTC" "$FOLLOW_FILE" |
| 54 | + typeset timestamps |
| 55 | + timestamps=$(follow_absolute_cmd \ |
| 56 | + perl -e 'print time(), "\n"; sleep(2); print time(), "\n"') |
| 57 | + typeset first second |
| 58 | + first=$(echo "$timestamps" | head -1) |
| 59 | + second=$(echo "$timestamps" | tail -1) |
| 60 | + asserteq "$first" "$second" \ |
| 61 | + "time should stay frozen within a single process" |
| 62 | +} |
| 63 | + |
| 64 | +# Test that time tracks file mtime changes at millisecond precision |
| 65 | +follow_absolute_tracks_mtime() |
| 66 | +{ |
| 67 | + touch -d "2020-03-15 10:30:00.000 UTC" "$FOLLOW_FILE" |
| 68 | + typeset first |
| 69 | + first=$(follow_absolute_cmd \ |
| 70 | + perl -MTime::HiRes=time -e 'printf "%.3f\n", time()') |
| 71 | + |
| 72 | + touch -d "2020-03-15 10:30:00.005 UTC" "$FOLLOW_FILE" |
| 73 | + typeset second |
| 74 | + second=$(follow_absolute_cmd \ |
| 75 | + perl -MTime::HiRes=time -e 'printf "%.3f\n", time()') |
| 76 | + |
| 77 | + assertneq "$first" "$second" \ |
| 78 | + "time should advance with file mtime (ms precision)" |
| 79 | +} |
0 commit comments