Search in sources :

Example 1 with AssertSubscriber

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();
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) Uni(io.smallrye.mutiny.Uni) Test(org.junit.jupiter.api.Test) Flowable(io.reactivex.Flowable) Duration(java.time.Duration) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) Collections(java.util.Collections) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.jupiter.api.Test)

Example 2 with AssertSubscriber

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();
    }
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Awaitility.await(org.awaitility.Awaitility.await) Arrays(java.util.Arrays) java.util.concurrent(java.util.concurrent) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) Random(java.util.Random) Context(io.smallrye.mutiny.Context) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Subscription(org.reactivestreams.Subscription) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with AssertSubscriber

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();
}
Also used : AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Awaitility.await(org.awaitility.Awaitility.await) Matchers.greaterThanOrEqualTo(org.hamcrest.Matchers.greaterThanOrEqualTo) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) InfrastructureResource(junit5.support.InfrastructureResource) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadFactory(java.util.concurrent.ThreadFactory) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) ExecutorService(java.util.concurrent.ExecutorService) ThreadFactory(java.util.concurrent.ThreadFactory) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with AssertSubscriber

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Subscriber(org.reactivestreams.Subscriber) MultiSubscriber(io.smallrye.mutiny.subscription.MultiSubscriber) Subscription(org.reactivestreams.Subscription) Test(org.junit.jupiter.api.Test)

Example 5 with AssertSubscriber

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!");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) AssertSubscriber(io.smallrye.mutiny.helpers.test.AssertSubscriber) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BiFunction(java.util.function.BiFunction) Publisher(org.reactivestreams.Publisher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UniOnCancellationSpy(io.smallrye.mutiny.helpers.spies.UniOnCancellationSpy) IOException(java.io.IOException) ResourceLock(org.junit.jupiter.api.parallel.ResourceLock) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) InfrastructureResource(junit5.support.InfrastructureResource) Multi(io.smallrye.mutiny.Multi) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) CompositeException(io.smallrye.mutiny.CompositeException) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Spy(io.smallrye.mutiny.helpers.spies.Spy) ResourceAccessMode(org.junit.jupiter.api.parallel.ResourceAccessMode) CompositeException(io.smallrye.mutiny.CompositeException) IOException(java.io.IOException) Test(org.junit.jupiter.api.Test)

Aggregations

AssertSubscriber (io.smallrye.mutiny.helpers.test.AssertSubscriber)80 Test (org.junit.jupiter.api.Test)75 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)60 Multi (io.smallrye.mutiny.Multi)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)55 IOException (java.io.IOException)51 Uni (io.smallrye.mutiny.Uni)44 InfrastructureResource (junit5.support.InfrastructureResource)34 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)34 ResourceAccessMode (org.junit.jupiter.api.parallel.ResourceAccessMode)34 ResourceLock (org.junit.jupiter.api.parallel.ResourceLock)34 Duration (java.time.Duration)32 List (java.util.List)32 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 CompositeException (io.smallrye.mutiny.CompositeException)28 RepeatedTest (org.junit.jupiter.api.RepeatedTest)28 Subscription (org.reactivestreams.Subscription)27 Supplier (java.util.function.Supplier)25 Consumer (java.util.function.Consumer)23