Java Code Examples for reactor.core.Scannable#from()

The following examples show how to use reactor.core.Scannable#from() . 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: FluxMetrics.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * Extract the name from the upstream, and detect if there was an actual name (ie. distinct from {@link
 * Scannable#stepName()}) set by the user.
 *
 * @param source the upstream
 *
 * @return a name
 */
static String resolveName(Publisher<?> source) {
	Scannable scannable = Scannable.from(source);
	if (scannable.isScanAvailable()) {
		String nameOrDefault = scannable.name();
		if (scannable.stepName()
		             .equals(nameOrDefault)) {
			return REACTOR_DEFAULT_NAME;
		}
		else {
			return nameOrDefault;
		}
	}
	else {
		log.warn("Attempting to activate metrics but the upstream is not Scannable. " + "You might want to use `name()` (and optionally `tags()`) right before `metrics()`");
		return REACTOR_DEFAULT_NAME;
	}

}
 
Example 2
Source File: FluxPublishOnTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void scanRunOn() {
	Scannable publishOnScannable = Scannable.from(
			Flux.just(1).hide()
			    .publishOn(Schedulers.boundedElastic())
	);
	Scannable runOnScannable = publishOnScannable.scan(Scannable.Attr.RUN_ON);

	assertThat(runOnScannable).isNotNull()
	                          .matches(Scannable::isScanAvailable, "isScanAvailable");

	System.out.println(runOnScannable + " isScannable " + runOnScannable.isScanAvailable());
	System.out.println(runOnScannable.scan(Scannable.Attr.NAME));
	runOnScannable.parents().forEach(System.out::println);
	System.out.println(runOnScannable.scan(Scannable.Attr.BUFFERED));
}
 
Example 3
Source File: SourceProducer.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
@Nullable
default Object scanUnsafe(Attr key) {
	if (key == Attr.PARENT) return Scannable.from(null);
	if (key == Attr.ACTUAL) return Scannable.from(null);

	return null;
}
 
Example 4
Source File: FluxMetrics.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
/**
 * Extract the tags from the upstream
 *
 * @param source the upstream
 *
 * @return a {@link Tags} of {@link Tag}
 */
static Tags resolveTags(Publisher<?> source, Tags tags, String sequenceName) {
	Scannable scannable = Scannable.from(source);
	tags = tags.and(Tag.of(FluxMetrics.TAG_SEQUENCE_NAME, sequenceName));

	if (scannable.isScanAvailable()) {
		return scannable.tags()
		                //Note the combiner below is for parallel streams, which won't be used
		                //For the identity, `commonTags` should be ok (even if reduce uses it multiple times)
		                //since it deduplicates
		                .reduce(tags, TAG_ACCUMULATOR, TAG_COMBINER);
	}

	return tags;
}
 
Example 5
Source File: FluxTimeout.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Nullable
static String addNameToTimeoutDescription(Publisher<?> source,
		@Nullable  String timeoutDescription) {
	if (timeoutDescription == null) {
		return null;
	}

	Scannable s = Scannable.from(source);
	if (s.isScanAvailable()) {
		return timeoutDescription + " in '" + s.name() + "'";
	}
	else {
		return timeoutDescription;
	}
}
 
Example 6
Source File: SingleWorkerScheduler.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    Scannable mainScannable = Scannable.from(main);
    if (mainScannable.isScanAvailable()) {
        return Schedulers.SINGLE + "Worker(" + mainScannable.scanUnsafe(Attr.NAME) + ")";
    }
    return Schedulers.SINGLE + "Worker(" + main.toString() + ")";
}
 
Example 7
Source File: BaseOperatorTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
final void touchInner(@Nullable Object t){
	if(t == null) return;
	Scannable o = Scannable.from(t);
	o.scan(Attr.ACTUAL);
	o.scan(Attr.BUFFERED);
	o.scan(Attr.CANCELLED);
	o.scan(Attr.CAPACITY);
	o.scan(Attr.DELAY_ERROR);
	o.scan(Attr.ERROR);
	o.scan(Attr.PREFETCH);
	o.scan(Attr.PARENT);
	o.scan(Attr.REQUESTED_FROM_DOWNSTREAM);
	o.scan(Attr.TERMINATED);
	o.inners();
}
 
