Class StreamRecorder<T>

  • All Implemented Interfaces:
    io.grpc.stub.StreamObserver<T>

    public class StreamRecorder<T>
    extends java.lang.Object
    implements io.grpc.stub.StreamObserver<T>
    Utility implementation of StreamObserver used in testing. Records all the observed values produced by the stream as well as any errors.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Throwable error  
      private com.google.common.util.concurrent.SettableFuture<T> firstValue  
      private java.util.concurrent.CountDownLatch latch  
      private java.util.List<T> results  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StreamRecorder()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void awaitCompletion()
      Waits for the stream to terminate.
      boolean awaitCompletion​(int timeout, java.util.concurrent.TimeUnit unit)
      Waits a fixed timeout for the stream to terminate.
      static <T> StreamRecorder<T> create()
      Creates a new recorder.
      com.google.common.util.concurrent.ListenableFuture<T> firstValue()
      Returns a ListenableFuture for the first value received from the stream.
      java.lang.Throwable getError()
      Returns the stream terminating error.
      java.util.List<T> getValues()
      Returns the current set of received values.
      void onCompleted()  
      void onError​(java.lang.Throwable t)  
      void onNext​(T value)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • latch

        private final java.util.concurrent.CountDownLatch latch
      • results

        private final java.util.List<T> results
      • error

        private java.lang.Throwable error
      • firstValue

        private final com.google.common.util.concurrent.SettableFuture<T> firstValue
    • Constructor Detail

      • StreamRecorder

        private StreamRecorder()
    • Method Detail

      • create

        public static <T> StreamRecorder<T> create()
        Creates a new recorder.
      • onNext

        public void onNext​(T value)
        Specified by:
        onNext in interface io.grpc.stub.StreamObserver<T>
      • onError

        public void onError​(java.lang.Throwable t)
        Specified by:
        onError in interface io.grpc.stub.StreamObserver<T>
      • onCompleted

        public void onCompleted()
        Specified by:
        onCompleted in interface io.grpc.stub.StreamObserver<T>
      • awaitCompletion

        public void awaitCompletion()
                             throws java.lang.Exception
        Waits for the stream to terminate.
        Throws:
        java.lang.Exception
      • awaitCompletion

        public boolean awaitCompletion​(int timeout,
                                       java.util.concurrent.TimeUnit unit)
                                throws java.lang.Exception
        Waits a fixed timeout for the stream to terminate.
        Throws:
        java.lang.Exception
      • getValues

        public java.util.List<T> getValues()
        Returns the current set of received values.
      • getError

        @Nullable
        public java.lang.Throwable getError()
        Returns the stream terminating error.
      • firstValue

        public com.google.common.util.concurrent.ListenableFuture<T> firstValue()
        Returns a ListenableFuture for the first value received from the stream. Useful for testing unary call patterns.