Skip to content

Commit

Permalink
[test] Add direct assertion method on RetainedDetector, polish few tests
Browse files Browse the repository at this point in the history
Tests that combined Awaitility, AssertJ and RetainedDetector now use the
direct assertion of the later.

SchedulersMetricsTest#shouldReportExecutionTimes now schedules with
larger delays, so that the jitter is thinner.
  • Loading branch information
simonbasle committed Sep 20, 2019
1 parent 07e0be7 commit 87d636d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ public void untilChangedDisposesStateOnComplete() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount(),
is(100L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -335,8 +334,7 @@ public void untilChangedDisposesStateOnError() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount(),
is(100L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -359,10 +357,7 @@ public void untilChangedDisposesStateOnCancel() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount(),
// 50 + 1 (taking 50 buffers requires having 51st element as a
// key in the predicate)
is(51L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public void doesntRetainObjectsWithForcedCompleteOnSubscriber() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -719,7 +719,7 @@ public void doesntRetainObjectsWithForcedCompleteOnSubscriber_conditional() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand Down Expand Up @@ -748,7 +748,7 @@ protected void hookOnError(Throwable throwable) { }

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand Down Expand Up @@ -777,7 +777,7 @@ protected void hookOnError(Throwable throwable) { }

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -803,7 +803,7 @@ public void doesntRetainObjectsWithForcedCancelOnSubscriber() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -829,7 +829,7 @@ public void doesntRetainObjectsWithForcedCancelOnSubscriber_conditional() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isEqualTo(3));
.untilAsserted(retainedDetector::assertAllFinalized);
}

static class DistinctDefault {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public void doesntRetainObjectsWithForcedCompleteOnSubscriber() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -398,7 +398,7 @@ public void doesntRetainObjectsWithForcedCompleteOnSubscriber_conditional() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -422,7 +422,7 @@ protected void hookOnError(Throwable throwable) { }

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -446,7 +446,7 @@ protected void hookOnError(Throwable throwable) { }

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -467,7 +467,7 @@ public void doesntRetainObjectsWithForcedCancelOnSubscriber() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -488,7 +488,7 @@ public void doesntRetainObjectsWithForcedCancelOnSubscriber_conditional() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> assertThat(retainedDetector.finalizedCount()).isOne());
.untilAsserted(retainedDetector::assertAllFinalized);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ public void untilChangedDisposesStateOnComplete() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> Assert.assertThat(retainedDetector.finalizedCount(),
is(100L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -291,8 +290,7 @@ public void untilChangedDisposesStateOnError() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> Assert.assertThat(retainedDetector.finalizedCount(),
is(100L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Test
Expand All @@ -315,10 +313,7 @@ public void untilChangedDisposesStateOnCancel() {

await()
.atMost(2, TimeUnit.SECONDS)
.untilAsserted(() -> Assert.assertThat(retainedDetector.finalizedCount(),
// 50 + 1 (taking 50 windows requires having 51st element as a
// key in the predicate)
is(51L)));
.untilAsserted(retainedDetector::assertAllFinalized);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void shouldReportExecutionTimes(Supplier<Scheduler> schedulerSupplier, St
Phaser phaser = new Phaser(1);
for (int i = 1; i <= taskCount; i++) {
phaser.register();
int delay = i * 20;
int delay = i * 200; //bumped delay from 20ms to make actual scheduling times more precise
scheduler.schedule(() -> {
try {
Thread.sleep(delay);
Expand All @@ -250,7 +250,7 @@ public void shouldReportExecutionTimes(Supplier<Scheduler> schedulerSupplier, St
assertThat(timers.stream()
.reduce(0d, (time, timer) -> time + timer.totalTime(TimeUnit.MILLISECONDS), Double::sum))
.as("total durations")
.isEqualTo(60 + 40 + 20, offset(10.0d));
.isEqualTo(600 + 400 + 200, offset(20.0d));
assertThat(timers.stream().mapToLong(Timer::count).sum())
.as("count")
.isEqualTo(taskCount);
Expand Down
18 changes: 17 additions & 1 deletion reactor-core/src/test/java/reactor/test/MemoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.lang.ref.ReferenceQueue;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.assertj.core.api.Assertions;

/**
* Test utility around memory, references, leaks and retained object detection.
Expand All @@ -37,6 +40,7 @@ public static final class RetainedDetector {
private final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
private final List<PhantomReference<Object>> phantomReferences = new LinkedList<>();
private long finalizedSoFar = 0L;
private AtomicInteger trackedTotal = new AtomicInteger(0);

/**
* Track the object in this {@link RetainedDetector}'s {@link ReferenceQueue}
Expand All @@ -47,6 +51,7 @@ public static final class RetainedDetector {
*/
public final <T> T tracked(T object) {
phantomReferences.add(new PhantomReference<>(object, referenceQueue));
trackedTotal.incrementAndGet();
return object;
}

Expand All @@ -63,8 +68,19 @@ public final synchronized long finalizedCount() {
return finalizedSoFar;
}

/**
* @return the total number of objects that have been added to this {@link RetainedDetector}
*/
public final long trackedTotal() {
return phantomReferences.size();
return trackedTotal.get();
}

/**
* Assert that all tracked elements have been finalized.
* @throws AssertionError if some tracked elements have not been finalized
*/
public final void assertAllFinalized() {
Assertions.assertThat(this.finalizedCount()).as("all tracked finalized").isEqualTo(trackedTotal.get());
}
}

Expand Down

0 comments on commit 87d636d

Please sign in to comment.