|
16 | 16 | import static me.ahoo.cosid.machine.DefaultClockBackwardsSynchronizer.DEFAULT_BROKEN_THRESHOLD; |
17 | 17 | import static org.junit.jupiter.api.Assertions.*; |
18 | 18 | import static org.hamcrest.MatcherAssert.assertThat; |
19 | | -import static org.hamcrest.Matchers.*; |
20 | 19 |
|
| 20 | +import me.ahoo.cosid.CosIdException; |
21 | 21 | import me.ahoo.cosid.snowflake.exception.ClockTooManyBackwardsException; |
22 | 22 |
|
23 | 23 | import lombok.SneakyThrows; |
24 | 24 | import org.junit.jupiter.api.Assertions; |
25 | 25 | import org.junit.jupiter.api.Test; |
26 | 26 |
|
27 | 27 | class DefaultClockBackwardsSynchronizerTest { |
28 | | - |
| 28 | + DefaultClockBackwardsSynchronizer synchronizer = new DefaultClockBackwardsSynchronizer(); |
| 29 | + |
29 | 30 | @SneakyThrows |
30 | 31 | @Test |
31 | 32 | void sync() { |
32 | | - DefaultClockBackwardsSynchronizer clockBackwardsSynchronizer = new DefaultClockBackwardsSynchronizer(); |
33 | | - clockBackwardsSynchronizer.sync(System.currentTimeMillis()); |
34 | | - clockBackwardsSynchronizer.sync(System.currentTimeMillis() + 1); |
35 | | - clockBackwardsSynchronizer.sync(System.currentTimeMillis() - 10); |
36 | | - clockBackwardsSynchronizer.sync(System.currentTimeMillis() + 10); |
| 33 | + synchronizer.sync(System.currentTimeMillis()); |
| 34 | + synchronizer.sync(System.currentTimeMillis() + 1); |
| 35 | + synchronizer.sync(System.currentTimeMillis() + 1); |
| 36 | + synchronizer.sync(System.currentTimeMillis() + 1); |
| 37 | + synchronizer.sync(System.currentTimeMillis() - 10); |
| 38 | + synchronizer.sync(System.currentTimeMillis() + 10); |
37 | 39 | Assertions.assertThrows(ClockTooManyBackwardsException.class, () -> { |
38 | | - clockBackwardsSynchronizer.sync(System.currentTimeMillis() + DEFAULT_BROKEN_THRESHOLD + 100); |
| 40 | + synchronizer.sync(System.currentTimeMillis() + DEFAULT_BROKEN_THRESHOLD + 100); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + @SneakyThrows |
| 45 | + @Test |
| 46 | + void syncUninterruptibly() { |
| 47 | + long normalTimestamp = System.currentTimeMillis(); |
| 48 | + assertDoesNotThrow(() -> synchronizer.syncUninterruptibly(normalTimestamp)); |
| 49 | + } |
| 50 | + |
| 51 | + @SneakyThrows |
| 52 | + @Test |
| 53 | + void syncUninterruptiblyWhenInterrupted() { |
| 54 | + final long triggerTimestamp = System.currentTimeMillis() + DEFAULT_BROKEN_THRESHOLD; |
| 55 | + |
| 56 | + Thread testThread = new Thread(() -> { |
| 57 | + Thread.currentThread().interrupt(); |
| 58 | + synchronizer.syncUninterruptibly(triggerTimestamp); |
39 | 59 | }); |
| 60 | + |
| 61 | + testThread.start(); |
| 62 | + testThread.join(); |
| 63 | + assertTrue(testThread.isInterrupted()); |
| 64 | + |
| 65 | + // Cleanup |
| 66 | + Thread.interrupted(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * TC3: 验证时钟回拨过大异常透传 |
| 71 | + */ |
| 72 | + @Test |
| 73 | + void syncUninterruptiblyWhenExceedBrokenThreshold() { |
| 74 | + long exceedTimestamp = System.currentTimeMillis() + DEFAULT_BROKEN_THRESHOLD + 100; |
| 75 | + assertThrows(ClockTooManyBackwardsException.class, |
| 76 | + () -> synchronizer.syncUninterruptibly(exceedTimestamp)); |
40 | 77 | } |
41 | 78 | } |
0 commit comments