001/************************************************************************
002 * Licensed under Public Domain (CC0)                                    *
003 *                                                                       *
004 * To the extent possible under law, the person who associated CC0 with  *
005 * this code has waived all copyright and related or neighboring         *
006 * rights to this code.                                                  *
007 *                                                                       *
008 * You should have received a copy of the CC0 legalcode along with this  *
009 * work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.*
010 ************************************************************************/
011
012package org.reactivestreams.tck.flow;
013
014import org.reactivestreams.Publisher;
015import org.reactivestreams.FlowAdapters;
016import org.reactivestreams.tck.PublisherVerification;
017import org.reactivestreams.tck.TestEnvironment;
018
019import java.util.concurrent.Flow;
020
021/**
022 * Provides tests for verifying a Java 9+ {@link java.util.concurrent.Flow.Publisher} specification rules.
023 *
024 * @see java.util.concurrent.Flow.Publisher
025 */
026public abstract class FlowPublisherVerification<T> extends PublisherVerification<T> {
027
028  public FlowPublisherVerification(TestEnvironment env, long publisherReferenceGCTimeoutMillis) {
029    super(env, publisherReferenceGCTimeoutMillis);
030  }
031
032  public FlowPublisherVerification(TestEnvironment env) {
033    super(env);
034  }
035
036  @Override
037  final public Publisher<T> createPublisher(long elements) {
038    final Flow.Publisher<T> flowPublisher = createFlowPublisher(elements);
039    return FlowAdapters.toPublisher(flowPublisher);
040  }
041  /**
042   * This is the main method you must implement in your test incarnation.
043   * It must create a Publisher for a stream with exactly the given number of elements.
044   * If `elements` is `Long.MAX_VALUE` the produced stream must be infinite.
045   */
046  public abstract Flow.Publisher<T> createFlowPublisher(long elements);
047
048  @Override
049  final public Publisher<T> createFailedPublisher() {
050    final Flow.Publisher<T> failed = createFailedFlowPublisher();
051    if (failed == null) return null; // because `null` means "SKIP" in createFailedPublisher
052    else return FlowAdapters.toPublisher(failed);
053  }
054  /**
055   * By implementing this method, additional TCK tests concerning a "failed" publishers will be run.
056   *
057   * The expected behaviour of the {@link Flow.Publisher} returned by this method is hand out a subscription,
058   * followed by signalling {@code onError} on it, as specified by Rule 1.9.
059   *
060   * If you ignore these additional tests, return {@code null} from this method.
061   */
062  public abstract Flow.Publisher<T> createFailedFlowPublisher();
063}