LocalDispatcher extends Thread { protected boolean dispatch() { try { final BallRequest request = mRequestQueue.take(); request.addMarker("local-queue-take"); if (request.isCanceled()) { request.finish("local-discard-canceled"); return true; } Object responseContent = request.getLocalRequestProcessor().getLocalResponse(); if (responseContent == null) { request.addMarker("local-response-content-null-exit"); mDelivery.postEmptyIntermediateResponse(request, BallResponse.ResponseSource.LOCAL); return true; } request.addMarker("local-response-get-content-successful"); BallResponse response = BallResponse.success(responseContent, null); response.setResponseSource(BallResponse.ResponseSource.LOCAL); response.setIntermediate(true); mDelivery.postResponse(request, response); } catch (InterruptedException e) { if (mQuit) { return false; } return true; } return true; } LocalDispatcher(BlockingQueue<BallRequest> requestQueue, BlockingQueue<BallRequest> networkQueue, BallResponseDelivery delivery); void quit(); @Override void run();  }
@Test public void shouldDoNothingWhenRequestIsCanceled() throws Exception { Mockito.when(mRequest.isCanceled()).thenReturn(true); Assertions.assertThat(mLocalDispatcher.dispatch()).isEqualTo(true); Mockito.verify(mRequestQueue).take(); Mockito.verify(mRequest).addMarker("local-queue-take"); Mockito.verify(mRequest).finish("local-discard-canceled"); Mockito.verifyZeroInteractions(mResponseDelivery); }
@Test public void shouldPostNoResponseWhenContentIsNull() throws Exception { Mockito.when(mLocalRequestProcessor.getLocalResponse()).thenReturn(null); Assertions.assertThat(mLocalDispatcher.dispatch()).isEqualTo(true); Mockito.verify(mRequestQueue).take(); Mockito.verify(mRequest).addMarker("local-queue-take"); Mockito.verify(mRequest).addMarker("local-response-content-null-exit"); Mockito.verify(mResponseDelivery).postEmptyIntermediateResponse(mRequest, BallResponse.ResponseSource.LOCAL); Mockito.verifyNoMoreInteractions(mResponseDelivery); }
@Test public void shouldPostResponse() throws Exception { final Object responseContent = new Object(); Mockito.when(mLocalRequestProcessor.getLocalResponse()).thenReturn(responseContent); Assertions.assertThat(mLocalDispatcher.dispatch()).isEqualTo(true); Mockito.verify(mRequestQueue).take(); Mockito.verify(mRequest).addMarker("local-queue-take"); Mockito.verify(mRequest).addMarker("local-response-get-content-successful"); Mockito.verify(mResponseDelivery).postResponse(Mockito.eq(mRequest), Mockito.argThat(new ArgumentMatcher<BallResponse<?>>() { @Override public boolean matches(Object argument) { if (argument instanceof BallResponse ) { BallResponse response = (BallResponse) argument; if (response.getResponseSource() == BallResponse.ResponseSource.LOCAL && response.isIntermediate() && response.getResult().equals(responseContent)) { return true; } } return false; } })); Mockito.verifyNoMoreInteractions(mResponseDelivery); }