diff --git a/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java b/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java index 9bbb2ed51..ac698bba0 100644 --- a/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java +++ b/src/test/java/org/apache/commons/collections4/bloomfilter/LayeredBloomFilterTest.java @@ -21,11 +21,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.time.Duration; +import java.time.Instant; import java.util.ArrayList; import java.util.Deque; import java.util.Iterator; import java.util.List; -import java.util.concurrent.TimeUnit; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.function.Supplier; @@ -40,16 +41,16 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest> { - long quanta; + Duration quanta; - AdvanceOnTimeQuanta(final long quanta, final TimeUnit unit) { - this.quanta = unit.toMillis(quanta); + AdvanceOnTimeQuanta(final Duration quanta) { + this.quanta = quanta; } @Override - public boolean test(final LayerManager lm) { + public boolean test(final LayerManager layerManager) { // can not use getTarget() as it causes recursion. - return lm.last().timestamp + quanta < System.currentTimeMillis(); + return layerManager.last().getTimestamp().plus(quanta).isBefore(Instant.now()); } } @@ -58,23 +59,22 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest implements Consumer> { - long elapsedTime; + Duration elapsedTime; - CleanByTime(final long duration, final TimeUnit unit) { - elapsedTime = unit.toMillis(duration); + CleanByTime(final Duration elapsedTime) { + this.elapsedTime = elapsedTime; } @Override public void accept(final List t) { - final long min = System.currentTimeMillis() - elapsedTime; + final Instant min = Instant.now().minus(elapsedTime); final Iterator iter = t.iterator(); while (iter.hasNext()) { final TimestampedBloomFilter bf = iter.next(); - if (bf.getTimestamp() >= min) { + if (bf.getTimestamp().isAfter(min) || bf.getTimestamp().equals(min)) { return; } - dbgInstrument.add(String.format("Removing old entry: T:%s (Aged: %s) \n", bf.getTimestamp(), - min - bf.getTimestamp())); + dbgInstrument.add(String.format("Removing old entry: T:%s (Aged: %s) \n", bf.getTimestamp(), Duration.between(bf.getTimestamp(), min))); iter.remove(); } } @@ -96,17 +96,17 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest createTimedLayeredFilter(final Shape shape, final long duration, final TimeUnit dUnit, final long quanta, - final TimeUnit qUnit) { + static LayeredBloomFilter createTimedLayeredFilter(final Shape shape, final Duration duration, final Duration quanta) { final LayerManager.Builder builder = LayerManager.builder(); - final Consumer> cleanup = Cleanup.removeEmptyTarget().andThen(new CleanByTime(duration, dUnit)); + final Consumer> cleanup = Cleanup.removeEmptyTarget().andThen(new CleanByTime(duration)); final LayerManager layerManager = builder .setSupplier(() -> new TimestampedBloomFilter(new SimpleBloomFilter(shape))) .setCleanup(cleanup) - .setExtendCheck(new AdvanceOnTimeQuanta(quanta, qUnit) + .setExtendCheck(new AdvanceOnTimeQuanta(quanta) .or(LayerManager.ExtendCheck.advanceOnSaturation(shape.estimateMaxN()))) .build(); return new LayeredBloomFilter<>(shape, layerManager); @@ -183,8 +180,8 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest dbg = bf -> { final TimestampedBloomFilter tbf = (TimestampedBloomFilter) bf; - final long ts = System.currentTimeMillis(); - dbgInstrument.add(String.format("T:%s (Elapsed:%s)- EstN:%s (Card:%s)\n", tbf.timestamp, ts - tbf.timestamp, + final Instant ts = Instant.now(); + dbgInstrument.add(String.format("T:%s (Elapsed:%s)- EstN:%s (Card:%s)\n", tbf.timestamp, Duration.between(tbf.timestamp, ts), tbf.estimateN(), tbf.cardinality())); return true; }; @@ -280,13 +277,12 @@ public class LayeredBloomFilterTest extends AbstractBloomFilterTest lst = new ArrayList<>(); + final List lst = new ArrayList<>(); final Shape shape = Shape.fromNM(4, 64); // create a filter that removes filters that are 4 seconds old // and quantises time to 1 second intervals. - final LayeredBloomFilter underTest = createTimedLayeredFilter(shape, 600, TimeUnit.MILLISECONDS, 150, - TimeUnit.MILLISECONDS); + final LayeredBloomFilter underTest = createTimedLayeredFilter(shape, Duration.ofMillis(600), Duration.ofMillis(150)); for (int i = 0; i < 10; i++) { underTest.merge(TestingHashers.randomHasher());