Class FlowPublisherConcurrencyDetector
java.util.concurrent.Flow
subscribers: overlapping onNext delivery, signals after a terminal signal,
and deliveries exceeding recorded demand.
The Flow API inherits the reactive-streams specification: signals to a
Subscriber must be serialized (rule 1.3), at most one terminal signal may be
delivered and nothing after it (rule 1.7), and a publisher must not deliver more
onNext signals than the subscriber has requested (rule 1.1). A hand-rolled
Publisher that fans work out to an executor breaks rule 1.3 first: two threads
inside onNext at once corrupt any non-thread-safe subscriber state, exactly the
class of bug SubmissionPublisher's per-subscriber serialization exists to
prevent.
Overlap is observed, not inferred: recordNextStart(java.lang.Object, java.lang.Thread) and
recordNextEnd(java.lang.Object) bracket each delivery, and the high-water mark of concurrent
in-flight deliveries per subscriber is the finding. Demand violations are reported at
MEDIUM with conditional wording, because the detector can only compare recorded
deliveries against recorded requests — an unrecorded request() call would look
like an overrun. No demand finding is emitted when no request was ever recorded.
Usage:
var d = new FlowPublisherConcurrencyDetector();
d.recordSubscribe(subscriber, "priceFeed", Thread.currentThread());
d.recordRequest(subscriber, 1);
d.recordNextStart(subscriber, Thread.currentThread());
// ... subscriber.onNext(item) ...
d.recordNextEnd(subscriber);
d.recordComplete(subscriber, Thread.currentThread());
- Since:
- 1.7.1
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionanalyze()Evaluate the observed state and produce a report.voidrecordComplete(@Nullable Object subscriber, @Nullable Thread thread) Record a terminalonComplete.voidrecordError(@Nullable Object subscriber, @Nullable Thread thread) Record a terminalonError.voidrecordNextEnd(@Nullable Object subscriber) Record exit fromonNext.voidrecordNextStart(@Nullable Object subscriber, @Nullable Thread thread) Record entry intoonNext.voidrecordRequest(@Nullable Object subscriber, long n) Record aSubscription.request(n)call.voidrecordSubscribe(@Nullable Object subscriber, @Nullable String label, @Nullable Thread thread) Record a subscription.
-
Constructor Details
-
FlowPublisherConcurrencyDetector
public FlowPublisherConcurrencyDetector()
-
-
Method Details
-
recordSubscribe
public void recordSubscribe(@Nullable Object subscriber, @Nullable String label, @Nullable Thread thread) Record a subscription. Registers the subscriber underlabelso later findings read as the test author named them.- Parameters:
subscriber- the subscriber instance (null-safe)label- human-readable label for triage (may benull)thread- the thread deliveringonSubscribe
-
recordRequest
Record aSubscription.request(n)call. Once any demand has been recorded,analyze()compares total deliveries against total demand.- Parameters:
subscriber- the subscriber whose subscription requested (null-safe)n- the requested amount
-
recordNextStart
Record entry intoonNext. Pair withrecordNextEnd(java.lang.Object); the bracket is what makes overlapping delivery observable rather than inferred.- Parameters:
subscriber- the subscriber receiving the signal (null-safe)thread- the delivering thread
-
recordNextEnd
Record exit fromonNext. Unbalanced calls floor at zero rather than going negative, so a missing bracket cannot manufacture an overlap.- Parameters:
subscriber- the subscriber whose delivery completed (null-safe)
-
recordComplete
Record a terminalonComplete. A second terminal signal, or any signal after this one, counts against rule 1.7.- Parameters:
subscriber- the subscriber completing (null-safe)thread- the delivering thread
-
recordError
Record a terminalonError. A second terminal signal, or any signal after this one, counts against rule 1.7.- Parameters:
subscriber- the subscriber erroring (null-safe)thread- the delivering thread
-
analyze
Evaluate the observed state and produce a report. Must be idempotent: calling it N times on quiescent state yields N identical reports.
-