kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 5f007c700854a8b6d90bf0e41c290af5e66a510ea5aa8ea649116f3a1ac9921e |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-03 00:29 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-03 00:49 +02:00 |
| Parents | bc214054949d |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/CountingTimestampGenerator.java +5 −7
| @@ -25,17 +25,15 @@ | ||
| 25 | 25 | public class CountingTimestampGenerator implements TimestampGenerator { |
| 26 | 26 | private static final Logger logger = LoggerFactory.getLogger(CountingTimestampGenerator.class); |
| 27 | 27 | |
| 28 | + // We're deliberately avoiding an anonymous subclass with initialValue(), because this can introduce | |
| 29 | + // classloader leaks in managed environments like Tomcat | |
| 28 | 30 | private final ThreadLocal<Long> last = new ThreadLocal<Long>(); |
| 29 | 31 | |
| 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 | - | |
| 36 | 32 | @Override |
| 37 | 33 | public long next() { |
| 38 | - long micros = this.last.get(); | |
| 34 | + Long micros = this.last.get(); | |
| 35 | + if (micros == null) | |
| 36 | + micros = 0L; | |
| 39 | 37 | |
| 40 | 38 | long millis = micros / 1000; |
| 41 | 39 | long counter = micros % 1000; |
modified driver-core/src/test/java/com/datastax/driver/core/CountingTimestampGeneratorTest.java +56 −9
| @@ -1,21 +1,68 @@ | ||
| 1 | 1 | package com.datastax.driver.core; |
| 2 | 2 | |
| 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; | |
| 4 | 7 | |
| 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; | |
| 5 | 14 | import static org.testng.Assert.assertTrue; |
| 15 | +import static org.testng.Assert.fail; | |
| 6 | 16 | |
| 7 | 17 | public class CountingTimestampGeneratorTest { |
| 18 | + private static final int THREAD_COUNT = 2; | |
| 19 | + | |
| 8 | 20 | @Test(groups = "unit") |
| 9 | 21 | 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 | + })); | |
| 17 | 52 | } |
| 53 | + executor.shutdown(); | |
| 18 | 54 | |
| 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 | + } | |
| 20 | 67 | } |
| 21 | 68 | } |