Java Code Examples for reactor.core.publisher.MonoProcessor#onError()

The following examples show how to use reactor.core.publisher.MonoProcessor#onError() . 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: ReactorNettyTcpClient.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private <T> Consumer<T> updateConnectMono(MonoProcessor<Void> connectMono) {
	return o -> {
		if (!connectMono.isTerminated()) {
			if (o instanceof Throwable) {
				connectMono.onError((Throwable) o);
			}
			else {
				connectMono.onComplete();
			}
		}
	};
}
 
Example 2
Source File: ReactorNettyTcpClient.java    From java-technology-stack with MIT License 5 votes vote down vote up
private <T> Consumer<T> updateConnectMono(MonoProcessor<Void> connectMono) {
	return o -> {
		if (!connectMono.isTerminated()) {
			if (o instanceof Throwable) {
				connectMono.onError((Throwable) o);
			}
			else {
				connectMono.onComplete();
			}
		}
	};
}
 
Example 3
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldExpireValueOnRacingDisposeAndComplete() {
  for (int i = 0; i < 10000; i++) {
    final int index = i;

    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingResolution()
        .thenAddObserver(consumer)
        .assertPendingSubscribers(1)
        .assertPendingResolution()
        .then(self -> RaceTestUtils.race(() -> self.complete("value" + index), self::dispose))
        .assertDisposeCalled(1)
        .assertExpiredExactly("value" + index)
        .ifResolvedAssertEqual("value" + index)
        .assertIsDisposed();

    if (processor.isError()) {
      Assertions.assertThat(processor.getError())
          .isInstanceOf(CancellationException.class)
          .hasMessage("Disposed");

    } else {
      Assertions.assertThat(processor.peek()).isEqualTo("value" + i);
    }
  }
}
 
Example 4
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldNotifyAllTheSubscribersUnderRacingBetweenSubscribeAndComplete() {
  for (int i = 0; i < 10000; i++) {
    final String valueToSend = "value" + i;

    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };

    MonoProcessor<String> processor2 = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer2 =
        (v, t) -> {
          if (t != null) {
            processor2.onError(t);
            return;
          }

          processor2.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .then(
            self -> {
              RaceTestUtils.race(() -> self.complete(valueToSend), () -> self.observe(consumer));

              StepVerifier.create(processor)
                  .expectNext(valueToSend)
                  .expectComplete()
                  .verify(Duration.ofMillis(10));
            })
        .assertDisposeCalled(0)
        .assertReceivedExactly(valueToSend)
        .assertNothingExpired()
        .thenAddObserver(consumer2)
        .assertPendingSubscribers(0);

    StepVerifier.create(processor2)
        .expectNext(valueToSend)
        .expectComplete()
        .verify(Duration.ofMillis(10));
  }
}
 
Example 5
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldNotExpireNewlyResolvedValueIfSubscribeIsRacingWithInvalidate() {
  for (int i = 0; i < 10000; i++) {
    final String valueToSend = "value" + i;
    final String valueToSend2 = "value2" + i;

    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };

    MonoProcessor<String> processor2 = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer2 =
        (v, t) -> {
          if (t != null) {
            processor2.onError(t);
            return;
          }

          processor2.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .thenAddObserver(consumer)
        .then(
            self -> {
              self.complete(valueToSend);

              StepVerifier.create(processor)
                  .expectNext(valueToSend)
                  .expectComplete()
                  .verify(Duration.ofMillis(10));
            })
        .assertReceivedExactly(valueToSend)
        .then(
            self ->
                RaceTestUtils.race(
                    self::invalidate,
                    () -> {
                      self.observe(consumer2);
                      if (!processor2.isTerminated()) {
                        self.complete(valueToSend2);
                      }
                    },
                    Schedulers.parallel()))
        .then(
            self -> {
              if (self.isPending()) {
                self.assertReceivedExactly(valueToSend);
              } else {
                self.assertReceivedExactly(valueToSend, valueToSend2);
              }
            })
        .assertExpiredExactly(valueToSend)
        .assertPendingSubscribers(0)
        .assertDisposeCalled(0)
        .then(
            self ->
                StepVerifier.create(processor2)
                    .expectNextMatches(
                        (v) -> {
                          if (self.subscribers == ResolvingOperator.READY) {
                            return v.equals(valueToSend2);
                          } else {
                            return v.equals(valueToSend);
                          }
                        })
                    .expectComplete()
                    .verify(Duration.ofMillis(100)));
  }
}
 
Example 6
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldNotExpireNewlyResolvedValueIfBlockIsRacingWithInvalidate() {
  for (int i = 0; i < 10000; i++) {
    final String valueToSend = "value" + i;
    final String valueToSend2 = "value2" + i;

    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .thenAddObserver(consumer)
        .then(
            self -> {
              self.complete(valueToSend);

              StepVerifier.create(processor)
                  .expectNext(valueToSend)
                  .expectComplete()
                  .verify(Duration.ofMillis(10));
            })
        .assertReceivedExactly(valueToSend)
        .then(
            self ->
                RaceTestUtils.race(
                    () ->
                        Assertions.assertThat(self.block(null))
                            .matches((v) -> v.equals(valueToSend) || v.equals(valueToSend2)),
                    () ->
                        RaceTestUtils.race(
                            self::invalidate,
                            () -> {
                              for (; ; ) {
                                if (self.subscribers != ResolvingOperator.READY) {
                                  self.complete(valueToSend2);
                                  break;
                                }
                              }
                            },
                            Schedulers.parallel()),
                    Schedulers.parallel()))
        .then(
            self -> {
              if (self.isPending()) {
                self.assertReceivedExactly(valueToSend);
              } else {
                self.assertReceivedExactly(valueToSend, valueToSend2);
              }
            })
        .assertExpiredExactly(valueToSend)
        .assertPendingSubscribers(0)
        .assertDisposeCalled(0);
  }
}
 
