001/***************************************************
002 * Licensed under MIT No Attribution (SPDX: MIT-0) *
003 ***************************************************/
004
005package org.reactivestreams.tck.flow;
006
007import org.reactivestreams.Publisher;
008import org.reactivestreams.FlowAdapters;
009import org.reactivestreams.tck.PublisherVerification;
010import org.reactivestreams.tck.TestEnvironment;
011
012import java.util.concurrent.Flow;
013
014/**
015 * Provides tests for verifying a Java 9+ {@link java.util.concurrent.Flow.Publisher} specification rules.
016 *
017 * @see java.util.concurrent.Flow.Publisher
018 */
019public abstract class FlowPublisherVerification<T> extends PublisherVerification<T> {
020
021  public FlowPublisherVerification(TestEnvironment env, long publisherReferenceGCTimeoutMillis) {
022    super(env, publisherReferenceGCTimeoutMillis);
023  }
024
025  public FlowPublisherVerification(TestEnvironment env) {
026    super(env);
027  }
028
029  @Override
030  final public Publisher<T> createPublisher(long elements) {
031    final Flow.Publisher<T> flowPublisher = createFlowPublisher(elements);
032    return FlowAdapters.toPublisher(flowPublisher);
033  }
034  /**
035   * This is the main method you must implement in your test incarnation.
036   * It must create a Publisher for a stream with exactly the given number of elements.
037   * If `elements` is `Long.MAX_VALUE` the produced stream must be infinite.
038   */
039  public abstract Flow.Publisher<T> createFlowPublisher(long elements);
040
041  @Override
042  final public Publisher<T> createFailedPublisher() {
043    final Flow.Publisher<T> failed = createFailedFlowPublisher();
044    if (failed == null) return null; // because `null` means "SKIP" in createFailedPublisher
045    else return FlowAdapters.toPublisher(failed);
046  }
047  /**
048   * By implementing this method, additional TCK tests concerning a "failed" publishers will be run.
049   *
050   * The expected behaviour of the {@link Flow.Publisher} returned by this method is hand out a subscription,
051   * followed by signalling {@code onError} on it, as specified by Rule 1.9.
052   *
053   * If you ignore these additional tests, return {@code null} from this method.
054   */
055  public abstract Flow.Publisher<T> createFailedFlowPublisher();
056}