Elffuss BraidHub

kikocis/java-driver public

Fix stupid bug in timestamp generator.
Changeset5f007c700854a8b6d90bf0e41c290af5e66a510ea5aa8ea649116f3a1ac9921e
Authorolim7t <omichallat+github@gmail.com> · 2014-10-03 00:29 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-03 00:49 +02:00
Parentsbc214054949d

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/CountingTimestampGenerator.java +5 −7
@@ -25,17 +25,15 @@
2525 public class CountingTimestampGenerator implements TimestampGenerator {
2626 private static final Logger logger = LoggerFactory.getLogger(CountingTimestampGenerator.class);
2727
28+ // We're deliberately avoiding an anonymous subclass with initialValue(), because this can introduce
29+ // classloader leaks in managed environments like Tomcat
2830 private final ThreadLocal<Long> last = new ThreadLocal<Long>();
2931
30- public CountingTimestampGenerator() {
31- // We're deliberately avoiding an anonymous subclass with initialValue(), because this can introduce
32- // classloader leaks in managed environments like Tomcat
33- last.set(0L);
34- }
35-
3632 @Override
3733 public long next() {
38- long micros = this.last.get();
34+ Long micros = this.last.get();
35+ if (micros == null)
36+ micros = 0L;
3937
4038 long millis = micros / 1000;
4139 long counter = micros % 1000;
modified driver-core/src/test/java/com/datastax/driver/core/CountingTimestampGeneratorTest.java +56 −9
@@ -1,21 +1,68 @@
11 package com.datastax.driver.core;
22
3-import org.testng.annotations.Test;
3+import java.util.List;
4+import java.util.concurrent.CountDownLatch;
5+import java.util.concurrent.ExecutionException;
6+import java.util.concurrent.Executors;
47
8+import com.google.common.collect.Lists;
9+import com.google.common.util.concurrent.Futures;
10+import com.google.common.util.concurrent.ListenableFuture;
11+import com.google.common.util.concurrent.ListeningExecutorService;
12+import com.google.common.util.concurrent.MoreExecutors;
13+import org.testng.annotations.Test;
514 import static org.testng.Assert.assertTrue;
15+import static org.testng.Assert.fail;
616
717 public class CountingTimestampGeneratorTest {
18+ private static final int THREAD_COUNT = 2;
19+
820 @Test(groups = "unit")
921 public void should_generate_incrementing_timestamps() {
10- CountingTimestampGenerator generator = new CountingTimestampGenerator();
11- long previous = (System.currentTimeMillis() - 1) * 1000;
12-
13- for (int i = 0; i < 1000; i++) {
14- long timestamp = generator.next();
15- assertTrue(timestamp > previous);
16- previous = timestamp;
22+ final CountingTimestampGenerator generator = new CountingTimestampGenerator();
23+
24+ ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(THREAD_COUNT));
25+ final CountDownLatch latch = new CountDownLatch(1);
26+
27+ List<ListenableFuture<?>> futures = Lists.newArrayListWithExpectedSize(THREAD_COUNT);
28+ for (int i = 0; i < THREAD_COUNT; i++) {
29+ futures.add(executor.submit(
30+ new Runnable() {
31+ @Override
32+ public void run() {
33+ try {
34+ latch.await();
35+ } catch (InterruptedException e) {
36+ fail("Interrupted while waiting for test start");
37+ }
38+
39+ long previous = (System.currentTimeMillis() - 1) * 1000;
40+
41+ for (int i = 0; i < 10000; i++) {
42+ long timestamp = generator.next();
43+ assertTrue(timestamp > previous,
44+ String.format("iteration %d, timestamp=%d, previous=%d",
45+ i, timestamp, previous));
46+ previous = timestamp;
47+ }
48+
49+ assertTrue((System.currentTimeMillis() + 1) * 1000 > previous);
50+ }
51+ }));
1752 }
53+ executor.shutdown();
1854
19- assertTrue((System.currentTimeMillis() + 1) * 1000 > previous);
55+ latch.countDown();
56+ try {
57+ Futures.allAsList(futures).get();
58+ } catch (InterruptedException e) {
59+ fail("Interrupted while getting future");
60+ } catch (ExecutionException e) {
61+ Throwable cause = e.getCause();
62+ if (cause instanceof AssertionError)
63+ throw (AssertionError)cause;
64+ else
65+ fail("Error in a test thread", cause);
66+ }
2067 }
2168 }