Class SourceTestUtils

    • Constructor Detail

      • SourceTestUtils

        public SourceTestUtils()
    • Method Detail

      • createStructuralValues

        public static <T> java.util.List<org.apache.beam.sdk.testing.SourceTestUtils.ReadableStructuralValue<T>> createStructuralValues​(Coder<T> coder,
                                                                                                                                        java.util.List<T> list)
                                                                                                                                 throws java.lang.Exception
        Testing utilities below depend on standard assertions and matchers to compare elements read by sources. In general the elements may not implement equals/hashCode properly, however every source has a Coder and every Coder can produce a Coder.structuralValue(T) whose equals/hashCode is consistent with equality of encoded format. So we use this Coder.structuralValue(T) to compare elements read by sources.
        Throws:
        java.lang.Exception
      • readFromSource

        public static <T> java.util.List<T> readFromSource​(BoundedSource<T> source,
                                                           PipelineOptions options)
                                                    throws java.io.IOException
        Reads all elements from the given BoundedSource.
        Throws:
        java.io.IOException
      • readFromSplitsOfSource

        public static <T> java.util.List<T> readFromSplitsOfSource​(BoundedSource<T> source,
                                                                   long desiredBundleSizeBytes,
                                                                   PipelineOptions options)
                                                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readFromUnstartedReader

        public static <T> java.util.List<T> readFromUnstartedReader​(Source.Reader<T> reader)
                                                             throws java.io.IOException
        Reads all elements from the given unstarted Source.Reader.
        Throws:
        java.io.IOException
      • readFromStartedReader

        public static <T> java.util.List<T> readFromStartedReader​(Source.Reader<T> reader)
                                                           throws java.io.IOException
        Reads all elements from the given started Source.Reader.
        Throws:
        java.io.IOException
      • readNItemsFromUnstartedReader

        public static <T> java.util.List<T> readNItemsFromUnstartedReader​(Source.Reader<T> reader,
                                                                          int n)
                                                                   throws java.io.IOException
        Read elements from a Source.Reader until n elements are read.
        Throws:
        java.io.IOException
      • readNItemsFromStartedReader

        public static <T> java.util.List<T> readNItemsFromStartedReader​(Source.Reader<T> reader,
                                                                        int n)
                                                                 throws java.io.IOException
        Read elements from a Source.Reader that has already had Source.Reader.start() called on it, until n elements are read.
        Throws:
        java.io.IOException
      • readRemainingFromReader

        public static <T> java.util.List<T> readRemainingFromReader​(Source.Reader<T> reader,
                                                                    boolean started)
                                                             throws java.io.IOException
        Read all remaining elements from a Source.Reader.
        Throws:
        java.io.IOException
      • assertSourcesEqualReferenceSource

        public static <T> void assertSourcesEqualReferenceSource​(BoundedSource<T> referenceSource,
                                                                 java.util.List<? extends BoundedSource<T>> sources,
                                                                 PipelineOptions options)
                                                          throws java.lang.Exception
        Given a reference Source and a list of Sources, assert that the union of the records read from the list of sources is equal to the records read from the reference source.
        Throws:
        java.lang.Exception
      • assertUnstartedReaderReadsSameAsItsSource

        public static <T> void assertUnstartedReaderReadsSameAsItsSource​(BoundedSource.BoundedReader<T> reader,
                                                                         PipelineOptions options)
                                                                  throws java.lang.Exception
        Assert that a Reader returns a Source that, when read from, produces the same records as the reader.
        Throws:
        java.lang.Exception
      • assertSplitAtFractionSucceedsAndConsistent

        public static <T> void assertSplitAtFractionSucceedsAndConsistent​(BoundedSource<T> source,
                                                                          int numItemsToReadBeforeSplit,
                                                                          double splitFraction,
                                                                          PipelineOptions options)
                                                                   throws java.lang.Exception
        Verifies some consistency properties of BoundedSource.BoundedReader.splitAtFraction(double) on the given source. Equivalent to the following pseudocode:
           Reader reader = source.createReader();
           read N items from reader;
           Source residual = reader.splitAtFraction(splitFraction);
           Source primary = reader.getCurrentSource();
           assert: items in primary == items we read so far
                                       + items we'll get by continuing to read from reader;
           assert: items in original source == items in primary + items in residual
         
        Throws:
        java.lang.Exception
      • assertSplitAtFractionFails

        public static <T> void assertSplitAtFractionFails​(BoundedSource<T> source,
                                                          int numItemsToReadBeforeSplit,
                                                          double splitFraction,
                                                          PipelineOptions options)
                                                   throws java.lang.Exception
        Asserts that the source's reader fails to splitAtFraction(fraction) after reading numItemsToReadBeforeSplit items.
        Throws:
        java.lang.Exception
      • assertSplitAtFractionExhaustive

        public static <T> void assertSplitAtFractionExhaustive​(BoundedSource<T> source,
                                                               PipelineOptions options)
                                                        throws java.lang.Exception
        Asserts that for each possible start position, BoundedSource.BoundedReader.splitAtFraction(double) at every interesting fraction (halfway between two fractions that differ by at least one item) can be called successfully and the results are consistent if a split succeeds. Verifies multithreaded splitting as well.
        Throws:
        java.lang.Exception