ScribeEventRecorder implements EventRecorder { @VisibleForTesting void sendEvents() { if (mScribeRequestManager.isAtCapacity()) { return; } final List<BaseEvent> events = dequeueEvents(); if (events.isEmpty()) { return; } mScribeRequestManager.makeRequest( new ScribeRequest.ScribeRequestFactory() { @Override public ScribeRequest createRequest(ScribeRequest.Listener listener) { return new ScribeRequest(SCRIBE_URL, events, mEventSerializer, listener); } }, new ScribeBackoffPolicy() ); } ScribeEventRecorder(@NonNull Looper looper); @VisibleForTesting  ScribeEventRecorder(@NonNull EventSampler eventSampler,
            @NonNull Queue<BaseEvent> eventQueue,
            @NonNull EventSerializer eventSerializer,
            @NonNull ScribeRequestManager scribeRequestManager,
            @NonNull Handler handler); @Override void record(@NonNull BaseEvent baseEvent);  }
@Test public void sendEvents_shouldDequeueEvents_shouldAddRequestToScribeRequestManager() throws Exception { when(mockQueue.size()).thenReturn(1); when(mockQueue.peek()).thenReturn(mockEvent).thenReturn(null); when(mockQueue.poll()).thenReturn(mockEvent).thenReturn(null); doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ScribeRequest scribeRequest = ((ScribeRequest.ScribeRequestFactory) invocation.getArguments()[0]).createRequest(null); assertThat(scribeRequest.getUrl()).isEqualTo("https: assertThat(scribeRequest.getEvents()).containsOnly(mockEvent); return null; } }).when(mockScribeRequestManager).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); subject.sendEvents(); verify(mockQueue, times(2)).peek(); verify(mockQueue, times(1)).poll(); verify(mockScribeRequestManager).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); }
@Test public void sendEvents_withRequestInFlightShouldReturnFast() throws Exception { when(mockScribeRequestManager.isAtCapacity()).thenReturn(true); subject.sendEvents(); verify(mockQueue, never()).poll(); verify(mockScribeRequestManager, never()).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); }