Java Code Examples for reactor.core.Fuseable#QueueSubscription

The following examples show how to use reactor.core.Fuseable#QueueSubscription . 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: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void requestFusionDelegates() {
	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, Clock.SYSTEM, "foo", Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	fuseableSubscriber.onSubscribe(testQueue);

	assertThat(fuseableSubscriber.requestFusion(Fuseable.SYNC))
			.as("fusion mode SYNC").isEqualTo(Fuseable.SYNC);

	assertThat(fuseableSubscriber.requestFusion(Fuseable.ASYNC))
			.as("fusion mode ASYNC").isEqualTo(Fuseable.ASYNC);

	assertThat(fuseableSubscriber.requestFusion(Fuseable.NONE))
			.as("fusion mode NONE").isEqualTo(Fuseable.NONE);
}
 
Example 2
Source File: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void queueClearEmptySizeDelegates() {
	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, Clock.SYSTEM, "foo", Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);
	assertThat(testQueue.size()).isEqualTo(1);

	fuseableSubscriber.onSubscribe(testQueue);

	assertThat(fuseableSubscriber.isEmpty()).as("isEmpty").isFalse();
	assertThat(fuseableSubscriber.size()).as("size").isEqualTo(1);

	fuseableSubscriber.clear();

	assertThat(testQueue.size()).as("original queue impacted").isZero();
	assertThat(fuseableSubscriber.size()).as("size after clear").isEqualTo(0);
}
 
Example 3
Source File: ParallelLiftFuseable.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Override
protected void subscribe(CoreSubscriber<? super O>[] s) {
	@SuppressWarnings("unchecked") CoreSubscriber<? super I>[] subscribers =
			new CoreSubscriber[parallelism()];

	int i = 0;
	while (i < subscribers.length) {
		CoreSubscriber<? super O> actual = s[i];
		CoreSubscriber<? super I> converted =
				Objects.requireNonNull(lifter.apply(source, actual),
						"Lifted subscriber MUST NOT be null");

		Objects.requireNonNull(converted, "Lifted subscriber MUST NOT be null");

		if (actual instanceof Fuseable.QueueSubscription
				&& !(converted instanceof QueueSubscription)) {
			//user didn't produce a QueueSubscription, original was one
			converted = new FluxHide.SuppressFuseableSubscriber<>(converted);
		}
		//otherwise QS is not required or user already made a compatible conversion
		subscribers[i] = converted;
		i++;
	}

	source.subscribe(subscribers);
}
 
Example 4
Source File: SignalLogger.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
/**
 * Structured logging with level adaptation and operator ascii graph if required +
 * protection against loggers that detect objects like {@link Fuseable.QueueSubscription}
 * as {@link java.util.Collection} and attempt to use their iterator for logging.
 *
 * @see #log
 */
void safeLog(SignalType signalType, Object signalValue) {
	if (signalValue instanceof Fuseable.QueueSubscription) {
		signalValue = String.valueOf(signalValue);
		if (log.isDebugEnabled()) {
			log.debug("A Fuseable Subscription has been passed to the logging framework, this is generally a sign of a misplaced log(), " +
					"eg. 'window(2).log()' instead of 'window(2).flatMap(w -> w.log())'");
		}
	}
	try {
		log(signalType, signalValue);
	}
	catch (UnsupportedOperationException uoe) {
		log(signalType, String.valueOf(signalValue));
		if (log.isDebugEnabled()) {
			log.debug("UnsupportedOperationException has been raised by the logging framework, does your log() placement make sense? " +
					"eg. 'window(2).log()' instead of 'window(2).flatMap(w -> w.log())'", uoe);
		}
	}
}
 
Example 5
Source File: SignalLogger.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
static String subscriptionAsString(@Nullable Subscription s) {
	if (s == null) {
		return "null subscription";
	}
	StringBuilder asString = new StringBuilder();
	if (s instanceof Fuseable.SynchronousSubscription) {
		asString.append("[Synchronous Fuseable] ");
	}
	else if (s instanceof Fuseable.QueueSubscription) {
		asString.append("[Fuseable] ");
	}

	Class<? extends Subscription> clazz = s.getClass();
	String name = clazz.getCanonicalName();
	if (name == null) {
		name = clazz.getName();
	}
	name = name.replaceFirst(clazz.getPackage()
	                              .getName() + ".", "");
	asString.append(name);

	return asString.toString();
}
 
