Skip to content

Commit

Permalink
Merge pull request #1356 from siewp/rename-UniMemoize-atLeast
Browse files Browse the repository at this point in the history
Rename UniMemoize#atLeast to forFixedDuration
  • Loading branch information
jponge authored Sep 4, 2023
2 parents 58a8ae0 + 72d4d39 commit 54e0100
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,28 @@ public Uni<T> until(BooleanSupplier invalidationGuard) {
* {@code null}, must be strictly positive
* @return a new {@link Uni}
* @apiNote This is an experimental API
* @deprecated use {@link #forFixedDuration(Duration)} instead.
*/
@Deprecated
@CheckReturnValue
public Uni<T> atLeast(Duration duration) {
return forFixedDuration(duration);
}

/**
* Memoize the received item or failure for a duration after the upstream subscription has been received.
* <p>
* New subscribers will receive the memoized item or failure.
* When duration has elapsed then the first subscription causes a new upstream subscription, and the next
* subscribers get a chance to observe new values.
*
* @param duration the memoization duration after having received the subscription from upstream, must not be
* {@code null}, must be strictly positive
* @return a new {@link Uni}
* @apiNote This is an experimental API
*/
@CheckReturnValue
public Uni<T> forFixedDuration(Duration duration) {
Duration validatedDuration = validate(duration, "duration");
return until(new BooleanSupplier() {
private volatile long startTime = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,22 @@ void testUntilNull() {
}

@Test
@DisplayName("memoize().atLeast(null) is forbidden")
void testAtLeastNull() {
assertThrows(IllegalArgumentException.class, () -> Uni.createFrom().item(1).memoize().atLeast(null));
@DisplayName("memoize().forFixedDuration(null) is forbidden")
void testForFixedDurationNull() {
assertThrows(IllegalArgumentException.class, () -> Uni.createFrom().item(1).memoize().forFixedDuration(null));
}

@Test
@DisplayName("memoize().atLeast(0) is forbidden")
void testAtLeastZero() {
assertThrows(IllegalArgumentException.class, () -> Uni.createFrom().item(1).memoize().atLeast(Duration.ZERO));
@DisplayName("memoize().forFixedDuration(0) is forbidden")
void testForFixedDurationZero() {
assertThrows(IllegalArgumentException.class, () -> Uni.createFrom().item(1).memoize().forFixedDuration(Duration.ZERO));
}

@Test
@DisplayName("memoize().atLeast(negative) is forbidden")
void testAtLeastNegative() {
@DisplayName("memoize().forFixedDuration(negative) is forbidden")
void testForFixedDurationNegative() {
assertThrows(IllegalArgumentException.class,
() -> Uni.createFrom().item(1).memoize().atLeast(Duration.ofMillis(-10)));
() -> Uni.createFrom().item(1).memoize().forFixedDuration(Duration.ofMillis(-10)));
}

@Test
Expand Down Expand Up @@ -421,7 +421,7 @@ void testBasicInvalidation() {
void testDurationInvalidation() throws InterruptedException {
AtomicInteger counter = new AtomicInteger(0);
UniOnSubscribeSpy<Integer> onSubscribeSpy = Spy.onSubscribe(Uni.createFrom().item(counter::getAndIncrement));
Uni<Integer> cachingUni = onSubscribeSpy.memoize().atLeast(Duration.ofMillis(250));
Uni<Integer> cachingUni = onSubscribeSpy.memoize().forFixedDuration(Duration.ofMillis(250));

UniAssertSubscriber<Integer> subscriber = cachingUni.subscribe().withSubscriber(UniAssertSubscriber.create());
subscriber.assertCompleted().assertItem(0);
Expand Down Expand Up @@ -511,7 +511,7 @@ void testMemoizeForever() {
return "hello-" + i;
})
.onItem().delayIt().by(Duration.ofMillis(500))
.memoize().atLeast(ChronoUnit.FOREVER.getDuration());
.memoize().forFixedDuration(ChronoUnit.FOREVER.getDuration());
UniAssertSubscriber<String> subscriber1 = uni.subscribe().withSubscriber(UniAssertSubscriber.create());
UniAssertSubscriber<String> subscriber2 = uni.subscribe().withSubscriber(UniAssertSubscriber.create());

Expand Down

0 comments on commit 54e0100

Please sign in to comment.