@Test public void testRandomDispatchIntervals() throws Exception { final List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); final int threadCount = 10; final int queryCount = 100; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); new Thread(new Runnable() { @Override public void run() { try { while (getFlattenedQueries(new ArrayList<>(dryRunData)).size() != threadCount * queryCount) { mDispatcher.setDispatchInterval(new Random().nextInt(20 - -1) + -1); } } catch (Exception e) {e.printStackTrace();} } }).start(); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@Test public void testMultiThreadDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(20); final int threadCount = 20; final int queryCount = 100; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@Test public void testRandomDispatchIntervals() throws Exception { final List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); final int threadCount = 10; final int queryCount = 100; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); new Thread(new Runnable() { @Override public void run() { try { while (getFlattenedQueries(new ArrayList<>(dryRunData)).size() != threadCount * queryCount) { mDispatcher.setDispatchInterval(new Random().nextInt(20 - -1) + -1); } } catch (Exception e) {e.printStackTrace();} } }).start(); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@Test public void testMultiThreadDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(20); final int threadCount = 20; final int queryCount = 100; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@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 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 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 testBatchDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(1500); final int threadCount = 5; final int queryCount = 5; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); await().atMost(2, TimeUnit.SECONDS).until(() -> createdEvents.size(), is(threadCount * queryCount)); assertEquals(0, dryRunData.size()); await().atMost(2, TimeUnit.SECONDS).until(() -> createdEvents.size(), is(threadCount * queryCount)); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@Test public void testBatchDispatch() throws Exception { List<Packet> dryRunData = Collections.synchronizedList(new ArrayList<Packet>()); mDispatcher.setDryRunTarget(dryRunData); mDispatcher.setDispatchInterval(1500); final int threadCount = 5; final int queryCount = 5; final List<String> createdEvents = Collections.synchronizedList(new ArrayList<String>()); launchTestThreads(mApiUrl, mDispatcher, threadCount, queryCount, createdEvents); await().atMost(2, TimeUnit.SECONDS).until(() -> createdEvents.size(), is(threadCount * queryCount)); assertEquals(0, dryRunData.size()); await().atMost(2, TimeUnit.SECONDS).until(() -> createdEvents.size(), is(threadCount * queryCount)); checkForMIAs(threadCount * queryCount, createdEvents, dryRunData); }
@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); }
@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); }
@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()); }