@Test public void testForceDispatchTwice() throws Exception { mDispatcher.setDispatchInterval(-1); mDispatcher.setConnectionTimeOut(20); mDispatcher.submit(getTestEvent()); assertTrue(mDispatcher.forceDispatch()); assertFalse(mDispatcher.forceDispatch()); }
/** * Packets are collected and dispatched in batches, this intervals sets the pause between batches. * * @param dispatchInterval in milliseconds */ @Override public void setDispatchInterval(long dispatchInterval) { mDispatchInterval = dispatchInterval; if (mDispatchInterval != -1) launch(); }
@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 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 testForceDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(-1L); final int threadCount = 10; final int queryCount = 10; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); TestHelper.sleep(500); assertEquals(threadCount * queryCount, createdEvents.size()); assertEquals(0, dryRunData.size()); mDispatcher.forceDispatch(); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
return null; }).when(mEventCache).clear(); mDispatcher = new DefaultDispatcher(mEventCache, mConnectivity, new PacketFactory(mApiUrl), mPacketSender);
@Test public void testGetDispatchGzipped() { assertFalse(mDispatcher.getDispatchGzipped()); mDispatcher.setDispatchGzipped(true); assertTrue(mDispatcher.getDispatchGzipped()); verify(mPacketSender).setGzipData(true); }
@Test public void testSetConnectionTimeout() throws Exception { mDispatcher.setConnectionTimeOut(100); assertEquals(100, mDispatcher.getConnectionTimeOut()); verify(mPacketSender).setTimeout(100); }
@Override public void clear() { mEventCache.clear(); // Try to exit the loop as the queue is empty if (mRunning) forceDispatch(); }
@Test public void testGetDispatchMode() { assertEquals(DispatchMode.ALWAYS, mDispatcher.getDispatchMode()); mDispatcher.setDispatchMode(DispatchMode.WIFI_ONLY); assertEquals(DispatchMode.WIFI_ONLY, mDispatcher.getDispatchMode()); }
@Test public void testClear() { mDispatcher.clear(); verify(mEventCache).clear(); }
@Test public void testDefaultDispatchInterval() throws Exception { assertEquals(Dispatcher.DEFAULT_DISPATCH_INTERVAL, mDispatcher.getDispatchInterval()); }
@Test public void testDefaultConnectionTimeout() throws Exception { assertEquals(Dispatcher.DEFAULT_CONNECTION_TIMEOUT, mDispatcher.getConnectionTimeOut()); }
@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 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 testForceDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(-1L); final int threadCount = 10; final int queryCount = 10; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); TestHelper.sleep(500); assertEquals(threadCount * queryCount, createdEvents.size()); assertEquals(0, dryRunData.size()); mDispatcher.forceDispatch(); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
return null; }).when(mEventCache).clear(); mDispatcher = new DefaultDispatcher(mEventCache, mConnectivity, new PacketFactory(mApiUrl), mPacketSender);
@Test public void testGetDispatchGzipped() { assertFalse(mDispatcher.getDispatchGzipped()); mDispatcher.setDispatchGzipped(true); assertTrue(mDispatcher.getDispatchGzipped()); verify(mPacketSender).setGzipData(true); }