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:
io.grpc.stub.StreamObserver<ResponseT>
public class FakeResponseObserver<ResponseT> extends io.grpc.stub.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 useio.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.FailureTrackingThreadPoolExecutor
supplied to the constructor.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FakeResponseObserver.FailureTrackingThreadPoolExecutor
Tracks task scheduling failures, occurring mainly on attempts to execute a task after the executor was shutdown.
-
Field Summary
Fields Modifier and Type Field Description boolean
failOnMultipleFinalizations
Should an AssertionError 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.FailureTrackingThreadPoolExecutor 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 or timeout exceeds.void
callWithinListenerLock(Consumer<io.grpc.stub.StreamObserver<ResponseT>> unaryClientMethod)
CallsunaryClientMethod
within listener's lock.<RequestT> void
callWithinListenerLock(Function<io.grpc.stub.StreamObserver<ResponseT>,io.grpc.stub.StreamObserver<RequestT>> streamingClientMethod, Consumer<io.grpc.stub.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)
.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 AssertionError be thrown immediately upon second finalization. By defaultfalse
.
-
-
Constructor Detail
-
FakeResponseObserver
public FakeResponseObserver(FakeResponseObserver.FailureTrackingThreadPoolExecutor 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<io.grpc.stub.StreamObserver<ResponseT>> unaryClientMethod)
CallsunaryClientMethod
within listener's lock.
-
callWithinListenerLock
public <RequestT> void callWithinListenerLock(Function<io.grpc.stub.StreamObserver<ResponseT>,io.grpc.stub.StreamObserver<RequestT>> streamingClientMethod, Consumer<io.grpc.stub.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 classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
setOnReadyHandler
public void setOnReadyHandler(Runnable onReadyHandler)
- Specified by:
setOnReadyHandler
in classio.grpc.stub.ServerCallStreamObserver<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 or timeout exceeds.- Throws:
InterruptedException
-
disableAutoRequest
public void disableAutoRequest()
- Overrides:
disableAutoRequest
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
disableAutoInboundFlowControl
public void disableAutoInboundFlowControl()
- Specified by:
disableAutoInboundFlowControl
in classio.grpc.stub.CallStreamObserver<ResponseT>
-
request
public void request(int count)
- Specified by:
request
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
cancel
public void cancel()
Simulates client canceling a call by a client.
-
isCancelled
public boolean isCancelled()
- Specified by:
isCancelled
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
setOnCancelHandler
public void setOnCancelHandler(Runnable onCancelHandler)
- Specified by:
setOnCancelHandler
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
setCompression
public void setCompression(String compression)
- Specified by:
setCompression
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
setMessageCompression
public void setMessageCompression(boolean enable)
- Specified by:
setMessageCompression
in classio.grpc.stub.ServerCallStreamObserver<ResponseT>
-
getLogger
public static Logger getLogger()
-
-