Java Code Examples for reactor.core.Fuseable#ConditionalSubscriber

The following examples show how to use reactor.core.Fuseable#ConditionalSubscriber . 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: FluxArrayTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void scanConditionalSubscriptionRequested() {
	@SuppressWarnings("unchecked")
	Fuseable.ConditionalSubscriber<? super Object> subscriber = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
	//the mock will not drain the request, so it can be tested
	Mockito.when(subscriber.tryOnNext(Mockito.any()))
	       .thenReturn(false);

	FluxArray.ArrayConditionalSubscription<Object> test =
			new FluxArray.ArrayConditionalSubscription<>(subscriber,
					new Object[]{"foo", "bar", "baz"});

	test.request(2);
	assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(2L);
	assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(3);
}
 
Example 2
Source File: ParallelLog.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T>[] subscribers) {
	if (!validate(subscribers)) {
		return;
	}
	
	int n = subscribers.length;
	CoreSubscriber<? super T>[] parents = new CoreSubscriber[n];

	boolean conditional = subscribers[0] instanceof Fuseable.ConditionalSubscriber;

	for (int i = 0; i < n; i++) {
		if (conditional) {
			parents[i] = new FluxPeekFuseable.PeekConditionalSubscriber<>(
					(Fuseable.ConditionalSubscriber<T>)subscribers[i], log);
		}
		else {
			parents[i] = new FluxPeek.PeekSubscriber<>(subscribers[i], log);
		}
	}
	
	source.subscribe(parents);
}
 
Example 3
Source File: FluxUsingTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
  public void scanConditionalSubscriber() {
@SuppressWarnings("unchecked")
Fuseable.ConditionalSubscriber<Integer> actual = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
FluxUsing.UsingConditionalSubscriber<Integer, String> test =
		new FluxUsing.UsingConditionalSubscriber<>(actual, s -> {}, "", true);
      Subscription parent = Operators.emptySubscription();
      test.onSubscribe(parent);

      Assertions.assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
      Assertions.assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

      Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
      Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
      test.onComplete();
      Assertions.assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
      Assertions.assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
  }
 
Example 4
Source File: ParallelPeek.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T>[] subscribers) {
	if (!validate(subscribers)) {
		return;
	}
	
	int n = subscribers.length;

	CoreSubscriber<? super T>[] parents = new CoreSubscriber[n];

	boolean conditional = subscribers[0] instanceof Fuseable.ConditionalSubscriber;

	for (int i = 0; i < n; i++) {
		if (conditional) {
			parents[i] = new FluxPeekFuseable.PeekConditionalSubscriber<>(
					(Fuseable.ConditionalSubscriber<T>)subscribers[i], this);
		}
		else {
			parents[i] = new FluxPeek.PeekSubscriber<>(subscribers[i], this);
		}
	}
	
	source.subscribe(parents);
}
 
Example 5
Source File: FluxDistinctTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void distinctPredicateThrowsConditionalOnNext() {
	IllegalStateException error = new IllegalStateException("forced failure");

	@SuppressWarnings("unchecked")
	Fuseable.ConditionalSubscriber<Integer> actualConditional = Mockito.mock(Fuseable.ConditionalSubscriber.class);
	when(actualConditional.currentContext()).thenReturn(Context.empty());

	DistinctConditionalSubscriber<Integer, Integer, Set<Integer>> conditionalSubscriber =
			new DistinctConditionalSubscriber<>(
					actualConditional,
					new HashSet<>(),
					k -> k,
					(c, k) -> { throw error; },
					Set::clear);

	conditionalSubscriber.onNext(1);

	verify(actualConditional, times(1)).onError(error);
}
 
Example 6
Source File: FluxMapTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void scanFuseableConditionalSubscriber() {
 @SuppressWarnings("unchecked")
 Fuseable.ConditionalSubscriber<String> actual = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
    FluxMapFuseable.MapFuseableConditionalSubscriber<Integer, String> test =
    		new FluxMapFuseable.MapFuseableConditionalSubscriber<>(actual, i -> String.valueOf(i));
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);

    assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

    assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    test.onError(new IllegalStateException("boom"));
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
}
 
