EventCache { public boolean updateState(boolean online) { if (online) { final List<Event> uncache = mDiskCache.uncache(); ListIterator<Event> it = uncache.listIterator(uncache.size()); while (it.hasPrevious()) { mQueue.offerFirst(it.previous()); } Timber.tag(TAG).d("Switched state to ONLINE, uncached %d events from disk.", uncache.size()); } else if (!mQueue.isEmpty()) { List<Event> toCache = new ArrayList<>(); mQueue.drainTo(toCache); mDiskCache.cache(toCache); Timber.tag(TAG).d("Switched state to OFFLINE, caching %d events to disk.", toCache.size()); } return online && !mQueue.isEmpty(); } EventCache(EventDiskCache cache); void add(Event event); void drainTo(List<Event> drainedEvents); void clear(); boolean isEmpty(); boolean updateState(boolean online); void requeue(List<Event> events);  }
@Test public void testUpdateState_online() throws Exception { verify(mEventDiskCache, never()).uncache(); mEventCache.updateState(true); mEventCache.updateState(true); verify(mEventDiskCache, times(2)).uncache(); }