Example 8
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureNoLeaksPopulatedQueueAndRacingCancelAndOnNext() {
	Assumptions.assumeThat(discardScenario.subscriptionsNumber).isOne();
	for (int i = 0; i < 10000; i++) {
		tracker.reset();
		TestPublisher<Tracked> testPublisher = TestPublisher.createNoncompliant(
				TestPublisher.Violation.DEFER_CANCELLATION,
				TestPublisher.Violation.REQUEST_OVERFLOW);
		Publisher<Tracked> source = discardScenario.producePublisherFromSources(testPublisher);

		if (conditional) {
			if (source instanceof Flux) {
				source = ((Flux<Tracked>) source).filter(t -> true);
			}
			else {
				source = ((Mono<Tracked>) source).filter(t -> true);
			}
		}

		Scannable scannable = Scannable.from(source);
		Integer prefetch = scannable.scan(Scannable.Attr.PREFETCH);

		Assumptions.assumeThat(prefetch).isNotZero();

		AssertSubscriber<Tracked> assertSubscriber = new AssertSubscriber<>(Operators.enableOnDiscard(null, Tracked::safeRelease), 0);
		if (fused) {
			assertSubscriber.requestedFusionMode(Fuseable.ANY);
		}
		source.subscribe(assertSubscriber);

		testPublisher.next(tracker.track(1));
		testPublisher.next(tracker.track(2));

		Tracked value3 = tracker.track(3);
		Tracked value4 = tracker.track(4);
		Tracked value5 = tracker.track(5);

		RaceTestUtils.race(assertSubscriber::cancel, () -> {
			testPublisher.next(value3);
			testPublisher.next(value4);
			testPublisher.next(value5);
		}, scheduler);

		List<Tracked> values = assertSubscriber.values();
		values.forEach(Tracked::release);

		tracker.assertNoLeaks();
	}
}
 
Example 9
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureNoLeaksPopulatedQueueAndRacingCancelAndOnComplete() {
	Assumptions.assumeThat(discardScenario.subscriptionsNumber).isOne();
	for (int i = 0; i < 10000; i++) {
		tracker.reset();
		TestPublisher<Tracked> testPublisher = TestPublisher.createNoncompliant(
				TestPublisher.Violation.DEFER_CANCELLATION,
				TestPublisher.Violation.REQUEST_OVERFLOW);
		Publisher<Tracked> source = discardScenario.producePublisherFromSources(testPublisher);

		if (conditional) {
			if (source instanceof Flux) {
				source = ((Flux<Tracked>) source).filter(t -> true);
			}
			else {
				source = ((Mono<Tracked>) source).filter(t -> true);
			}
		}

		Scannable scannable = Scannable.from(source);
		Integer prefetch = scannable.scan(Scannable.Attr.PREFETCH);

		Assumptions.assumeThat(prefetch).isNotZero();

		AssertSubscriber<Tracked> assertSubscriber = new AssertSubscriber<>(Operators.enableOnDiscard(null, Tracked::safeRelease), 0);
		if (fused) {
			assertSubscriber.requestedFusionMode(Fuseable.ANY);
		}
		source.subscribe(assertSubscriber);

		testPublisher.next(tracker.track(1));
		testPublisher.next(tracker.track(2));
		testPublisher.next(tracker.track(3));
		testPublisher.next(tracker.track(4));

		RaceTestUtils.race(
				assertSubscriber::cancel,
				() -> testPublisher.complete(),
				scheduler);

		List<Tracked> values = assertSubscriber.values();
		values.forEach(Tracked::release);

		tracker.assertNoLeaks();
	}
}
 