Example 7
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
  public void scanConditionalSubscriber() {
@SuppressWarnings("unchecked")
Fuseable.ConditionalSubscriber<Integer> actual = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
      FluxPublishOn.PublishOnConditionalSubscriber<Integer> test =
      		new FluxPublishOn.PublishOnConditionalSubscriber<>(actual, Schedulers.single(),
      				Schedulers.single().createWorker(), true, 123, 123, Queues.unbounded());
      Subscription parent = Operators.emptySubscription();
      test.onSubscribe(parent);

      assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
      assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);
      assertThat(test.scan(Scannable.Attr.DELAY_ERROR)).isTrue();
      assertThat(test.scan(Scannable.Attr.PREFETCH)).isEqualTo(123);
      test.requested = 35;
      assertThat(test.scan(Scannable.Attr.REQUESTED_FROM_DOWNSTREAM)).isEqualTo(35L);
      test.queue.add(1);
      assertThat(test.scan(Scannable.Attr.BUFFERED)).isEqualTo(1);

      assertThat(test.scan(Scannable.Attr.ERROR)).isNull();
      assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
      test.onError(new IllegalStateException("boom"));
      assertThat(test.scan(Scannable.Attr.ERROR)).hasMessage("boom");
      assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();

      assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
      test.cancel();
      assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
  }
 
Example 8
Source File: OperatorsTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void convertConditionalToConditionalShouldReturnTheSameInstance() {
	@SuppressWarnings("unchecked")
	Fuseable.ConditionalSubscriber<String> original = Mockito.mock(Fuseable.ConditionalSubscriber.class);

	Assertions.assertThat(Operators.toConditionalSubscriber(original))
	          .isSameAs(original);
}
 
Example 9
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void unitRequestsAreSerialTest() {
    @SuppressWarnings("unchecked")
    BiFunction<FluxSwitchOnFirst.AbstractSwitchOnFirstMain, CoreSubscriber, InnerOperator>[] factories = new BiFunction[] {
            (parent, assertSubscriber) -> new FluxSwitchOnFirst.SwitchOnFirstControlSubscriber((FluxSwitchOnFirst.AbstractSwitchOnFirstMain) parent, (CoreSubscriber) assertSubscriber, true),
            (parent, assertSubscriber) -> new FluxSwitchOnFirst.SwitchOnFirstConditionalControlSubscriber((FluxSwitchOnFirst.AbstractSwitchOnFirstMain) parent, (Fuseable.ConditionalSubscriber) assertSubscriber, true)
    };
    for (BiFunction<FluxSwitchOnFirst.AbstractSwitchOnFirstMain, CoreSubscriber, InnerOperator> factory : factories) {
        for (int i = 0; i < 100000; i++) {
            long[] valueHolder = new long[] { 0 };
            FluxSwitchOnFirst.AbstractSwitchOnFirstMain mockParent = Mockito.mock(FluxSwitchOnFirst.AbstractSwitchOnFirstMain.class);
            Mockito.doNothing().when(mockParent).request(Mockito.anyLong());
            Mockito.doNothing().when(mockParent).cancel();
            Subscription mockSubscription = Mockito.mock(Subscription.class);
            Mockito.doAnswer((a) -> valueHolder[0] += (long) a.getArgument(0)).when(mockSubscription).request(Mockito.anyLong());
            Mockito.doNothing().when(mockSubscription).cancel();
            AssertSubscriber<Object> subscriber = AssertSubscriber.create(0);
            InnerOperator switchOnFirstControlSubscriber = factory.apply(mockParent, Operators.toConditionalSubscriber(subscriber));

            switchOnFirstControlSubscriber.request(10);
            RaceTestUtils.race(() -> {
                        switchOnFirstControlSubscriber.request(10);
                        switchOnFirstControlSubscriber.request(10);
                        switchOnFirstControlSubscriber.request(10);
                        switchOnFirstControlSubscriber.request(10);
                    },
                    () -> switchOnFirstControlSubscriber.onSubscribe(mockSubscription),
                    Schedulers.parallel());

            switchOnFirstControlSubscriber.request(10);
            Assertions.assertThat(valueHolder[0])
                      .isEqualTo(60L);
            mockSubscription.toString();
        }
    }
}
 
