Skip to content

Commit

Permalink
Update some tests to use the new test utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier committed Mar 5, 2021
1 parent 6c6a113 commit ab1acf8
Show file tree
Hide file tree
Showing 54 changed files with 484 additions and 403 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public void testOnSubscribe() {
})
.subscribe().withSubscriber(AssertSubscriber.create(20));

subscriber.await()
subscriber.awaitCompletion()
.assertItems(0L, 1L, 2L, 3L, 4L);

}
Expand All @@ -270,7 +270,7 @@ public void testSelectWhere() {
})
.subscribe().withSubscriber(AssertSubscriber.create(20));

subscriber.await()
subscriber.awaitCompletion()
.assertItems(0L, 2L, 4L);

}
Expand All @@ -289,8 +289,7 @@ public void testScan() {
.select().first(10)
.subscribe().withSubscriber(AssertSubscriber.create(10));

subscriber.await()
.assertCompleted()
subscriber.awaitCompletion()
.assertItems(0, 1, 3, 6, 10, 15, 21, 28, 36, 45);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public void test() {
// end::code[]
AssertSubscriber<String> subscriber = AssertSubscriber.create(Long.MAX_VALUE);
multi.subscribe().withSubscriber(subscriber)
.await()
.assertCompleted();
.awaitCompletion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void test() {
// end::code[]
AssertSubscriber<String> subscriber = AssertSubscriber.create(Long.MAX_VALUE);
multi.subscribe().withSubscriber(subscriber)
.await()
.awaitCompletion()
.run(() -> assertThat(subscriber.getItems()).hasSize(1000));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,13 @@ public Uni<T> call(Function<Throwable, Uni<?>> action) {
//noinspection unchecked
return (Uni<T>) uni
.onItem().failWith(ignored -> failure)
.onFailure().apply(subFailure -> new CompositeException(failure, subFailure));
.onFailure().transform(subFailure -> {
if (subFailure != failure) {
return new CompositeException(failure, subFailure);
} else {
return subFailure;
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ public boolean isCancelled() {
public String toString() {
return "MultiOnCancellationSpy{} " + super.toString();
}

public void assertCancelled() {
if (!isCancelled()) {
throw new AssertionError("Expected downstream cancellation, but it did not happen");
}
}

public void assertNotCancelled() {
if (isCancelled()) {
throw new AssertionError("Did not expect to receive a downstream cancellation");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ public void testRequests() {

subscriber2.request(1000);

subscriber1.await().assertCompleted();
subscriber1.awaitCompletion();
assertThat(subscriber1.getItems()).hasSize(1000);
subscriber2.await().assertCompleted();
subscriber2.awaitCompletion();
assertThat(subscriber2.getItems()).hasSize(1000);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.smallrye.mutiny.groups;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -159,16 +158,16 @@ public void testFlatMapRequestsWithEmissionOnExecutor() {
.subscribe().withSubscriber(AssertSubscriber.create(0));

subscriber
.assertSubscribed()
.awaitSubscription()
.assertHasNotReceivedAnyItem()
.request(1);

await().until(() -> subscriber.getItems().contains("A"));

subscriber.request(2);
await().until(() -> subscriber.getItems().contains("B") && subscriber.getItems().contains("C"));
subscriber.awaitNextItem()
.request(2)
.awaitNextItems(2)
.assertItems("A", "B", "C");

subscriber.request(100);
subscriber.await().assertCompleted();
subscriber.awaitCompletion();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ void testSubscribersRaceWithRandomInvalidations() {
public void testDrainBlockedByAwait() {
Uni<Integer> uni = Uni.createFrom().item(() -> 1)
.memoize().indefinitely();
uni
assertThat(uni
.onItem().transform(x -> uni.await().indefinitely())
.subscribe().withSubscriber(UniAssertSubscriber.create()).await();
.subscribe().withSubscriber(UniAssertSubscriber.create()).awaitItem().getItem()).isEqualTo(1);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import org.junit.jupiter.api.Test;

import io.smallrye.mutiny.TestException;
import io.smallrye.mutiny.TimeoutException;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.subscription.UniEmitter;

Expand All @@ -31,15 +34,16 @@ void testCompletion() {
.isInstanceOf(AssertionError.class);
}

@SuppressWarnings("deprecation")
@Test
void testAwait() {
UniAssertSubscriber<Integer> subscriber = Uni.createFrom().item(123)
.subscribe().withSubscriber(new UniAssertSubscriber<>());

assertThat(subscriber.awaitItem().getItem()).isEqualTo(123);
subscriber.await();
subscriber.assertCompleted();
subscriber.assertTerminated();
subscriber.assertItem(123);
}

@Test
Expand Down Expand Up @@ -197,8 +201,8 @@ public void testDoubleOnItem() {
UniAssertSubscriber<String> subscriber = UniAssertSubscriber.create();
subscriber.onSubscribe(() -> {
});
subscriber.onItem("Yo");
subscriber.onItem("Yo");
subscriber.onItem("Yo1");
subscriber.onItem("Yo2");

assertThatThrownBy(subscriber::assertSignalsReceivedInOrder)
.isInstanceOf(AssertionError.class)
Expand All @@ -210,8 +214,8 @@ public void testDoubleOnFailure() {
UniAssertSubscriber<String> subscriber = UniAssertSubscriber.create();
subscriber.onSubscribe(() -> {
});
subscriber.onFailure(new RuntimeException("Yo"));
subscriber.onFailure(new RuntimeException("Yo"));
subscriber.onFailure(new RuntimeException("Yo1"));
subscriber.onFailure(new RuntimeException("Yo2"));

assertThatThrownBy(subscriber::assertSignalsReceivedInOrder)
.isInstanceOf(AssertionError.class)
Expand All @@ -230,4 +234,113 @@ public void testBothOnFailureAndOnItem() {
.isInstanceOf(AssertionError.class)
.hasMessageContaining("There are both onItem and onFailure");
}

@Test
public void testAwaitSubscription() {
// already subscribed
UniAssertSubscriber<Integer> subscriber = Uni.createFrom().item(1)
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitSubscription()).isSameAs(subscriber);

// Delay
subscriber = Uni.createFrom().item(1)
.onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitSubscription()).isSameAs(subscriber);

subscriber = Uni.createFrom().item(1)
.onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitSubscription(Duration.ofSeconds(5))).isSameAs(subscriber);

// timeout
subscriber = Uni.createFrom().item(1)
.onSubscribe().call(x -> Uni.createFrom().item(x).onItem().delayIt().by(Duration.ofSeconds(10)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
UniAssertSubscriber<Integer> tmp = subscriber;
assertThatThrownBy(() -> tmp.awaitSubscription(Duration.ofMillis(100))).isInstanceOf(AssertionError.class)
.hasMessageContaining("subscription").hasMessageContaining("100 ms");
}

@Test
public void testAwaitItem() {
UniAssertSubscriber<Integer> subscriber = Uni.createFrom().item(1)
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitItem().getItem()).isEqualTo(1);

// Delay
subscriber = Uni.createFrom().item(1)
.onItem().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitItem().getItem()).isEqualTo(1);

subscriber = Uni.createFrom().item(1)
.onItem().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitItem(Duration.ofSeconds(5)).getItem()).isEqualTo(1);

// timeout
subscriber = Uni.createFrom().item(1)
.onItem().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(10)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
UniAssertSubscriber<Integer> tmp = subscriber;
assertThatThrownBy(() -> tmp.awaitItem(Duration.ofMillis(100))).isInstanceOf(AssertionError.class)
.hasMessageContaining("item").hasMessageContaining("100 ms");
}

@Test
public void testAwaitFailure() {
UniAssertSubscriber<Integer> subscriber = Uni.createFrom().<Integer> failure(new TestException())
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitFailure()).isSameAs(subscriber);

assertThat(
subscriber.awaitFailure(t -> assertThat(t).isInstanceOf(TestException.class))).isSameAs(subscriber);

UniAssertSubscriber<Integer> tmp = subscriber;
Consumer<Throwable> failedValidation = t -> assertThat(t).isInstanceOf(IOException.class);
Consumer<Throwable> passedValidation = t -> assertThat(t).isInstanceOf(TestException.class);

assertThatThrownBy(() -> tmp.awaitFailure(failedValidation))
.isInstanceOf(AssertionError.class).hasMessageContaining("validation");

// Delay
subscriber = Uni.createFrom().<Integer> failure(new TestException())
.onFailure().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitFailure()).isSameAs(subscriber);

subscriber = Uni.createFrom().<Integer> failure(new TestException())
.onFailure().call(() -> Uni.createFrom().item(0)
.onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitFailure(passedValidation)).isSameAs(subscriber);

subscriber = Uni.createFrom().<Integer> failure(new TestException())
.onFailure().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(1)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
assertThat(subscriber.awaitFailure(Duration.ofSeconds(5))).isSameAs(subscriber);

// timeout
subscriber = Uni.createFrom().<Integer> failure(new TestException())
.onFailure().call(() -> Uni.createFrom().voidItem()
.onItem().delayIt().by(Duration.ofSeconds(10)))
.subscribe().withSubscriber(UniAssertSubscriber.create());
UniAssertSubscriber<Integer> tmp2 = subscriber;
assertThatThrownBy(() -> tmp2.awaitFailure(Duration.ofMillis(100))).isInstanceOf(AssertionError.class)
.hasMessageContaining("failure").hasMessageContaining("100 ms");
}

@Test
public void testFailureWithNoMessage() {
Uni.createFrom().failure(new TimeoutException())
.subscribe().withSubscriber(UniAssertSubscriber.create())
.awaitFailure()
.assertFailedWith(TimeoutException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public void testCollectFirstAndLast() {
items
.collect().first()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(1);

items
.collect().last()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(3);
}

Expand All @@ -56,13 +56,13 @@ public void testCollectFirstAndLastDeprecated() {
items
.collectItems().first()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(1);

items
.collectItems().last()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(3);
}

Expand All @@ -72,13 +72,13 @@ public void testCollectWithEmpty() {
items
.collect().first()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(null);

items
.collect().last()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitItem()
.assertItem(null);
}

Expand All @@ -88,13 +88,13 @@ public void testCollectFirstAndLastOnFailure() {
failing
.collect().first()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitFailure()
.assertFailedWith(IOException.class, "boom");

failing
.collect().last()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await()
.awaitFailure()
.assertFailedWith(IOException.class, "boom");
}

Expand All @@ -103,7 +103,7 @@ public void testAsList() {
UniAssertSubscriber<List<Integer>> subscriber = Multi.createFrom().items(1, 2, 3)
.collect().asList()
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await();
.awaitItem();

assertThat(subscriber.getItem()).containsExactly(1, 2, 3);
}
Expand All @@ -113,7 +113,7 @@ public void testCollectIn() {
UniAssertSubscriber<LinkedList<Integer>> subscriber = Multi.createFrom().range(1, 10)
.collect().in(LinkedList<Integer>::new, LinkedList::add)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.await();
.awaitItem();

assertThat(subscriber.getItem()).containsExactly(1, 2, 3, 4, 5, 6, 7, 8, 9).isInstanceOf(LinkedList.class);
}
Expand Down
Loading

0 comments on commit ab1acf8

Please sign in to comment.