Example 10
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureNoLeaksPopulatedQueueAndRacingCancelAndOnError() {
	Assumptions.assumeThat(discardScenario.subscriptionsNumber).isOne();
	for (int i = 0; i < 10000; i++) {
		tracker.reset();
		TestPublisher<Tracked> testPublisher = TestPublisher.createNoncompliant(
				TestPublisher.Violation.DEFER_CANCELLATION,
				TestPublisher.Violation.REQUEST_OVERFLOW);
		@SuppressWarnings("unchecked")
		Publisher<Tracked> source = discardScenario.producePublisherFromSources(testPublisher);

		if (conditional) {
			if (source instanceof Flux) {
				source = ((Flux<Tracked>) source).filter(t -> true);
			}
			else {
				source = ((Mono<Tracked>) source).filter(t -> true);
			}
		}

		Scannable scannable = Scannable.from(source);
		Integer prefetch = scannable.scan(Scannable.Attr.PREFETCH);

		Assumptions.assumeThat(prefetch).isNotZero();

		AssertSubscriber<Tracked> assertSubscriber = new AssertSubscriber<>(Operators.enableOnDiscard(null, Tracked::safeRelease), 0);
		if (fused) {
			assertSubscriber.requestedFusionMode(Fuseable.ANY);
		}
		source.subscribe(assertSubscriber);

		testPublisher.next(tracker.track(1));
		testPublisher.next(tracker.track(2));
		testPublisher.next(tracker.track(3));
		testPublisher.next(tracker.track(4));

		RaceTestUtils.race(
				assertSubscriber::cancel,
				() -> testPublisher.error(new RuntimeException("test")),
				scheduler);

		List<Tracked> values = assertSubscriber.values();
		values.forEach(Tracked::release);
		if (assertSubscriber.isTerminated()) {
			// has a chance to error with rejected exception
			assertSubscriber.assertError();
		}

		tracker.assertNoLeaks();
	}
}
 
Example 11
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureNoLeaksPopulatedQueueAndRacingCancelAndOverflowError() {
	Assumptions.assumeThat(discardScenario.subscriptionsNumber).isOne();
	for (int i = 0; i < 10000; i++) {
		tracker.reset();
		TestPublisher<Tracked> testPublisher = TestPublisher.createNoncompliant(
				TestPublisher.Violation.DEFER_CANCELLATION,
				TestPublisher.Violation.REQUEST_OVERFLOW);
		@SuppressWarnings("unchecked")
		Publisher<Tracked> source = discardScenario.producePublisherFromSources(testPublisher);

		if (conditional) {
			if (source instanceof Flux) {
				source = ((Flux<Tracked>) source).filter(t -> true);
			}
			else {
				source = ((Mono<Tracked>) source).filter(t -> true);
			}
		}

		Scannable scannable = Scannable.from(source);
		Integer capacity = scannable.scan(Scannable.Attr.CAPACITY);
		Integer prefetch = Math.min(scannable.scan(Scannable.Attr.PREFETCH),
				capacity == 0 ? Integer.MAX_VALUE : capacity);

		Assumptions.assumeThat(prefetch).isNotZero();
		Assumptions.assumeThat(prefetch).isNotEqualTo(Integer.MAX_VALUE);
		AssertSubscriber<Tracked> assertSubscriber = new AssertSubscriber<>(Operators.enableOnDiscard(null, Tracked::safeRelease), 0);
		if (fused) {
			assertSubscriber.requestedFusionMode(Fuseable.ANY);
		}
		source.subscribe(assertSubscriber);

		for (int j = 0; j < prefetch - 1; j++) {
			testPublisher.next(tracker.track(j));
		}

		Tracked lastValue = tracker.track(prefetch - 1);
		Tracked overflowValue1 = tracker.track(prefetch);
		Tracked overflowValue2 = tracker.track(prefetch + 1);

		RaceTestUtils.race(assertSubscriber::cancel, () -> {
			testPublisher.next(lastValue);
			testPublisher.next(overflowValue1);
			testPublisher.next(overflowValue2);
		});

		List<Tracked> values = assertSubscriber.values();
		values.forEach(Tracked::release);
		if (assertSubscriber.isTerminated()) {
			// has a chance to error with rejected exception
			assertSubscriber.assertError();
		}

		tracker.assertNoLeaks();
	}
}
 
