Java Code Examples for io.smallrye.mutiny.Multi#subscribe()
The following examples show how to use
io.smallrye.mutiny.Multi#subscribe() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MultiFlatMapOnFailure.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Override public void subscribe(Subscriber<? super T> subscriber) { if (subscriber == null) { throw new NullPointerException("The subscriber must not be `null`"); } Function<? super Throwable, ? extends Publisher<? extends T>> next = failure -> { if (predicate.test(failure)) { Publisher<? extends T> res = mapper.apply(failure); if (res == null) { return Multi.createFrom().failure(new NullPointerException(MAPPER_RETURNED_NULL)); } else { return res; } } return Multi.createFrom().failure(failure); }; Multi<T> op = Infrastructure.onMultiCreation(new MultiOnFailureResumeOp<>(upstream(), next)); op.subscribe(subscriber); }
Example 2
Source File: MultiMapOnFailure.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Override public void subscribe(Subscriber<? super T> subscriber) { if (subscriber == null) { throw new NullPointerException("The subscriber must not be `null`"); } Function<? super Throwable, ? extends Publisher<? extends T>> next = failure -> { if (predicate.test(failure)) { Throwable throwable = mapper.apply(failure); if (throwable == null) { return Multi.createFrom().failure(new NullPointerException(MAPPER_RETURNED_NULL)); } else { return Multi.createFrom().failure(throwable); } } return Multi.createFrom().failure(failure); }; Multi<T> op = Infrastructure.onMultiCreation(new MultiOnFailureResumeOp<>(upstream(), next)); op.subscribe(subscriber); }
Example 3
Source File: MultiBroadcastTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testPublishAtLeast() { MultiAssertSubscriber<Integer> s1 = MultiAssertSubscriber.create(10); MultiAssertSubscriber<Integer> s2 = MultiAssertSubscriber.create(10); Multi<Integer> multi = Multi.createFrom().range(1, 5).broadcast().toAtLeast(2); multi.subscribe(s1); s1.assertNotTerminated() .assertHasNotReceivedAnyItem(); multi.subscribe(s2); s1.assertCompletedSuccessfully() .assertReceived(1, 2, 3, 4); s2.assertCompletedSuccessfully() .assertReceived(1, 2, 3, 4); }
Example 4
Source File: MultiOnOverflowTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testDropStrategyWithEmitter() { MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create(); AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>(); List<Integer> list = new CopyOnWriteArrayList<>(); Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set) .onOverflow().drop(list::add); multi.subscribe(sub); emitter.get().emit(1); sub.request(2); emitter.get().emit(2).emit(3).emit(4); sub.request(1); emitter.get().emit(5).complete(); sub .assertCompletedSuccessfully() .assertReceived(2, 3, 5); assertThat(list).containsExactly(1, 4); }
Example 5
Source File: MultiOnOverflowTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testDropStrategyWithEmitterWithoutCallback() { MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create(); AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>(); Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set) .onOverflow().drop(); multi.subscribe(sub); emitter.get().emit(1); sub.request(2); emitter.get().emit(2).emit(3).emit(4); sub.request(1); emitter.get().emit(5).complete(); sub .assertCompletedSuccessfully() .assertReceived(2, 3, 5); }
Example 6
Source File: MultiFromIterableTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testRequestProtocol() { ArrayList<Integer> list = new ArrayList<>(256); for (int i = 1; i <= 256 + 1; i++) { list.add(i); } Multi<Integer> f = Multi.createFrom().iterable(list); MultiAssertSubscriber<Integer> ts = MultiAssertSubscriber.create(); ts.assertHasNotReceivedAnyItem(); f.subscribe(ts); ts.request(1); ts.assertReceived(1); ts.request(2); ts.assertReceived(1, 2, 3); ts.request(3); ts.assertReceived(1, 2, 3, 4, 5, 6); ts.request(list.size()); ts.assertTerminated(); }
Example 7
Source File: MultiCreateFromEmitterTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testWhenConsumerThrowsAnException() { AtomicBoolean onTerminationCalled = new AtomicBoolean(); MultiAssertSubscriber<Integer> ts = MultiAssertSubscriber.create(); Multi<Integer> multi = Multi.createFrom().emitter(emitter -> { emitter.onTermination(() -> onTerminationCalled.set(true)); emitter.emit(1); emitter.emit(2); throw new IllegalStateException("boom"); }); multi.subscribe(ts); ts.assertSubscribed() .request(Long.MAX_VALUE) .assertHasFailedWith(IllegalStateException.class, "boom") .assertReceived(1, 2); assertThat(onTerminationCalled).isTrue(); }
Example 8
Source File: BroadcastProcessorTest.java From smallrye-mutiny with Apache License 2.0 | 6 votes |
@Test public void testUsingTheProcessorWithAnUpstream() { Multi<Integer> upstream = Multi.createFrom().range(0, 10); BroadcastProcessor<Integer> processor = BroadcastProcessor.create(); MultiAssertSubscriber<Integer> s1 = processor.subscribe() .withSubscriber(MultiAssertSubscriber.create(10)); MultiAssertSubscriber<Integer> s2 = processor.subscribe() .withSubscriber(MultiAssertSubscriber.create(10)); upstream.subscribe(processor); s1.assertCompletedSuccessfully() .assertReceived(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); s2.assertCompletedSuccessfully() .assertReceived(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); }
Example 9
Source File: MultiFromIterableTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
/** * This tests the path that can not optimize based on size so must use setProducer. */ @Test public void testWithRawIterable() { Iterable<String> it = () -> new Iterator<String>() { int i; @Override public boolean hasNext() { return i < 3; } @Override public String next() { return String.valueOf(++i); } @Override public void remove() { } }; Multi<String> multi = Multi.createFrom().iterable(it); Subscriber<String> subscriber = Mocks.subscriber(); multi.subscribe(subscriber); verify(subscriber, times(1)).onNext("1"); verify(subscriber, times(1)).onNext("2"); verify(subscriber, times(1)).onNext("3"); verify(subscriber, Mockito.never()).onError(any(Throwable.class)); verify(subscriber, times(1)).onComplete(); }
Example 10
Source File: MultiSwitchOnCompletion.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Override public void subscribe(Subscriber<? super T> subscriber) { if (subscriber == null) { throw new NullPointerException("The subscriber must not be `null`"); } Publisher<T> followup = Multi.createFrom().deferred(() -> { Publisher<? extends T> publisher; try { publisher = supplier.get(); } catch (Throwable e) { return Multi.createFrom().failure(e); } if (publisher == null) { return Multi.createFrom().failure(new NullPointerException(SUPPLIER_PRODUCED_NULL)); } if (publisher instanceof Multi) { //noinspection unchecked return (Multi<T>) publisher; } else { return Multi.createFrom().publisher(publisher); } }); @SuppressWarnings("unchecked") Publisher<T>[] publishers = new Publisher[] { upstream(), followup }; Multi<T> op = Infrastructure.onMultiCreation(new MultiConcatOp<>(false, publishers)); op.subscribe(subscriber); }
Example 11
Source File: MultiFromIterableTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testList() { Multi<String> multi = Multi.createFrom().iterable(Arrays.asList("one", "two", "three")); Subscriber<String> subscriber = Mocks.subscriber(); multi.subscribe(subscriber); verify(subscriber, times(1)).onNext("one"); verify(subscriber, times(1)).onNext("two"); verify(subscriber, times(1)).onNext("three"); verify(subscriber, Mockito.never()).onError(any(Throwable.class)); verify(subscriber, times(1)).onComplete(); }
Example 12
Source File: BroadcastProcessorTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testUsingTheProcessorWithAnUpstreamThatHasAlreadyFailed() { Multi<Integer> upstream = Multi.createFrom().failure(new Exception("boom")); BroadcastProcessor<Integer> processor = BroadcastProcessor.create(); upstream.subscribe(processor); MultiAssertSubscriber<Integer> s1 = processor.subscribe() .withSubscriber(MultiAssertSubscriber.create(10)); MultiAssertSubscriber<Integer> s2 = processor.subscribe() .withSubscriber(MultiAssertSubscriber.create(10)); s1.assertHasFailedWith(Exception.class, "boom"); s2.assertHasFailedWith(Exception.class, "boom"); }
Example 13
Source File: CancelStageFactory.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Override public <I, O> TerminalStage<I, O> create(Engine engine, Stage.Cancel stage) { Objects.requireNonNull(stage); return (Multi<I> flow) -> { //noinspection SubscriberImplementation flow.subscribe(new Subscriber<I>() { @Override public void onSubscribe(Subscription s) { s.cancel(); } @Override public void onNext(I in) { // Do nothing. } @Override public void onError(Throwable t) { // Do nothing. } @Override public void onComplete() { // Do nothing. } }); return Infrastructure.wrapCompletableFuture(CompletableFuture.completedFuture(null)); }; }
Example 14
Source File: BroadcastProcessorTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testUsingTheProcessorWithAnUpstreamAndLateSubscriber() { Multi<Integer> upstream = Multi.createFrom().range(0, 10); BroadcastProcessor<Integer> processor = BroadcastProcessor.create(); upstream.subscribe(processor); MultiAssertSubscriber<Integer> s1 = processor.subscribe() .withSubscriber(MultiAssertSubscriber.create(10)); s1.assertCompletedSuccessfully() .assertHasNotReceivedAnyItem(); }
Example 15
Source File: MultiFromIterableTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testThatWeCanSubscribeMultipleTimes() { Multi<Integer> f = Multi.createFrom().iterable(Arrays.asList(1, 2, 3)); for (int i = 0; i < 10; i++) { MultiAssertSubscriber<Integer> ts = MultiAssertSubscriber.create(Long.MAX_VALUE); f.subscribe(ts); ts.assertReceived(1, 2, 3) .assertCompletedSuccessfully(); } }
Example 16
Source File: MultiCreateFromEmitterTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testWithDefaultBackPressure() { MultiAssertSubscriber<Integer> ts = MultiAssertSubscriber.create(); Multi<Integer> multi = Multi.createFrom().emitter(emitter -> { emitter.emit(1); emitter.emit(2); emitter.emit(3); emitter.complete(); }); multi.subscribe(ts); ts.assertSubscribed() .request(Long.MAX_VALUE) .assertCompletedSuccessfully() .assertReceived(1, 2, 3); }
Example 17
Source File: MultiOnOverflowTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testDropPreviousStrategyWithEmitter() { MultiAssertSubscriber<Integer> sub = MultiAssertSubscriber.create(); AtomicReference<MultiEmitter<? super Integer>> emitter = new AtomicReference<>(); Multi<Integer> multi = Multi.createFrom().emitter((Consumer<MultiEmitter<? super Integer>>) emitter::set) .onOverflow().dropPreviousItems(); multi.subscribe(sub); emitter.get().emit(1); sub.assertNotTerminated().assertHasNotReceivedAnyItem(); emitter.get().emit(2); sub.assertNotTerminated().assertHasNotReceivedAnyItem(); sub.request(1); sub.assertNotTerminated().assertReceived(2); emitter.get().emit(3).emit(4); sub.request(2); sub.assertNotTerminated().assertReceived(2, 4); emitter.get().emit(5); sub.assertNotTerminated().assertReceived(2, 4, 5); emitter.get().complete(); sub.assertCompletedSuccessfully(); }
Example 18
Source File: MultiRetryWhenOp.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
private static <T> void subscribe(MultiSubscriber<? super T> downstream, Function<? super Multi<Throwable>, ? extends Publisher<?>> triggerStreamFactory, Multi<? extends T> upstream) { TriggerSubscriber other = new TriggerSubscriber(); Subscriber<Throwable> signaller = new SerializedSubscriber<>(other.processor); signaller.onSubscribe(Subscriptions.empty()); MultiSubscriber<T> serialized = new SerializedSubscriber<>(downstream); RetryWhenOperator<T> operator = new RetryWhenOperator<>(upstream, serialized, signaller); other.operator = operator; serialized.onSubscribe(operator); Publisher<?> publisher; try { publisher = triggerStreamFactory.apply(other); if (publisher == null) { throw new NullPointerException("The stream factory returned `null`"); } } catch (Throwable e) { downstream.onFailure(e); return; } publisher.subscribe(other); if (!operator.isCancelled()) { upstream.subscribe(operator); } }
Example 19
Source File: MultiFromIterableTest.java From smallrye-mutiny with Apache License 2.0 | 5 votes |
@Test public void testWithoutBackPressure() { Multi<Integer> f = Multi.createFrom().iterable(Arrays.asList(1, 2, 3, 4, 5)); MultiAssertSubscriber<Integer> ts = MultiAssertSubscriber.create(); ts.assertHasNotReceivedAnyItem(); ts.request(Long.MAX_VALUE); // infinite f.subscribe(ts); ts.assertReceived(1, 2, 3, 4, 5); ts.assertTerminated(); }
Example 20
Source File: MultiCreateFromEmitterTest.java From smallrye-mutiny with Apache License 2.0 | 4 votes |
@Test public void testCancellationWhenSubscriberThrowAnException() { AtomicBoolean errored = new AtomicBoolean(); AtomicBoolean cancelled = new AtomicBoolean(); AtomicInteger numberOfResults = new AtomicInteger(); Multi<Integer> source = Multi.createFrom().emitter(emitter -> { emitter.onTermination(() -> cancelled.set(true)); emitter.emit(1); emitter.emit(2); emitter.emit(3); emitter.complete(); }); //noinspection SubscriberImplementation source.subscribe(new Subscriber<Integer>() { @Override public void onSubscribe(Subscription s) { s.request(10); } @Override public void onNext(Integer integer) { numberOfResults.incrementAndGet(); throw new RuntimeException("BOOM!"); } @Override public void onError(Throwable t) { errored.set(true); } @Override public void onComplete() { // ignored. } }); assertThat(errored).isFalse(); assertThat(cancelled).isTrue(); assertThat(numberOfResults.get()).isEqualTo(1); }