|
| 1 | +require_relative "test_helper" |
| 2 | +require 'timecop' |
| 3 | + |
| 4 | +class TestTimecopWithProcessClock < Minitest::Test |
| 5 | + TIME_EPSILON = 0.001 # seconds - represents enough time for Process.clock_gettime to have advanced if not frozen |
| 6 | + |
| 7 | + def teardown |
| 8 | + Timecop.return |
| 9 | + end |
| 10 | + |
| 11 | + if RUBY_VERSION >= '2.1.0' |
| 12 | + def test_process_clock_gettime_monotonic |
| 13 | + Timecop.freeze do |
| 14 | + assert_same(*consecutive_monotonic, "CLOCK_MONOTONIC is not frozen") |
| 15 | + end |
| 16 | + |
| 17 | + initial_time = monotonic |
| 18 | + Timecop.freeze(-0.5) do |
| 19 | + assert_operator(monotonic, :<, initial_time, "CLOCK_MONOTONIC is not traveling back in time") |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + def test_process_clock_gettime_monotonic_with_date_freeze |
| 24 | + date = Date.new(2024, 6, 1) |
| 25 | + monotonic1 = Timecop.freeze(date) { monotonic } |
| 26 | + monotonic2 = Timecop.freeze(date) { monotonic } |
| 27 | + |
| 28 | + refute_equal(monotonic1, monotonic2, "CLOCK_MONOTONIC is not expected to freeze deterministically with a date") |
| 29 | + end |
| 30 | + |
| 31 | + def test_process_clock_gettime_realtime_with_date_freeze |
| 32 | + date = Date.new(2024, 6, 1) |
| 33 | + realtime_1 = Timecop.freeze(date) { realtime } |
| 34 | + realtime_2 = Timecop.freeze(date) { realtime } |
| 35 | + |
| 36 | + assert_equal(realtime_1, realtime_2, "CLOCK_REALTIME is expected to support freezing with a date") |
| 37 | + end |
| 38 | + |
| 39 | + def test_process_clock_gettime_units_integer |
| 40 | + Timecop.freeze do |
| 41 | + time_in_nanoseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) |
| 42 | + time_in_microseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :microsecond) |
| 43 | + time_in_milliseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) |
| 44 | + time_in_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) |
| 45 | + |
| 46 | + assert_equal(time_in_microseconds, (time_in_nanoseconds / 10**3).to_i) |
| 47 | + assert_equal(time_in_milliseconds, (time_in_nanoseconds / 10**6).to_i) |
| 48 | + assert_equal(time_in_seconds, (time_in_nanoseconds / 10**9).to_i) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + def test_process_clock_gettime_units_float |
| 53 | + Timecop.freeze do |
| 54 | + time_in_nanoseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond).to_f |
| 55 | + |
| 56 | + float_microseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond) |
| 57 | + float_milliseconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) |
| 58 | + float_seconds = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) |
| 59 | + |
| 60 | + delta = 0.000001 |
| 61 | + assert_in_delta(float_microseconds, time_in_nanoseconds / 10**3, delta) |
| 62 | + assert_in_delta(float_milliseconds, time_in_nanoseconds / 10**6, delta) |
| 63 | + assert_in_delta(float_seconds, time_in_nanoseconds / 10**9, delta) |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def test_process_clock_gettime_monotonic_nested |
| 68 | + Timecop.freeze do |
| 69 | + parent = monotonic |
| 70 | + |
| 71 | + sleep(TIME_EPSILON) |
| 72 | + |
| 73 | + delta = 0.5 |
| 74 | + Timecop.freeze(delta) do |
| 75 | + child = monotonic |
| 76 | + assert_equal(child, parent + delta, "Nested freeze not working for monotonic time") |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + def test_process_clock_gettime_monotonic_travel |
| 82 | + initial_time = monotonic |
| 83 | + Timecop.travel do |
| 84 | + refute_same(*consecutive_monotonic, "CLOCK_MONOTONIC is frozen") |
| 85 | + assert_operator(monotonic, :>, initial_time, "CLOCK_MONOTONIC is not moving forward") |
| 86 | + end |
| 87 | + |
| 88 | + Timecop.travel(-0.5) do |
| 89 | + refute_same(*consecutive_monotonic, "CLOCK_MONOTONIC is frozen") |
| 90 | + assert_operator(monotonic, :<, initial_time, "CLOCK_MONOTONIC is not traveling properly") |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + def test_process_clock_gettime_monotonic_scale |
| 95 | + scale = 4 |
| 96 | + sleep_length = 0.25 |
| 97 | + Timecop.scale(scale) do |
| 98 | + initial_time = monotonic |
| 99 | + sleep(sleep_length) |
| 100 | + expected_time = initial_time + (scale * sleep_length) |
| 101 | + assert_times_effectively_equal expected_time, monotonic, 0.1, "CLOCK_MONOTONIC is not scaling" |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + def test_process_clock_gettime_realtime |
| 106 | + Timecop.freeze do |
| 107 | + assert_same(*consecutive_realtime, "CLOCK_REALTIME is not frozen") |
| 108 | + end |
| 109 | + |
| 110 | + initial_time = realtime |
| 111 | + Timecop.freeze(-20) do |
| 112 | + assert_operator(realtime, :<, initial_time, "CLOCK_REALTIME is not traveling back in time") |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + def test_process_clock_gettime_realtime_travel |
| 117 | + initial_time = realtime |
| 118 | + Timecop.travel do |
| 119 | + refute_equal consecutive_realtime, "CLOCK_REALTIME is frozen" |
| 120 | + assert_operator(realtime, :>, initial_time, "CLOCK_REALTIME is not moving forward") |
| 121 | + end |
| 122 | + |
| 123 | + delta = 0.1 |
| 124 | + Timecop.travel(Time.now - delta) do |
| 125 | + refute_equal consecutive_realtime, "CLOCK_REALTIME is frozen" |
| 126 | + assert_operator(realtime, :<, initial_time, "CLOCK_REALTIME is not traveling properly") |
| 127 | + sleep(delta) |
| 128 | + assert_operator(realtime, :>, initial_time, "CLOCK_REALTIME is not traveling properly") |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def test_process_clock_gettime_realtime_scale |
| 133 | + scale = 4 |
| 134 | + sleep_length = 0.25 |
| 135 | + Timecop.scale(scale) do |
| 136 | + initial_time = realtime |
| 137 | + sleep(sleep_length) |
| 138 | + assert_operator(initial_time + scale * sleep_length, :<, realtime, "CLOCK_REALTIME is not scaling") |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + private |
| 143 | + |
| 144 | + def monotonic |
| 145 | + Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 146 | + end |
| 147 | + |
| 148 | + def realtime |
| 149 | + Process.clock_gettime(Process::CLOCK_REALTIME) |
| 150 | + end |
| 151 | + |
| 152 | + def consecutive_monotonic |
| 153 | + consecutive_times(:monotonic) |
| 154 | + end |
| 155 | + |
| 156 | + def consecutive_realtime |
| 157 | + consecutive_times(:realtime) |
| 158 | + end |
| 159 | + |
| 160 | + def consecutive_times(time_method) |
| 161 | + t1 = send(time_method) |
| 162 | + sleep(TIME_EPSILON) |
| 163 | + t2 = send(time_method) |
| 164 | + |
| 165 | + [t1, t2] |
| 166 | + end |
| 167 | + end |
| 168 | +end |
0 commit comments