|
23 | 23 | import static org.mockito.ArgumentMatchers.same; |
24 | 24 | import static org.mockito.Mockito.mock; |
25 | 25 | import static org.mockito.Mockito.never; |
| 26 | +import static org.mockito.Mockito.times; |
26 | 27 | import static org.mockito.Mockito.verify; |
27 | 28 |
|
28 | 29 | import com.google.common.util.concurrent.MoreExecutors; |
@@ -256,6 +257,55 @@ public Void answer(org.mockito.invocation.InvocationOnMock invocation) { |
256 | 257 | verify(mockRealCall).cancel(eq("Failed to drain pending calls"), same(error)); |
257 | 258 | } |
258 | 259 |
|
| 260 | + @Test |
| 261 | + public void drainPendingCallbacksFails() { |
| 262 | + DelayedClientCall<String, Integer> delayedClientCall = |
| 263 | + new DelayedClientCall<>(callExecutor, fakeClock.getScheduledExecutorService(), null); |
| 264 | + delayedClientCall.start(listener, new Metadata()); |
| 265 | + |
| 266 | + final RuntimeException error = new RuntimeException("fail"); |
| 267 | + org.mockito.Mockito.doAnswer(new org.mockito.stubbing.Answer<Void>() { |
| 268 | + @Override |
| 269 | + public Void answer(org.mockito.invocation.InvocationOnMock invocation) { |
| 270 | + throw error; |
| 271 | + } |
| 272 | + }).when(listener).onReady(); |
| 273 | + |
| 274 | + final AtomicReference<ClientCall.Listener<Integer>> listenerCaptor = new AtomicReference<>(); |
| 275 | + org.mockito.Mockito.doAnswer(new org.mockito.stubbing.Answer<Void>() { |
| 276 | + @Override |
| 277 | + public Void answer(org.mockito.invocation.InvocationOnMock invocation) { |
| 278 | + ClientCall.Listener<Integer> delayedListener = invocation.getArgument(0); |
| 279 | + listenerCaptor.set(delayedListener); |
| 280 | + delayedListener.onReady(); |
| 281 | + return null; |
| 282 | + } |
| 283 | + }).when(mockRealCall).start(any(ClientCall.Listener.class), any(Metadata.class)); |
| 284 | + |
| 285 | + Runnable runnable = delayedClientCall.setCall(mockRealCall); |
| 286 | + assertThat(runnable).isNotNull(); |
| 287 | + |
| 288 | + try { |
| 289 | + runnable.run(); |
| 290 | + org.junit.Assert.fail("Should have thrown"); |
| 291 | + } catch (RuntimeException e) { |
| 292 | + assertThat(e).isSameInstanceAs(error); |
| 293 | + } |
| 294 | + |
| 295 | + ClientCall.Listener<Integer> delayedListener = listenerCaptor.get(); |
| 296 | + assertThat(delayedListener).isNotNull(); |
| 297 | + |
| 298 | + // Verify it transitioned to passThrough by showing it forwards. |
| 299 | + try { |
| 300 | + delayedListener.onReady(); |
| 301 | + org.junit.Assert.fail("Should have thrown"); |
| 302 | + } catch (RuntimeException e) { |
| 303 | + assertThat(e).isSameInstanceAs(error); |
| 304 | + } |
| 305 | + // Verify it was called twice (once during drain, once just now) |
| 306 | + verify(listener, times(2)).onReady(); |
| 307 | + } |
| 308 | + |
259 | 309 | private void callMeMaybe(Runnable r) { |
260 | 310 | if (r != null) { |
261 | 311 | r.run(); |
|
0 commit comments