Example 6
Source File: FluxIndexedFuseableTest.java    From reactor-core with Apache License 2.0 6 votes vote down vote up
@Test
public void fusionThreadBarrierConditionalMapperDoesFuse() {
	@SuppressWarnings("unchecked") Fuseable.ConditionalSubscriber<Tuple2<String, String>> cs =
			Mockito.mock(Fuseable.ConditionalSubscriber.class);
	@SuppressWarnings("unchecked") Fuseable.QueueSubscription<String> qs =
			Mockito.mock(Fuseable.QueueSubscription.class);
	Mockito.when(qs.requestFusion(Mockito.anyInt()))
	       .thenAnswer((Answer<Integer>) mock -> {
		       int requestedMode = mock.getArgument(0);
		       if ((requestedMode & Fuseable.THREAD_BARRIER) != 0) {
			       return requestedMode - Fuseable.THREAD_BARRIER;
		       }
		       return requestedMode;
	       });

	@SuppressWarnings("unchecked") FluxIndexFuseable.IndexFuseableConditionalSubscriber test =
			new FluxIndexFuseable.IndexFuseableConditionalSubscriber<>(cs, Flux.TUPLE2_BIFUNCTION);

	test.onSubscribe(qs);

	int mode = test.requestFusion(Fuseable.SYNC | Fuseable.THREAD_BARRIER);
	assertThat(mode).as("SYNC").isEqualTo(Fuseable.SYNC);

	mode = test.requestFusion(Fuseable.ASYNC | Fuseable.THREAD_BARRIER);
	assertThat(mode).as("ASYNC").isEqualTo(Fuseable.ASYNC);
}
 
Example 7
Source File: MonoMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollSyncTracksOnComplete() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	clock.add(Duration.ofMillis(123));
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_COMPLETE))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(200);
}
 
Example 8
Source File: FluxConcatMap.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.validate(this.s, s)) {
		this.s = s;

		if (s instanceof Fuseable.QueueSubscription) {
			@SuppressWarnings("unchecked") Fuseable.QueueSubscription<T> f =
					(Fuseable.QueueSubscription<T>) s;

			int m = f.requestFusion(Fuseable.ANY | Fuseable.THREAD_BARRIER);

			if (m == Fuseable.SYNC) {
				sourceMode = Fuseable.SYNC;
				queue = f;
				done = true;

				actual.onSubscribe(this);

				drain();
				return;
			}
			else if (m == Fuseable.ASYNC) {
				sourceMode = Fuseable.ASYNC;
				queue = f;
			}
			else {
				queue = queueSupplier.get();
			}
		}
		else {
			queue = queueSupplier.get();
		}

		actual.onSubscribe(this);

		s.request(Operators.unboundedOrPrefetch(prefetch));
	}
}
 
Example 9
Source File: FluxConcatMap.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.validate(this.s, s)) {
		this.s = s;

		if (s instanceof Fuseable.QueueSubscription) {
			@SuppressWarnings("unchecked") Fuseable.QueueSubscription<T> f =
					(Fuseable.QueueSubscription<T>) s;
			int m = f.requestFusion(Fuseable.ANY | Fuseable.THREAD_BARRIER);
			if (m == Fuseable.SYNC) {
				sourceMode = Fuseable.SYNC;
				queue = f;
				done = true;

				actual.onSubscribe(this);

				drain();
				return;
			}
			else if (m == Fuseable.ASYNC) {
				sourceMode = Fuseable.ASYNC;
				queue = f;
			}
			else {
				queue = queueSupplier.get();
			}
		}
		else {
			queue = queueSupplier.get();
		}

		actual.onSubscribe(this);

		s.request(Operators.unboundedOrPrefetch(prefetch));
	}
}
 
