Package pl.morgwai.base.grpc.utils
Class FakeResponseObserver<ResponseT>
- java.lang.Object
-
- io.grpc.stub.CallStreamObserver<V>
-
- io.grpc.stub.ServerCallStreamObserver<ResponseT>
-
- pl.morgwai.base.grpc.utils.FakeResponseObserver<ResponseT>
-
- All Implemented Interfaces:
StreamObserver<ResponseT>
public class FakeResponseObserver<ResponseT> extends ServerCallStreamObserver<ResponseT>
A fakeServerCallStreamObserver
testing helper class. Helps to emulate behavior of a client and the gRPC system in server-side infrastructure code tests.Note: in most cases it is better to use
io.grpc.inprocess.InProcessChannelBuilder
for testing gRPC methods. This class is mainly intended for testing infrastructure parts.Usage:
- Configure observer's readiness by adjusting
outputBufferSize
andunreadyDurationMillis
variables. - Depending on client type (unary/streaming) of your gRPC method pass it to one of
callWithinListenerLock(Consumer)
,callWithinListenerLock(Function, Consumer)
methods. awaitFinalization(long)
can be used to wait untilonCompleted()
oronError(Throwable)
is called.- Client canceling can be simulated using
cancel()
method. - Results can be verified with
getOutputData()
,getFinalizedCount()
,getReportedError()
methods and by shutting down and inspectingFakeResponseObserver.LoggingExecutor
supplied to the constructor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FakeResponseObserver.LoggingExecutor
Logs task scheduling and executions and scheduling rejections.
-
Field Summary
Fields Modifier and Type Field Description boolean
failOnMultipleFinalizations
Should an IllegalStateException be thrown immediately upon second finalization.int
outputBufferSize
Response observer becomes unready after eachoutputBufferSize
messages are submitted to it.long
unreadyDurationMillis
Duration for which observer will be unready.
-
Constructor Summary
Constructors Constructor Description FakeResponseObserver(FakeResponseObserver.LoggingExecutor grpcInternalExecutor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitFinalization(long timeoutMillis)
Awaits until finalization (call to eitheronCompleted()
oronError(Throwable)
) occurs ortimoutMillis
passes.void
callWithinListenerLock(Consumer<StreamObserver<ResponseT>> unaryClientMethod)
CallsunaryClientMethod
within listener's lock.<RequestT> void
callWithinListenerLock(Function<StreamObserver<ResponseT>,StreamObserver<RequestT>> streamingClientMethod, Consumer<StreamObserver<RequestT>> requestProducer)
CallsstreamingClientMethod
within listener's lock and delivers request messages to returned request observer fromrequestProducer
.void
cancel()
Simulates client canceling a call by a client.void
disableAutoInboundFlowControl()
void
disableAutoRequest()
int
getFinalizedCount()
Count of calls toonCompleted()
andonError(Throwable)
.static Logger
getLogger()
List<ResponseT>
getOutputData()
List of arguments of calls toonNext(Object)
.static long
getRemainingMillis(long startMillis, long timeoutMillis)
Throwable
getReportedError()
Stored argument ofonError(Throwable)
.boolean
isCancelled()
boolean
isReady()
void
onCompleted()
void
onError(Throwable t)
void
onNext(ResponseT message)
void
request(int count)
void
runWithinListenerLock(Runnable handler)
For low level testing of onReady and onCancel handlers.void
setCompression(String compression)
void
setMessageCompression(boolean enable)
void
setOnCancelHandler(Runnable onCancelHandler)
void
setOnReadyHandler(Runnable onReadyHandler)
-
-
-
Field Detail
-
outputBufferSize
public volatile int outputBufferSize
Response observer becomes unready after eachoutputBufferSize
messages are submitted to it. Default is0
which means always ready.
-
unreadyDurationMillis
public volatile long unreadyDurationMillis
Duration for which observer will be unready. By default 1ms.
-
failOnMultipleFinalizations
public boolean failOnMultipleFinalizations
Should an IllegalStateException be thrown immediately upon second finalization. By defaulttrue
.
-
-
Constructor Detail
-
FakeResponseObserver
public FakeResponseObserver(FakeResponseObserver.LoggingExecutor grpcInternalExecutor)
- Parameters:
grpcInternalExecutor
- executor for gRPC internal tasks, such as marking response observer as ready, delivering requested messages etc. Its pool size should be not smaller than the number of requests concurrently processed by the code under test (usually determined by the argument of the initial call toServerCallStreamObserver.request(int)
.
-
-
Method Detail
-
getOutputData
public List<ResponseT> getOutputData()
List of arguments of calls toonNext(Object)
.
-
callWithinListenerLock
public void callWithinListenerLock(Consumer<StreamObserver<ResponseT>> unaryClientMethod)
CallsunaryClientMethod
within listener's lock.
-
callWithinListenerLock
public <RequestT> void callWithinListenerLock(Function<StreamObserver<ResponseT>,StreamObserver<RequestT>> streamingClientMethod, Consumer<StreamObserver<RequestT>> requestProducer)
CallsstreamingClientMethod
within listener's lock and delivers request messages to returned request observer fromrequestProducer
.- Parameters:
requestProducer
- dispatched togrpcInternalExecutor
wheneverrequest(int)
method is called. It should usually call its argument'sStreamObserver.onNext(Object)
possibly followed byStreamObserver.onCompleted()
orStreamObserver.onError(Throwable)
to simulate client's behavior. It may sleep arbitrarily long to simulate before the above calls to simulate client's or network delay.
-
runWithinListenerLock
public void runWithinListenerLock(Runnable handler)
For low level testing of onReady and onCancel handlers.
-
onNext
public void onNext(ResponseT message)
-
isReady
public boolean isReady()
- Specified by:
isReady
in classServerCallStreamObserver<ResponseT>
-
setOnReadyHandler
public void setOnReadyHandler(Runnable onReadyHandler)
- Specified by:
setOnReadyHandler
in classServerCallStreamObserver<ResponseT>
-
getFinalizedCount
public int getFinalizedCount()
Count of calls toonCompleted()
andonError(Throwable)
. Should be 1 at the end of positive test methods.
-
onCompleted
public void onCompleted()
-
onError
public void onError(Throwable t)
-
getReportedError
public Throwable getReportedError()
Stored argument ofonError(Throwable)
.
-
awaitFinalization
public void awaitFinalization(long timeoutMillis) throws InterruptedException
Awaits until finalization (call to eitheronCompleted()
oronError(Throwable)
) occurs ortimoutMillis
passes.- Throws:
RuntimeException
- iftimoutMillis
is exceeded.InterruptedException
-
getRemainingMillis
public static long getRemainingMillis(long startMillis, long timeoutMillis)
-
disableAutoRequest
public void disableAutoRequest()
- Overrides:
disableAutoRequest
in classServerCallStreamObserver<ResponseT>
-
disableAutoInboundFlowControl
public void disableAutoInboundFlowControl()
- Specified by:
disableAutoInboundFlowControl
in classCallStreamObserver<ResponseT>
-
request
public void request(int count)
- Specified by:
request
in classServerCallStreamObserver<ResponseT>
-
cancel
public void cancel()
Simulates client canceling a call by a client.
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in classServerCallStreamObserver<ResponseT>
-
setOnCancelHandler
public void setOnCancelHandler(Runnable onCancelHandler)
- Specified by:
setOnCancelHandler
in classServerCallStreamObserver<ResponseT>
-
setCompression
public void setCompression(String compression)
- Specified by:
setCompression
in classServerCallStreamObserver<ResponseT>
-
setMessageCompression
public void setMessageCompression(boolean enable)
- Specified by:
setMessageCompression
in classServerCallStreamObserver<ResponseT>
-
getLogger
public static Logger getLogger()
-
-