use of io.smallrye.mutiny.helpers.test.AssertSubscriber in project smallrye-mutiny by smallrye.
the class MultiDisjointTest method testWithPublishers.
@Test
public void testWithPublishers() {
AtomicBoolean subscribed = new AtomicBoolean();
AssertSubscriber<String> subscriber = Multi.createFrom().items(Flowable.just("a", "b", "c"), Flowable.just("d", "e"), Flowable.empty(), Flowable.just("f", "g").doOnSubscribe(s -> subscribed.set(true))).onItem().<String>disjoint().subscribe().withSubscriber(AssertSubscriber.create(4));
assertThat(subscribed).isFalse();
subscriber.assertItems("a", "b", "c", "d");
subscriber.request(3);
subscriber.assertCompleted();
assertThat(subscriber.getItems()).contains("e", "f", "g");
assertThat(subscribed).isTrue();
}
use of io.smallrye.mutiny.helpers.test.AssertSubscriber in project smallrye-mutiny by smallrye.
the class MultiReplayTest method raceBetweenPushAndCancel.
@Test
void raceBetweenPushAndCancel() throws InterruptedException, TimeoutException {
ExecutorService pool = Executors.newCachedThreadPool();
try {
final int N = 32;
CountDownLatch startLatch = new CountDownLatch(N);
CountDownLatch endLatch = new CountDownLatch(N);
Multi<Long> upstream = Multi.createFrom().<Long>emitter(emitter -> {
try {
startLatch.await();
} catch (InterruptedException e) {
emitter.fail(e);
}
long i = 0;
while (endLatch.getCount() != 0) {
emitter.emit(i++);
}
emitter.complete();
}).runSubscriptionOn(pool);
Multi<Long> replay = Multi.createBy().replaying().ofMulti(upstream).runSubscriptionOn(pool);
CopyOnWriteArrayList<List<Long>> items = new CopyOnWriteArrayList<>();
for (int i = 0; i < N; i++) {
AssertSubscriber<Long> sub = replay.subscribe().withSubscriber(AssertSubscriber.create());
pool.submit(() -> {
startLatch.countDown();
randomSleep();
sub.request(Long.MAX_VALUE);
randomSleep();
sub.cancel();
items.add(sub.getItems());
endLatch.countDown();
});
}
if (!endLatch.await(10, TimeUnit.SECONDS)) {
throw new TimeoutException("The test did not finish within 10 seconds");
}
assertThat(items).hasSize(N);
items.forEach(list -> {
if (list.isEmpty()) {
// Might happen due to subscriber timing
return;
}
assertThat(list).isNotEmpty();
AtomicLong prev = new AtomicLong(list.get(0));
list.stream().skip(1).forEach(n -> {
assertThat(n).isEqualTo(prev.get() + 1);
prev.set(n);
});
});
} finally {
pool.shutdownNow();
}
}
use of io.smallrye.mutiny.helpers.test.AssertSubscriber in project smallrye-mutiny by smallrye.
the class BugReproducersTest method reproducer_705.
@Test
void reproducer_705() {
// Adapted from https://github.com/smallrye/smallrye-mutiny/issues/705
// The issue was an over-interpretation of one of the RS TCK rule regarding releasing subscriber references.
AssertSubscriber<List<Integer>> sub = AssertSubscriber.create();
AtomicInteger counter = new AtomicInteger();
AtomicReference<Throwable> threadFailure = new AtomicReference<>();
ExecutorService threadPool = Executors.newFixedThreadPool(4, new ThreadFactory() {
@Override
public Thread newThread(Runnable task) {
Thread thread = Executors.defaultThreadFactory().newThread(task);
thread.setUncaughtExceptionHandler((t, e) -> {
e.printStackTrace();
threadFailure.set(e);
});
return thread;
}
});
Multi.createFrom().range(0, 1000).emitOn(threadPool).group().intoLists().of(100).onItem().invoke(() -> {
if (counter.incrementAndGet() == 3) {
sub.cancel();
}
}).runSubscriptionOn(threadPool).subscribe().withSubscriber(sub);
sub.request(Long.MAX_VALUE);
await().atMost(5, TimeUnit.SECONDS).untilAtomic(counter, greaterThanOrEqualTo(3));
assertThat(threadFailure.get()).isNull();
sub.assertNotTerminated();
threadPool.shutdownNow();
}
use of io.smallrye.mutiny.helpers.test.AssertSubscriber in project smallrye-mutiny by smallrye.
the class MultiSelectFirstOrLastTest method testSelectFirstWith0.
@Test
public void testSelectFirstWith0() {
AbstractMulti<Integer> upstream = new AbstractMulti<Integer>() {
@Override
public void subscribe(Subscriber<? super Integer> subscriber) {
Subscription subscription = mock(Subscription.class);
subscriber.onSubscribe(subscription);
subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onNext(3);
subscriber.onComplete();
}
};
AssertSubscriber<Integer> subscriber = upstream.select().first(0).subscribe().withSubscriber(AssertSubscriber.create(5));
subscriber.assertSubscribed().assertCompleted().assertHasNotReceivedAnyItem();
}
use of io.smallrye.mutiny.helpers.test.AssertSubscriber in project smallrye-mutiny by smallrye.
the class MultiFromResourceFromUniTest method testThatFinalizerThrowingExceptionAfterStreamFailure.
@Test
public void testThatFinalizerThrowingExceptionAfterStreamFailure() {
AssertSubscriber<Integer> subscriber = AssertSubscriber.create(20);
Consumer<Integer> fin = s -> {
throw new IllegalStateException("boom");
};
Multi.createFrom().resourceFromUni(() -> Uni.createFrom().item(1), r -> Multi.createFrom().<Integer>emitter(e -> e.emit(1).emit(2).fail(new IOException("no!")))).withFinalizer(fin).subscribe(subscriber);
subscriber.assertItems(1, 2).assertFailedWith(CompositeException.class, "boom").assertFailedWith(CompositeException.class, "no!");
}
Aggregations