Spring / Spring gRPC Interview Questions
How do you assert on streaming responses in a test without blocking indefinitely?
The key is to bound how long the test is willing to wait. A CountDownLatch initialized to 1 is counted down inside the test observer's onCompleted() (and typically also on onError(), capturing the error for later assertion), and the test calls latch.await(timeout, TimeUnit.SECONDS) with a real timeout rather than an unbounded wait.
Received items are accumulated into a thread-safe collection (a synchronized list or a BlockingQueue) inside onNext, and once the latch releases (or the timeout expires and the test fails explicitly), assertions run against the collected items and the terminal state (completed vs errored) - ensuring a genuinely stuck stream produces a clear test failure instead of a hung build.
More Related questions...