|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.api; |
| 20 | + |
| 21 | +import java.time.Clock; |
| 22 | +import java.time.Duration; |
| 23 | +import java.time.Instant; |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.ZoneOffset; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.DisplayName; |
| 28 | +import org.junit.jupiter.api.Nested; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 35 | + |
| 36 | +class MonotonicClockTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + @DisplayName("MonotonicClock singleton instance should always return the same instance") |
| 40 | + void testSingletonInstance() { |
| 41 | + MonotonicClock clock1 = MonotonicClock.get(); |
| 42 | + MonotonicClock clock2 = MonotonicClock.get(); |
| 43 | + |
| 44 | + assertSame(clock1, clock2, "Multiple calls to get() should return the same instance"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + @DisplayName("MonotonicClock should always use UTC timezone") |
| 49 | + void testClockTimezone() { |
| 50 | + MonotonicClock clock = MonotonicClock.get(); |
| 51 | + |
| 52 | + assertEquals(ZoneOffset.UTC, clock.getZone(), "Clock should use UTC timezone"); |
| 53 | + |
| 54 | + // Verify that attempting to change timezone returns the same instance |
| 55 | + Clock newClock = clock.withZone(ZoneId.systemDefault()); |
| 56 | + assertSame(clock, newClock, "withZone() should return the same clock instance"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + @DisplayName("MonotonicClock should maintain monotonic time progression") |
| 61 | + void testMonotonicBehavior() throws InterruptedException { |
| 62 | + Instant first = MonotonicClock.now(); |
| 63 | + Thread.sleep(10); // Small delay |
| 64 | + Instant second = MonotonicClock.now(); |
| 65 | + Thread.sleep(10); // Small delay |
| 66 | + Instant third = MonotonicClock.now(); |
| 67 | + |
| 68 | + assertTrue(first.isBefore(second), "Time should progress forward between measurements"); |
| 69 | + assertTrue(second.isBefore(third), "Time should progress forward between measurements"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @DisplayName("MonotonicClock elapsed time should increase") |
| 74 | + void testElapsedTime() throws InterruptedException { |
| 75 | + Duration initial = MonotonicClock.elapsed(); |
| 76 | + Thread.sleep(50); // Longer delay for more reliable measurement |
| 77 | + Duration later = MonotonicClock.elapsed(); |
| 78 | + |
| 79 | + assertTrue(later.compareTo(initial) > 0, "Elapsed time should increase"); |
| 80 | + assertTrue( |
| 81 | + later.minus(initial).toMillis() >= 45, |
| 82 | + "Elapsed time difference should be at least 45ms (accounting for some timing variance)"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + @DisplayName("MonotonicClock start time should remain constant") |
| 87 | + void testStartTime() throws InterruptedException { |
| 88 | + Instant start1 = MonotonicClock.start(); |
| 89 | + Thread.sleep(10); |
| 90 | + Instant start2 = MonotonicClock.start(); |
| 91 | + |
| 92 | + assertEquals(start1, start2, "Start time should remain constant"); |
| 93 | + assertNotNull(start1, "Start time should not be null"); |
| 94 | + } |
| 95 | + |
| 96 | + @Nested |
| 97 | + @DisplayName("Time consistency tests") |
| 98 | + class TimeConsistencyTests { |
| 99 | + |
| 100 | + @Test |
| 101 | + @DisplayName("Current time should be after start time") |
| 102 | + void testCurrentTimeAfterStart() { |
| 103 | + Instant now = MonotonicClock.now(); |
| 104 | + Instant start = MonotonicClock.start(); |
| 105 | + |
| 106 | + assertTrue(now.isAfter(start), "Current time should be after start time"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + @DisplayName("Elapsed time should match time difference") |
| 111 | + void testElapsedTimeConsistency() { |
| 112 | + MonotonicClock clock = MonotonicClock.get(); |
| 113 | + Instant now = clock.instant(); |
| 114 | + Duration elapsed = clock.elapsedTime(); |
| 115 | + Duration calculated = Duration.between(clock.startInstant(), now); |
| 116 | + |
| 117 | + // Allow for small timing differences (1ms) due to execution time between measurements |
| 118 | + assertTrue( |
| 119 | + Math.abs(elapsed.toMillis() - calculated.toMillis()) <= 1, |
| 120 | + "Elapsed time should match calculated duration between start and now"); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + @DisplayName("MonotonicClock should handle rapid successive calls") |
| 126 | + void testRapidCalls() { |
| 127 | + Instant[] instants = new Instant[1000]; |
| 128 | + for (int i = 0; i < instants.length; i++) { |
| 129 | + instants[i] = MonotonicClock.now(); |
| 130 | + } |
| 131 | + |
| 132 | + // Verify monotonic behavior across all measurements |
| 133 | + for (int i = 1; i < instants.length; i++) { |
| 134 | + assertTrue( |
| 135 | + instants[i].compareTo(instants[i - 1]) >= 0, |
| 136 | + "Time should never go backwards even with rapid successive calls"); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + @DisplayName("MonotonicClock should maintain reasonable alignment with system time") |
| 142 | + void testSystemTimeAlignment() { |
| 143 | + Instant monotonic = MonotonicClock.now(); |
| 144 | + Instant system = Instant.now(); |
| 145 | + |
| 146 | + // The difference should be relatively small (allow for 1 second max) |
| 147 | + Duration difference = Duration.between(monotonic, system).abs(); |
| 148 | + assertTrue(difference.getSeconds() <= 1, "Monotonic time should be reasonably aligned with system time"); |
| 149 | + } |
| 150 | +} |
0 commit comments