Elffuss BraidHub

kikocis/java-driver public

Add test for clock resync via NTP (JAVA-467)
Changeset19ceb74cd6c4d9373b26fa957e4467b8e9ad5ec1475747f97c3b6d1f3cfcb687
AuthorPierre Laporte <pierre@pingtimeout.fr> · 2014-10-06 18:45 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-08 10:45 +02:00
Parents1eb65a0596f6

Browse the files at this changeset

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 @@
2929 // classloader leaks in managed environments like Tomcat
3030 private final ThreadLocal<Long> last = new ThreadLocal<Long>();
3131
32+ Clock clock = new SystemClock();
33+
3234 @Override
3335 public long next() {
3436 Long micros = this.last.get();
@@ -38,7 +40,7 @@
3840 long millis = micros / 1000;
3941 long counter = micros % 1000;
4042
41- long now = System.currentTimeMillis();
43+ long now = clock.currentTime();
4244 if (millis == now) {
4345 if (counter == 999)
4446 // 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 @@
6565 fail("Error in a test thread", cause);
6666 }
6767 }
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+ }
6892 }