Example 10
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public boolean tryOnNext(T t) {
    @SuppressWarnings("unchecked")
    final Fuseable.ConditionalSubscriber<? super T> i =
            (Fuseable.ConditionalSubscriber<? super T>) this.inner;
    if (this.done || i == Operators.EMPTY_SUBSCRIBER) {
        Operators.onNextDropped(t, currentContext());
        return false;
    }

    if (i == null) {
        final Publisher<? extends R> result;
        final CoreSubscriber<? super R> o = this.outer;

        try {
            result = Objects.requireNonNull(
                this.transformer.apply(Signal.next(t, o.currentContext()), this),
                "The transformer returned a null value"
            );
        }
        catch (Throwable e) {
            this.done = true;
            Operators.error(o, Operators.onOperatorError(this.s, e, t, o.currentContext()));
            return false;
        }

        this.first = t;
        result.subscribe(o);
        return true;
    }

    return i.tryOnNext(t);
}
 
Example 11
Source File: FluxMapTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanConditionalSubscriber() {
 @SuppressWarnings("unchecked")
 Fuseable.ConditionalSubscriber<String> actual = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
    FluxMap.MapConditionalSubscriber<Integer, String> test = new FluxMap.MapConditionalSubscriber<>(actual, i -> String.valueOf(i));
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);

    assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

    assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    test.onError(new IllegalStateException("boom"));
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
}
 
Example 12
Source File: FluxSwitchOnFirstTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void unitCancelRacingTest() {
    @SuppressWarnings("unchecked")
    BiFunction<FluxSwitchOnFirst.AbstractSwitchOnFirstMain, CoreSubscriber, InnerOperator>[] factories = new BiFunction[] {
            (parent, assertSubscriber) -> new FluxSwitchOnFirst.SwitchOnFirstControlSubscriber((FluxSwitchOnFirst.AbstractSwitchOnFirstMain) parent, (CoreSubscriber) assertSubscriber, true),
            (parent, assertSubscriber) -> new FluxSwitchOnFirst.SwitchOnFirstConditionalControlSubscriber((FluxSwitchOnFirst.AbstractSwitchOnFirstMain) parent, (Fuseable.ConditionalSubscriber) assertSubscriber, true)
    };
    for (BiFunction<FluxSwitchOnFirst.AbstractSwitchOnFirstMain, CoreSubscriber, InnerOperator> factory : factories) {
        for (int i = 0; i < 10000; i++) {
            FluxSwitchOnFirst.AbstractSwitchOnFirstMain mockParent = Mockito.mock(FluxSwitchOnFirst.AbstractSwitchOnFirstMain.class);
            Mockito.doNothing().when(mockParent).request(Mockito.anyLong());
            Mockito.doNothing().when(mockParent).cancel();
            Subscription mockSubscription = Mockito.mock(Subscription.class);
            ArgumentCaptor<Long> longArgumentCaptor = ArgumentCaptor.forClass(Long.class);
            Mockito.doNothing().when(mockSubscription).request(longArgumentCaptor.capture());
            Mockito.doNothing().when(mockSubscription).cancel();
            AssertSubscriber<Object> subscriber = AssertSubscriber.create(0);
            InnerOperator switchOnFirstControlSubscriber = factory.apply(mockParent, Operators.toConditionalSubscriber(subscriber));

            switchOnFirstControlSubscriber.request(10);
            RaceTestUtils.race(() -> switchOnFirstControlSubscriber.cancel(), () -> switchOnFirstControlSubscriber.onSubscribe(mockSubscription), Schedulers.parallel());

            Assertions.assertThat(longArgumentCaptor.getAllValues().size()).isBetween(0, 1);
            Mockito.verify(mockParent).cancel();
            if (longArgumentCaptor.getAllValues().size() == 1) {
                Assertions.assertThat(longArgumentCaptor.getValue()).isEqualTo(10L);
            }
            else if (longArgumentCaptor.getAllValues().size() > 1) {
                Assertions.fail("Unexpected number of calls");
            }
        }
    }
}
 