Example 12
Source File: OnDiscardShouldNotLeakTest.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void ensureNoLeaksPopulatedQueueAndRacingCancelAndRequest() {
	Assumptions.assumeThat(discardScenario.subscriptionsNumber).isOne();
	for (int i = 0; i < 10000; i++) {
		tracker.reset();
		TestPublisher<Tracked> testPublisher = TestPublisher.createNoncompliant(
				TestPublisher.Violation.DEFER_CANCELLATION,
				TestPublisher.Violation.REQUEST_OVERFLOW);
		@SuppressWarnings("unchecked")
		Publisher<Tracked> source = discardScenario.producePublisherFromSources(testPublisher);

		if (conditional) {
			if (source instanceof Flux) {
				source = ((Flux<Tracked>) source).filter(t -> true);
			}
			else {
				source = ((Mono<Tracked>) source).filter(t -> true);
			}
		}

		Scannable scannable = Scannable.from(source);
		Integer prefetch = scannable.scan(Scannable.Attr.PREFETCH);

		Assumptions.assumeThat(prefetch).isNotZero();

		AssertSubscriber<Tracked> assertSubscriber = new AssertSubscriber<>(Operators.enableOnDiscard(null, Tracked::safeRelease), 0);
		if (fused) {
			assertSubscriber.requestedFusionMode(Fuseable.ANY);
		}
		source.subscribe(assertSubscriber);

		testPublisher.next(tracker.track(1));
		testPublisher.next(tracker.track(2));
		testPublisher.next(tracker.track(3));
		testPublisher.next(tracker.track(4));

		RaceTestUtils.race(
				assertSubscriber::cancel,
				() -> assertSubscriber.request(Long.MAX_VALUE),
				scheduler);

		List<Tracked> values = assertSubscriber.values();
		values.forEach(Tracked::release);

		tracker.assertNoLeaks();
	}
}
 
Example 13
Source File: ReconnectMonoTests.java    From rsocket-java with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldBeScannable() {
  final TestPublisher<String> publisher =
      TestPublisher.createNoncompliant(TestPublisher.Violation.REQUEST_OVERFLOW);

  final Mono<String> parent = publisher.mono();
  final ReconnectMono<String> reconnectMono =
      parent.as(source -> new ReconnectMono<>(source, onExpire(), onValue()));

  final Scannable scannableOfReconnect = Scannable.from(reconnectMono);

  Assertions.assertThat(
          (List)
              scannableOfReconnect.parents().map(s -> s.getClass()).collect(Collectors.toList()))
      .hasSize(1)
      .containsExactly(publisher.mono().getClass());
  Assertions.assertThat(scannableOfReconnect.scanUnsafe(Scannable.Attr.TERMINATED))
      .isEqualTo(false);
  Assertions.assertThat(scannableOfReconnect.scanUnsafe(Scannable.Attr.ERROR)).isNull();

  final MonoProcessor<String> processor = reconnectMono.subscribeWith(MonoProcessor.create());

  final Scannable scannableOfMonoProcessor = Scannable.from(processor);

  Assertions.assertThat(
          (List)
              scannableOfMonoProcessor
                  .parents()
                  .map(s -> s.getClass())
                  .collect(Collectors.toList()))
      .hasSize(4)
      .containsExactly(
          ResolvingOperator.MonoDeferredResolutionOperator.class,
          ReconnectMono.ResolvingInner.class,
          ReconnectMono.class,
          publisher.mono().getClass());

  reconnectMono.dispose();

  Assertions.assertThat(scannableOfReconnect.scanUnsafe(Scannable.Attr.TERMINATED))
      .isEqualTo(true);
  Assertions.assertThat(scannableOfReconnect.scanUnsafe(Scannable.Attr.ERROR))
      .isInstanceOf(CancellationException.class);
}