Example 10
Source File: EmitterProcessor.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(final Subscription s) {
	if (Operators.setOnce(S, this, s)) {
		if (s instanceof Fuseable.QueueSubscription) {
			@SuppressWarnings("unchecked") Fuseable.QueueSubscription<T> f =
					(Fuseable.QueueSubscription<T>) s;

			int m = f.requestFusion(Fuseable.ANY);
			if (m == Fuseable.SYNC) {
				sourceMode = m;
				queue = f;
				drain();
				return;
			}
			else if (m == Fuseable.ASYNC) {
				sourceMode = m;
				queue = f;
				s.request(Operators.unboundedOrPrefetch(prefetch));
				return;
			}
		}

		queue = Queues.<T>get(prefetch).get();

		s.request(Operators.unboundedOrPrefetch(prefetch));
	}
}
 
Example 11
Source File: GroupedLiftFuseable.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void subscribe(CoreSubscriber<? super O> actual) {
	CoreSubscriber<? super I> input =
			lifter.apply(source, actual);

	Objects.requireNonNull(input, "Lifted subscriber MUST NOT be null");

	if (actual instanceof Fuseable.QueueSubscription
			&& !(input instanceof QueueSubscription)) {
		//user didn't produce a QueueSubscription, original was one
		input = new FluxHide.SuppressFuseableSubscriber<>(input);
	}
	//otherwise QS is not required or user already made a compatible conversion
	source.subscribe(input);
}
 
Example 12
Source File: ParallelSource.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.validate(this.s, s)) {
		this.s = s;

		if (s instanceof Fuseable.QueueSubscription) {
			@SuppressWarnings("unchecked")
			Fuseable.QueueSubscription<T> qs = (Fuseable.QueueSubscription<T>) s;
			
			int m = qs.requestFusion(Fuseable.ANY | Fuseable.THREAD_BARRIER);
			
			if (m == Fuseable.SYNC) {
				sourceMode = m;
				queue = qs;
				done = true;
				setupSubscribers();
				drain();
				return;
			} else
			if (m == Fuseable.ASYNC) {
				sourceMode = m;
				queue = qs;
				
				setupSubscribers();
				
				s.request(Operators.unboundedOrPrefetch(prefetch));
				
				return;
			}
		}
		
		queue = queueSupplier.get();
		
		setupSubscribers();
		
		s.request(Operators.unboundedOrPrefetch(prefetch));
	}
}
 
Example 13
Source File: FluxPublish.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Override
public void onSubscribe(Subscription s) {
	if (Operators.setOnce(S, this, s)) {
		if (s instanceof Fuseable.QueueSubscription) {
			@SuppressWarnings("unchecked") Fuseable.QueueSubscription<T> f =
					(Fuseable.QueueSubscription<T>) s;

			int m = f.requestFusion(Fuseable.ANY | Fuseable.THREAD_BARRIER);
			if (m == Fuseable.SYNC) {
				sourceMode = m;
				queue = f;
				drain();
				return;
			}
			if (m == Fuseable.ASYNC) {
				sourceMode = m;
				queue = f;
				s.request(Operators.unboundedOrPrefetch(prefetch));
				return;
			}
		}

		queue = parent.queueSupplier.get();

		s.request(Operators.unboundedOrPrefetch(prefetch));
	}
}
 
Example 14
Source File: RequestOperator.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void onSubscribe(Subscription s) {
  if (Operators.validate(this.s, s)) {
    this.s = s;
    if (s instanceof Fuseable.QueueSubscription) {
      this.qs = (Fuseable.QueueSubscription<Payload>) s;
    }
    this.actual.onSubscribe(this);
  }
}
 
Example 15
Source File: ReactorSubscriberAndClientProducer.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected Subscription fuse(Subscription s) {
    if (s instanceof Fuseable.QueueSubscription) {
        @SuppressWarnings("unchecked")
        Fuseable.QueueSubscription<T> f = (Fuseable.QueueSubscription<T>) s;

        int m = f.requestFusion(Fuseable.ANY);

        if (m != Fuseable.NONE) {
            return new FusionAwareQueueSubscriptionAdapter<T>(f, m);
        }
    }

    return s;
}
 
