ScribeEventRecorder implements EventRecorder { @Override public void record(@NonNull BaseEvent baseEvent) { if (!mEventSampler.sample(baseEvent)) { return; } if (mEventQueue.size() >= QUEUE_MAX_SIZE) { MoPubLog.d("EventQueue is at max capacity. " + "Event \"" + baseEvent.getName() + "\" is being dropped."); return; } mEventQueue.add(baseEvent); if (mEventQueue.size() >= EVENT_COUNT_SEND_THRESHHOLD) { sendEvents(); } scheduleNextPoll(); } 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 record_shouldSampleEvent() throws Exception { subject.record(mockEvent); verify(mockEventSampler).sample(mockEvent); }
@Test public void record_withQueueSizeBelowSendThreshold_shouldQueueEvent_shouldNotSendEvents_shouldScheduleNextPoll() throws Exception { when(mockQueue.size()).thenReturn(99); subject.record(mockEvent); verify(mockQueue).add(mockEvent); verify(mockScribeRequestManager, never()).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); verify(mockHandler).postDelayed(any(ScribeEventRecorder.PollingRunnable.class), eq(Long.valueOf(120000))); }
@Test public void record_withQueueSizeAtSendThreshold_shouldQueueEvent_shouldSendEvents_shouldScheduleNextPoll() throws Exception { when(mockQueue.size()).thenReturn(100); when(mockQueue.peek()).thenReturn(mockEvent); when(mockQueue.poll()).thenReturn(mockEvent); subject.record(mockEvent); verify(mockQueue).add(mockEvent); verify(mockScribeRequestManager).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); verify(mockHandler).postDelayed(any(ScribeEventRecorder.PollingRunnable.class), eq(Long.valueOf(120000))); }
@Test public void record_withQueueSizeAtQueueLimit_shouldNotQueueEvent_shouldNotSendEvents_shouldNotScheduleNextPoll() throws Exception { when(mockQueue.size()).thenReturn(500); subject.record(mockEvent); verify(mockQueue, never()).add(mockEvent); verify(mockScribeRequestManager, never()).makeRequest(any(ScribeRequest.ScribeRequestFactory.class), any(ScribeBackoffPolicy.class)); verify(mockHandler, never()).postDelayed(any(ScribeEventRecorder.PollingRunnable.class), anyLong()); }