kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 19ceb74cd6c4d9373b26fa957e4467b8e9ad5ec1475747f97c3b6d1f3cfcb687 |
|---|---|
| Author | Pierre Laporte <pierre@pingtimeout.fr> · 2014-10-06 18:45 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-08 10:45 +02:00 |
| Parents | 1eb65a0596f6 |
3 files changed
added driver-core/src/main/java/com/datastax/driver/core/Clock.java +24 −0
| @@ -0,0 +1,24 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * This interface allows us not to have a direct call to {@code System.currentTimeMillis()} for testing purposes | |
| 5 | + */ | |
| 6 | +interface Clock { | |
| 7 | + /** | |
| 8 | + * Returns the current time in milliseconds | |
| 9 | + * | |
| 10 | + * @return the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. | |
| 11 | + * @see System#currentTimeMillis() | |
| 12 | + */ | |
| 13 | + long currentTime(); | |
| 14 | +} | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Default implementation of a clock that delegate its calls to the system clock. | |
| 18 | + */ | |
| 19 | +class SystemClock implements Clock { | |
| 20 | + @Override | |
| 21 | + public long currentTime() { | |
| 22 | + return System.currentTimeMillis(); | |
| 23 | + } | |
| 24 | +} | |
| No newline at the end of the file |
modified driver-core/src/main/java/com/datastax/driver/core/CountingTimestampGenerator.java +3 −1
| @@ -29,6 +29,8 @@ | ||
| 29 | 29 | // classloader leaks in managed environments like Tomcat |
| 30 | 30 | private final ThreadLocal<Long> last = new ThreadLocal<Long>(); |
| 31 | 31 | |
| 32 | + Clock clock = new SystemClock(); | |
| 33 | + | |
| 32 | 34 | @Override |
| 33 | 35 | public long next() { |
| 34 | 36 | Long micros = this.last.get(); |
| @@ -38,7 +40,7 @@ | ||
| 38 | 40 | long millis = micros / 1000; |
| 39 | 41 | long counter = micros % 1000; |
| 40 | 42 | |
| 41 | - long now = System.currentTimeMillis(); | |
| 43 | + long now = clock.currentTime(); | |
| 42 | 44 | if (millis == now) { |
| 43 | 45 | if (counter == 999) |
| 44 | 46 | // This should not happen: even if System.currentTimeMillis() does not always have millisecond |
modified driver-core/src/test/java/com/datastax/driver/core/CountingTimestampGeneratorTest.java +24 −0
| @@ -65,4 +65,28 @@ | ||
| 65 | 65 | fail("Error in a test thread", cause); |
| 66 | 66 | } |
| 67 | 67 | } |
| 68 | + | |
| 69 | + @Test(groups = "unit") | |
| 70 | + public void should_generate_incrementing_timestamps_on_clock_resync() { | |
| 71 | + CountingTimestampGenerator generator = new CountingTimestampGenerator(); | |
| 72 | + generator.clock = new BackInTimeClock(); | |
| 73 | + | |
| 74 | + long beforeClockResync = generator.next(); | |
| 75 | + long afterClockResync = generator.next(); | |
| 76 | + | |
| 77 | + System.out.println(beforeClockResync); | |
| 78 | + System.out.println(afterClockResync); | |
| 79 | + | |
| 80 | + assertTrue(beforeClockResync < afterClockResync, "The generated timestamps are not increasing on block resync"); | |
| 81 | + } | |
| 82 | + | |
| 83 | + static class BackInTimeClock implements Clock { | |
| 84 | + final long arbitraryTimeStamp = 1412610226270L; | |
| 85 | + int calls; | |
| 86 | + | |
| 87 | + @Override | |
| 88 | + public long currentTime() { | |
| 89 | + return arbitraryTimeStamp - calls++; | |
| 90 | + } | |
| 91 | + } | |
| 68 | 92 | } |