@Test public void testDispatchInterval() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(500); assertThat(dryRunData.isEmpty(), is(true)); mDispatcher.submit(getTestEvent()); await().atLeast(500, TimeUnit.MILLISECONDS).until(() -> dryRunData.size() == 1); }
@Test public void testDispatchInterval() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(500); assertThat(dryRunData.isEmpty(), is(true)); mDispatcher.submit(getTestEvent()); await().atLeast(500, TimeUnit.MILLISECONDS).until(() -> dryRunData.size() == 1); }
@Test public void testClear_cleanExit() throws InterruptedException { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); TestHelper.sleep(100); assertThat(dryRunData.size(), is(1)); dryRunData.clear(); when(mConnectivity.isConnected()).thenReturn(false); mDispatcher.submit(getTestEvent()); TestHelper.sleep(100); assertThat(mEventCacheData.size(), is(1)); mDispatcher.clear(); when(mConnectivity.isConnected()).thenReturn(true); mDispatcher.forceDispatch(); TestHelper.sleep(100); assertThat(dryRunData.size(), is(0)); }
@Test public void testClear_cleanExit() throws InterruptedException { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); TestHelper.sleep(100); assertThat(dryRunData.size(), is(1)); dryRunData.clear(); when(mConnectivity.isConnected()).thenReturn(false); mDispatcher.submit(getTestEvent()); TestHelper.sleep(100); assertThat(mEventCacheData.size(), is(1)); mDispatcher.clear(); when(mConnectivity.isConnected()).thenReturn(true); mDispatcher.forceDispatch(); TestHelper.sleep(100); assertThat(dryRunData.size(), is(0)); }
@Test public void testConnectivityChange() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); when(mConnectivity.isConnected()).thenReturn(false); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); verify(mEventCache, timeout(1000)).add(any()); verify(mEventCache, never()).drainTo(Matchers.anyList()); assertThat(dryRunData.size(), is(0)); when(mConnectivity.isConnected()).thenReturn(true); mDispatcher.forceDispatch(); await().atMost(1, TimeUnit.SECONDS).until(() -> dryRunData.size(), is(1)); verify(mEventCache).updateState(true); verify(mEventCache).drainTo(Matchers.anyList()); }
@Test public void testConnectivityChange() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); when(mConnectivity.isConnected()).thenReturn(false); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); verify(mEventCache, timeout(1000)).add(any()); verify(mEventCache, never()).drainTo(Matchers.anyList()); assertThat(dryRunData.size(), is(0)); when(mConnectivity.isConnected()).thenReturn(true); mDispatcher.forceDispatch(); await().atMost(1, TimeUnit.SECONDS).until(() -> dryRunData.size(), is(1)); verify(mEventCache).updateState(true); verify(mEventCache).drainTo(Matchers.anyList()); }
@Test public void testDispatchMode_wifiOnly() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); when(mConnectivity.getType()).thenReturn(Connectivity.Type.MOBILE); mDispatcher.setDispatchMode(DispatchMode.WIFI_ONLY); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); verify(mEventCache, timeout(1000)).updateState(false); verify(mEventCache, never()).drainTo(Matchers.anyList()); when(mConnectivity.getType()).thenReturn(Connectivity.Type.WIFI); mDispatcher.forceDispatch(); await().atMost(1, TimeUnit.SECONDS).until(() -> dryRunData.size(), is(1)); verify(mEventCache).updateState(true); verify(mEventCache).drainTo(Matchers.anyList()); }
@Test public void testDispatchMode_wifiOnly() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); when(mConnectivity.getType()).thenReturn(Connectivity.Type.MOBILE); mDispatcher.setDispatchMode(DispatchMode.WIFI_ONLY); mDispatcher.submit(getTestEvent()); mDispatcher.forceDispatch(); verify(mEventCache, timeout(1000)).updateState(false); verify(mEventCache, never()).drainTo(Matchers.anyList()); when(mConnectivity.getType()).thenReturn(Connectivity.Type.WIFI); mDispatcher.forceDispatch(); await().atMost(1, TimeUnit.SECONDS).until(() -> dryRunData.size(), is(1)); verify(mEventCache).updateState(true); verify(mEventCache).drainTo(Matchers.anyList()); }
@Test public void testDispatchRetryWithBackoff() throws Exception { AtomicInteger cnt = new AtomicInteger(0); when(mPacketSender.send(any())).then(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return cnt.incrementAndGet() > 5; } }); mDispatcher.setDispatchInterval(100); mDispatcher.submit(getTestEvent()); await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 1); await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 2); await().atMost(1900, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 5); mDispatcher.submit(getTestEvent()); await().atMost(150, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 5); }
@Test public void testDispatchRetryWithBackoff() throws Exception { AtomicInteger cnt = new AtomicInteger(0); when(mPacketSender.send(any())).then(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { return cnt.incrementAndGet() > 5; } }); mDispatcher.setDispatchInterval(100); mDispatcher.submit(getTestEvent()); await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 1); await().atLeast(100, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 2); await().atMost(1900, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 5); mDispatcher.submit(getTestEvent()); await().atMost(150, TimeUnit.MILLISECONDS).until(() -> cnt.get() == 5); }
@Test public void testForceDispatchTwice() throws Exception { mDispatcher.setDispatchInterval(-1); mDispatcher.setConnectionTimeOut(20); mDispatcher.submit(getTestEvent()); assertTrue(mDispatcher.forceDispatch()); assertFalse(mDispatcher.forceDispatch()); }
@Test public void testForceDispatchTwice() throws Exception { mDispatcher.setDispatchInterval(-1); mDispatcher.setConnectionTimeOut(20); mDispatcher.submit(getTestEvent()); assertTrue(mDispatcher.forceDispatch()); assertFalse(mDispatcher.forceDispatch()); }