Class FlowPublisherConcurrencyDetector

java.lang.Object
se.deversity.asynctest.diagnostics.FlowPublisherConcurrencyDetector

public final class FlowPublisherConcurrencyDetector extends Object
Detects reactive-streams contract violations on 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
  • 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 under label so later findings read as the test author named them.
      Parameters:
      subscriber - the subscriber instance (null-safe)
      label - human-readable label for triage (may be null)
      thread - the thread delivering onSubscribe
    • recordRequest

      public void recordRequest(@Nullable Object subscriber, long n)
      Record a Subscription.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

      public void recordNextStart(@Nullable Object subscriber, @Nullable Thread thread)
      Record entry into onNext. Pair with recordNextEnd(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

      public void recordNextEnd(@Nullable Object subscriber)
      Record exit from onNext. 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

      public void recordComplete(@Nullable Object subscriber, @Nullable Thread thread)
      Record a terminal onComplete. 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

      public void recordError(@Nullable Object subscriber, @Nullable Thread thread)
      Record a terminal onError. 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.