Skip to content

Commit

Permalink
Check more timestamps and they are increasing #646
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lawrey committed Apr 10, 2024
1 parent d8dda53 commit c1a4cbc
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,28 @@ public void shouldProvideUniqueTimeAcrossThreads() throws InterruptedException {
final int numberOfThreads = 50;
final int factor = 50;
final int iterationsPerThread = 500;
ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
CountDownLatch latch = new CountDownLatch(numberOfThreads * factor);
final ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads);
final CountDownLatch latch = new CountDownLatch(numberOfThreads * factor);

for (int i = 0; i < numberOfThreads * factor; i++) {
executor.execute(() -> {
List<Long> threadTimeSet = new ArrayList<>(iterationsPerThread);
long lastTimestamp = 0;
for (int j = 0; j < iterationsPerThread; j++) {
setTimeProvider.advanceNanos(j);
long currentTimeMicros = timeProvider.currentTimeMicros();
threadTimeSet.add(currentTimeMicros);
assertTrue("Timestamps should always increase", currentTimeMicros > lastTimestamp);
lastTimestamp = currentTimeMicros;
try {
List<Long> threadTimeSet = new ArrayList<>(iterationsPerThread);
long lastTimestamp = 0;
for (int j = 0; j < iterationsPerThread; j++) {

// there could be a race condition for the next two methods, but it shouldn't matter for this test
setTimeProvider.advanceNanos(j);
long currentTimeMicros = timeProvider.currentTimeMicros();

threadTimeSet.add(currentTimeMicros);
assertTrue("Timestamps should always increase", currentTimeMicros > lastTimestamp);
lastTimestamp = currentTimeMicros;
}
allGeneratedTimestamps.addAll(threadTimeSet);
} finally {
latch.countDown();
}
allGeneratedTimestamps.addAll(threadTimeSet);
latch.countDown();
});
}

Expand Down

0 comments on commit c1a4cbc

Please sign in to comment.