Example 13
Source File: MonoMap.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CoreSubscriber<? super T> subscribeOrReturn(CoreSubscriber<? super R> actual) {
	if (actual instanceof Fuseable.ConditionalSubscriber) {
		Fuseable.ConditionalSubscriber<? super R> cs = (Fuseable.ConditionalSubscriber<? super R>) actual;
		return new FluxMap.MapConditionalSubscriber<>(cs, mapper);
	}
	return new FluxMap.MapSubscriber<>(actual, mapper);
}
 
Example 14
Source File: FluxFilterTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void discardConditionalTryOnNextPredicateMiss() {
	List<Object> discarded = new ArrayList<>();
	Fuseable.ConditionalSubscriber<Integer> actual = new FluxPeekFuseableTest.ConditionalAssertSubscriber<>(
			Context.of(Hooks.KEY_ON_DISCARD, (Consumer<?>) discarded::add));

	FilterConditionalSubscriber<Integer> subscriber =
			new FilterConditionalSubscriber<>(actual, i -> i % 2 == 0);
	subscriber.onSubscribe(Operators.emptySubscription());

	subscriber.tryOnNext(1);
	subscriber.tryOnNext(2);

	assertThat(discarded).containsExactly(1);
}
 
Example 15
Source File: DefaultTestPublisher.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
TestPublisherSubscription(Subscriber<? super T> actual, DefaultTestPublisher<T> parent) {
	this.actual = actual;
	if(actual instanceof Fuseable.ConditionalSubscriber){
		this.actualConditional = (Fuseable.ConditionalSubscriber<? super T>) actual;
	}
	else {
		this.actualConditional = null;
	}
	this.parent = parent;
}
 
Example 16
Source File: ParallelDoOnEach.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void subscribe(CoreSubscriber<? super T>[] subscribers) {
	if (!validate(subscribers)) {
		return;
	}

	int n = subscribers.length;

	CoreSubscriber<? super T>[] parents = new CoreSubscriber[n];

	boolean conditional = subscribers[0] instanceof Fuseable.ConditionalSubscriber;

	for (int i = 0; i < n; i++) {
		CoreSubscriber<? super T> subscriber = subscribers[i];
		SignalPeek<T> signalPeek = new DoOnEachSignalPeek(subscriber.currentContext());

		if (conditional) {
			parents[i] = new FluxPeekFuseable.PeekConditionalSubscriber<>(
					(Fuseable.ConditionalSubscriber<T>) subscriber, signalPeek);
		}
		else {
			parents[i] = new FluxPeek.PeekSubscriber<>(subscriber, signalPeek);
		}
	}

	source.subscribe(parents);
}
 
Example 17
Source File: FluxFilterTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void scanConditionalSubscriber() {
 @SuppressWarnings("unchecked")
 Fuseable.ConditionalSubscriber<String> actual = Mockito.mock(MockUtils.TestScannableConditionalSubscriber.class);
    FilterConditionalSubscriber<String> test = new FilterConditionalSubscriber<>(actual, t -> true);
    Subscription parent = Operators.emptySubscription();
    test.onSubscribe(parent);

    assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
    assertThat(test.scan(Scannable.Attr.ACTUAL)).isSameAs(actual);

    assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
    test.onError(new IllegalStateException("boom"));
    assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
}
 
Example 18
Source File: FluxSwitchOnFirst.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
SwitchOnFirstConditionalMain(
        Fuseable.ConditionalSubscriber<? super R> outer,
        BiFunction<Signal<? extends T>, Flux<T>, Publisher<? extends R>> transformer,
        boolean cancelSourceOnComplete) {
    super(outer, transformer, cancelSourceOnComplete);
}
 
Example 19
Source File: DiscardOnCancelSubscriber.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
DiscardOnCancelFuseableConditionalSubscriber(Fuseable.ConditionalSubscriber<? super T> actual) {
    super(actual);
}
 
Example 20
Source File: DiscardOnCancelSubscriber.java    From r2dbc-mysql with Apache License 2.0 4 votes vote down vote up
DiscardOnCancelFuseableConditionalSubscriber(Fuseable.ConditionalSubscriber<? super T> actual) {
    super(actual);
}