Search in sources :

Example 1 with UniEmitter

use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.

the class UniContextPropagationTest method testOnTermination.

@Test
public void testOnTermination() throws InterruptedException {
    MyContext ctx = MyContext.get();
    AtomicInteger count = new AtomicInteger();
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    Uni<Integer> uni = Uni.createFrom().emitter((Consumer<UniEmitter<? super Integer>>) reference::set).onTermination().invoke((value, failure, cancellation) -> {
        count.incrementAndGet();
        assertThat(ctx).isSameAs(MyContext.get());
    });
    // Test completion
    CountDownLatch latch1 = new CountDownLatch(1);
    uni.subscribe().with(x -> {
        latch1.countDown();
    });
    new Thread(() -> {
        reference.get().complete(23);
    }).start();
    latch1.await(10, TimeUnit.MILLISECONDS);
    assertThat(count).hasValue(1);
    // Test failure
    CountDownLatch latch2 = new CountDownLatch(1);
    uni.subscribe().with(x -> {
    }, fail -> {
        latch2.countDown();
    });
    new Thread(() -> {
        reference.get().fail(new Exception("boom"));
    }).start();
    latch2.await(10, TimeUnit.MILLISECONDS);
    assertThat(count).hasValue(2);
    // Test cancellation
    Cancellable cancellable = uni.subscribe().with(x -> {
    }, fail -> {
    });
    new Thread(cancellable::cancel).start();
    await().until(() -> count.get() == 3);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Cancellable(io.smallrye.mutiny.subscription.Cancellable) AtomicReference(java.util.concurrent.atomic.AtomicReference) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter)

Example 2 with UniEmitter

use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.

the class UniSerializedSubscriberTest method testRaceBetweenItemAndCancellationWithUniserializedSubscriber.

@RepeatedTest(100)
public void testRaceBetweenItemAndCancellationWithUniserializedSubscriber() {
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    AtomicBoolean cancelled = new AtomicBoolean();
    Uni<Integer> uni = Uni.createFrom().<Integer>emitter(reference::set).onCancellation().invoke(() -> cancelled.set(true));
    UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
    uni.subscribe().withSerializedSubscriber(subscriber);
    subscriber.assertSubscribed();
    CountDownLatch start = new CountDownLatch(2);
    CountDownLatch done = new CountDownLatch(2);
    Runnable runnable1 = () -> {
        try {
            start.countDown();
            await(start);
            reference.get().complete(1);
            done.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    };
    Runnable runnable2 = () -> {
        try {
            start.countDown();
            await(start);
            subscriber.cancel();
            done.countDown();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    };
    List<Runnable> runnables = Arrays.asList(runnable1, runnable2);
    Collections.shuffle(runnables);
    runnables.forEach(r -> new Thread(r).start());
    await(done);
    if (subscriber.getItem() != null) {
        assertThat(cancelled).isFalse();
        subscriber.assertCompleted().assertItem(1);
    } else {
        subscriber.assertNotTerminated();
    }
    subscriber.assertSignalsReceivedInOrder();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 3 with UniEmitter

use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.

the class UniSerializedSubscriberTest method testNormal.

@Test
public void testNormal() {
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    AbstractUni<? super Integer> uni = (AbstractUni<? super Integer>) Uni.createFrom().<Integer>emitter(reference::set);
    UniAssertSubscriber<Object> subscriber = UniAssertSubscriber.create();
    UniSerializedSubscriber.subscribe(uni, subscriber);
    subscriber.assertSubscribed();
    reference.get().complete(1);
    reference.get().complete(2);
    reference.get().fail(new IOException("boom"));
    subscriber.assertCompleted().assertItem(1).assertSignalsReceivedInOrder();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 4 with UniEmitter

use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.

the class UniSerializedSubscriberTest method testNormalWithNullItem.

@Test
public void testNormalWithNullItem() {
    AtomicReference<UniEmitter<? super String>> reference = new AtomicReference<>();
    AbstractUni<? super String> uni = (AbstractUni<? super String>) Uni.createFrom().<String>emitter(reference::set);
    UniAssertSubscriber<Object> subscriber = UniAssertSubscriber.create();
    UniSerializedSubscriber.subscribe(uni, subscriber);
    subscriber.assertSubscribed();
    reference.get().complete(null);
    reference.get().complete("hello");
    reference.get().fail(new IOException("boom"));
    subscriber.assertCompleted().assertItem(null).assertSignalsReceivedInOrder();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 5 with UniEmitter

use of io.smallrye.mutiny.subscription.UniEmitter in project smallrye-mutiny by smallrye.

the class UniSerializedSubscriberTest method testRaceBetweenItemAndFailureWithoutUniserializedSubscriber.

@RepeatedTest(100)
public void testRaceBetweenItemAndFailureWithoutUniserializedSubscriber() {
    AtomicReference<UniEmitter<? super Integer>> reference = new AtomicReference<>();
    Uni<Integer> uni = Uni.createFrom().<Integer>emitter(reference::set);
    UniAssertSubscriber<Integer> subscriber = UniAssertSubscriber.create();
    uni.subscribe().withSubscriber(subscriber);
    subscriber.assertSubscribed();
    CountDownLatch start = new CountDownLatch(2);
    Runnable runnable1 = () -> {
        start.countDown();
        await(start);
        reference.get().complete(1);
    };
    Runnable runnable2 = () -> {
        start.countDown();
        await(start);
        reference.get().fail(new IOException("boom"));
    };
    List<Runnable> runnables = Arrays.asList(runnable1, runnable2);
    Collections.shuffle(runnables);
    runnables.forEach(r -> new Thread(r).start());
    Awaitility.await().untilAsserted(subscriber::assertTerminated);
    if (subscriber.getFailure() != null) {
        subscriber.assertFailed().assertFailedWith(IOException.class, "boom");
    } else {
        subscriber.assertCompleted().assertItem(1);
    }
    subscriber.assertSignalsReceivedInOrder();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) UniEmitter(io.smallrye.mutiny.subscription.UniEmitter) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Aggregations

UniEmitter (io.smallrye.mutiny.subscription.UniEmitter)28 AtomicReference (java.util.concurrent.atomic.AtomicReference)28 Test (org.junit.jupiter.api.Test)20 RepeatedTest (org.junit.jupiter.api.RepeatedTest)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)14 IOException (java.io.IOException)12 DefaultUniEmitter (io.smallrye.mutiny.operators.uni.builders.DefaultUniEmitter)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Context (io.smallrye.mutiny.Context)2 UniAssertSubscriber (io.smallrye.mutiny.helpers.test.UniAssertSubscriber)2 UniSubscription (io.smallrye.mutiny.subscription.UniSubscription)2 Cancellable (io.smallrye.mutiny.subscription.Cancellable)1 ArrayList (java.util.ArrayList)1