Example 16
Source File: FluxMetricsFuseableTest.java    From reactor-core with Apache License 2.0 5 votes vote down vote up
@Test
public void queuePollSyncTracksOnComplete() {
	//prepare registry with mock clock
	MockClock clock = new MockClock();
	removeRegistry();
	registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
	Metrics.globalRegistry.add(registry);

	AssertSubscriber<Integer> testSubscriber = AssertSubscriber.create();
	FluxMetricsFuseable.MetricsFuseableSubscriber<Integer> fuseableSubscriber =
			new FluxMetricsFuseable.MetricsFuseableSubscriber<>(testSubscriber,
					registry, clock, "foo", Tags.empty());

	Fuseable.QueueSubscription<Integer> testQueue = new FluxPeekFuseableTest.AssertQueueSubscription<>();
	testQueue.offer(1);

	fuseableSubscriber.onSubscribe(testQueue);
	fuseableSubscriber.requestFusion(Fuseable.SYNC);

	clock.add(Duration.ofMillis(200));
	Integer val1 = fuseableSubscriber.poll();
	clock.add(Duration.ofMillis(123));
	Integer val2 = fuseableSubscriber.poll();

	assertThat(val1).isEqualTo(1);
	assertThat(val2).isNull();

	//test meters
	Timer terminationTimer = registry.find(METER_FLOW_DURATION)
	                          .tags(Tags.of(TAG_ON_COMPLETE))
	                          .timer();

	assertThat(terminationTimer).isNotNull();
	assertThat(terminationTimer.max(TimeUnit.MILLISECONDS)).as("terminate max delay").isEqualTo(323);
}
 
Example 17
Source File: FusionAwareQueueSubscriptionAdapter.java    From reactive-grpc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
FusionAwareQueueSubscriptionAdapter(Fuseable.QueueSubscription<T> delegate, int mode) {
    this.delegate = delegate;
    this.mode = mode;
}
 
Example 18
Source File: SignalLoggerTests.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Test
public void queueSubscriptionAsString() {
	Fuseable.QueueSubscription<Object> s = Operators.EmptySubscription.INSTANCE;

	assertThat(SignalLogger.subscriptionAsString(s)).isEqualTo("[Fuseable] Operators.EmptySubscription");
}
 
Example 19
Source File: AssertSubscriber.java    From RHub with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void onSubscribe(Subscription s) {
    subscriptionCount++;
    int requestMode = requestedFusionMode;
    if (requestMode >= 0) {
        if (!setWithoutRequesting(s)) {
            if (!isCancelled()) {
                errors.add(new IllegalStateException("Subscription already push: " +
                        subscriptionCount));
            }
        } else {
            if (s instanceof Fuseable.QueueSubscription) {
                this.qs = (Fuseable.QueueSubscription<T>)s;

                int m = qs.requestFusion(requestMode);
                establishedFusionMode = m;

                if (m == Fuseable.SYNC) {
                    for (;;) {
                        T v = qs.poll();
                        if (v == null) {
                            onComplete();
                            break;
                        }

                        onNext(v);
                    }
                }
                else {
                    requestDeferred();
                }
            }
            else {
                requestDeferred();
            }
        }
    } else {
        if (!set(s)) {
            if (!isCancelled()) {
                errors.add(new IllegalStateException("Subscription already push: " +
                        subscriptionCount));
            }
        }
    }
}
 
Example 20
Source File: AssertSubscriber.java    From reactor-core with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void onSubscribe(Subscription s) {
	subscriptionCount++;
	int requestMode = requestedFusionMode;
	if (requestMode >= 0) {
		if (!setWithoutRequesting(s)) {
			if (!isCancelled()) {
				errors.add(new IllegalStateException("Subscription already set: " +
						subscriptionCount));
			}
		} else {
			if (s instanceof Fuseable.QueueSubscription) {
				this.qs = (Fuseable.QueueSubscription<T>)s;

				int m = qs.requestFusion(requestMode);
				establishedFusionMode = m;

				if (m == Fuseable.SYNC) {
					for (;;) {
						T v = qs.poll();
						if (v == null) {
							onComplete();
							break;
						}

						onNext(v);
					}
				}
				else {
					requestDeferred();
				}
			}
			else {
				requestDeferred();
			}
		}
	} else {
		if (!set(s)) {
			if (!isCancelled()) {
				errors.add(new IllegalStateException("Subscription already set: " +
						subscriptionCount));
			}
		}
	}
}