Example 7
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEstablishValueOnceInCaseOfRacingBetweenSubscribers() {
  for (int i = 0; i < 10000; i++) {
    final String valueToSend = "value" + i;

    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };

    MonoProcessor<String> processor2 = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer2 =
        (v, t) -> {
          if (t != null) {
            processor2.onError(t);
            return;
          }

          processor2.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .then(
            self ->
                RaceTestUtils.race(() -> self.observe(consumer), () -> self.observe(consumer2)))
        .assertSubscribeCalled(1)
        .assertPendingSubscribers(2)
        .then(self -> self.complete(valueToSend))
        .assertPendingSubscribers(0)
        .assertReceivedExactly(valueToSend)
        .assertNothingExpired()
        .assertDisposeCalled(0)
        .then(
            self -> {
              Assertions.assertThat(processor.isTerminated()).isTrue();
              Assertions.assertThat(processor2.isTerminated()).isTrue();

              Assertions.assertThat(processor.peek()).isEqualTo(valueToSend);
              Assertions.assertThat(processor2.peek()).isEqualTo(valueToSend);

              Assertions.assertThat(self.subscribers).isEqualTo(ResolvingOperator.READY);

              Assertions.assertThat(self.add(consumer)).isEqualTo(ResolvingOperator.READY_STATE);
            });
  }
}
 
Example 8
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldEstablishValueOnceInCaseOfRacingBetweenSubscribeAndBlock() {
  for (int i = 0; i < 10000; i++) {
    final String valueToSend = "value" + i;

    MonoProcessor<String> processor = MonoProcessor.create();

    MonoProcessor<String> processor2 = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer2 =
        (v, t) -> {
          if (t != null) {
            processor2.onError(t);
            return;
          }

          processor2.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .whenSubscribe(self -> self.complete(valueToSend))
        .then(
            self ->
                RaceTestUtils.race(
                    () -> processor.onNext(self.block(null)), () -> self.observe(consumer2)))
        .assertSubscribeCalled(1)
        .assertPendingSubscribers(0)
        .assertReceivedExactly(valueToSend)
        .assertNothingExpired()
        .assertDisposeCalled(0)
        .then(
            self -> {
              Assertions.assertThat(processor.isTerminated()).isTrue();
              Assertions.assertThat(processor2.isTerminated()).isTrue();

              Assertions.assertThat(processor.peek()).isEqualTo(valueToSend);
              Assertions.assertThat(processor2.peek()).isEqualTo(valueToSend);

              Assertions.assertThat(self.subscribers).isEqualTo(ResolvingOperator.READY);

              Assertions.assertThat(self.add(consumer2)).isEqualTo(ResolvingOperator.READY_STATE);
            });
  }
}
 
Example 9
Source File: ResolvingOperatorTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldExpireValueOnRacingDisposeAndError() {
  Hooks.onErrorDropped(t -> {});
  RuntimeException runtimeException = new RuntimeException("test");
  for (int i = 0; i < 10000; i++) {
    MonoProcessor<String> processor = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer =
        (v, t) -> {
          if (t != null) {
            processor.onError(t);
            return;
          }

          processor.onNext(v);
        };
    MonoProcessor<String> processor2 = MonoProcessor.create();
    BiConsumer<String, Throwable> consumer2 =
        (v, t) -> {
          if (t != null) {
            processor2.onError(t);
            return;
          }

          processor2.onNext(v);
        };

    ResolvingTest.<String>create()
        .assertNothingExpired()
        .assertNothingReceived()
        .assertPendingSubscribers(0)
        .assertPendingResolution()
        .thenAddObserver(consumer)
        .assertSubscribeCalled(1)
        .assertPendingSubscribers(1)
        .then(self -> RaceTestUtils.race(() -> self.terminate(runtimeException), self::dispose))
        .assertPendingSubscribers(0)
        .assertNothingExpired()
        .assertDisposeCalled(1)
        .then(
            self -> {
              Assertions.assertThat(self.subscribers).isEqualTo(ResolvingOperator.TERMINATED);

              Assertions.assertThat(self.add((v, t) -> {}))
                  .isEqualTo(ResolvingOperator.TERMINATED_STATE);
            })
        .thenAddObserver(consumer2);

    StepVerifier.create(processor)
        .expectErrorSatisfies(
            t -> {
              if (t instanceof CancellationException) {
                Assertions.assertThat(t)
                    .isInstanceOf(CancellationException.class)
                    .hasMessage("Disposed");
              } else {
                Assertions.assertThat(t).isInstanceOf(RuntimeException.class).hasMessage("test");
              }
            })
        .verify(Duration.ofMillis(10));

    StepVerifier.create(processor2)
        .expectErrorSatisfies(
            t -> {
              if (t instanceof CancellationException) {
                Assertions.assertThat(t)
                    .isInstanceOf(CancellationException.class)
                    .hasMessage("Disposed");
              } else {
                Assertions.assertThat(t).isInstanceOf(RuntimeException.class).hasMessage("test");
              }
            })
        .verify(Duration.ofMillis(10));

    // no way to guarantee equality because of racing
    //      Assertions.assertThat(processor.getError())
    //                .isEqualTo(processor2.getError());
  }
}