BackOff { synchronized long next() { long nextTick, curr = currentTimeMillis(); long lastTick = mSharedPref.load(KEY_LAST_TICK, curr); if (lastTick == LAST_TICK_RESET_VALUE) { lastTick = curr; } nextTick = curr + getMills(mRetry); if (curr - lastTick > 0) { mSharedPref.save(KEY_RETRY_COUNT, ++mRetry); } mSharedPref.save(KEY_LAST_TICK, nextTick); return nextTick; } BackOff(Context context); static BackOff getInstance(Context context); boolean hasNext();  }
@Test public void testNext() { mBackOff.reset(); when(mConfig.getFlushInterval()).thenReturn(10000); when(mSharedPref.load(anyString(), anyLong())).thenReturn(0L); long t1 = mBackOff.next(); mCurrentMills = t1; when(mSharedPref.load(anyString(), anyLong())).thenReturn(t1); long t2 = mBackOff.next(); mCurrentMills = t2; when(mSharedPref.load(anyString(), anyLong())).thenReturn(t2); long t3 = mBackOff.next(); assertTrue(t1 < t2 && t2 < t3); verify(mSharedPref, times(1)).save(anyString(), eq(t1)); verify(mSharedPref, times(1)).save(anyString(), eq(t2)); verify(mSharedPref, times(1)).save(anyString(), eq(t3)); when(mSharedPref.load(anyString(), anyLong())).thenReturn(t3); }