FeedCycleRenderer extends SwingWorker<Map<String, List<Map<String, String>>>, Map<String, List<Map<String, String>>>> { @Override protected Map<String, List<Map<String, String>>> doInBackground() { dataRequestTimer.startInterval(); Map<String, List<Map<String, String>>> values = new HashMap<String, List<Map<String, String>>>(); PERF_LOGGER.debug("size of feed views {0}", activeFeedViews.size()); try { final Map<Request, Set<FeedProvider>> fullSpanRequests = batchByRequestTime(); Map<Request, Set<FeedProvider>> currentIterationRequests = getCurrentIterationRequests(fullSpanRequests,Collections.<Request,Set<FeedProvider>>emptyMap()); while (!currentIterationRequests.isEmpty()) { Set<Entry<Request, Set<FeedProvider>>> entries = currentIterationRequests.entrySet(); for (Entry<Request, Set<FeedProvider>> request : entries) { if (isCancelled()) { return Collections.emptyMap(); } Request r = request.getKey(); Set<String> feedIds = new HashSet<String>(); for (FeedProvider provider : request.getValue()) { try { feedIds.add(provider.getSubscriptionId()); } catch (Exception e) { LOGGER.error("exception occurred while getting subscription id from " + provider, e); } } FeedAggregator feedAggregator = PlatformAccess.getPlatform().getFeedAggregator(); Map<String, List<Map<String, String>>> data = new HashMap<String, List<Map<String, String>>>(); if (feedAggregator != null) { data = feedAggregator.getData(feedIds, TimeUnit.MILLISECONDS, r.getStartTime(), r.getEndTime()); } Map<String, List<Map<String,String>>> adjValues = adjustResponses(data, r.getStartTime()); values.putAll(adjValues); requestCompleted(values, r.getStartTime(), r.getEndTime()); } currentIterationRequests = getCurrentIterationRequests(fullSpanRequests, currentIterationRequests); } } catch (Exception e) { LOGGER.error("exception occurred while retrieving data ", e); } additionalBackgroundProcessing(values); dataRequestTimer.stopInterval(); return values; } FeedCycleRenderer(Map<FeedProvider, Long[]> timeMapping,
                    Set<FeedView> activeViews);   }
@Test public void testAdjustResponse() throws Exception { Map<FeedProvider, Long[]> times = new HashMap<FeedProvider, Long[]>(); FeedCycleRenderer worker = new FeedCycleRenderer(times, Collections.<FeedView>singleton(fv1)) { @Override public Map<String, List<Map<String,String>>> doInBackground() { return super.doInBackground(); } }; Method m = worker.getClass().getSuperclass().getDeclaredMethod("adjustResponses", Map.class, Long.TYPE); long startTime = 1000; Map<String, List<Map<String,String>>> values = new HashMap<String, List<Map<String,String>>>(); values.put("1", Collections.singletonList(Collections.singletonMap(FeedProvider.NORMALIZED_TIME_KEY, Long.toString(startTime-1)))); values.put("2", Collections.singletonList(Collections.singletonMap(FeedProvider.NORMALIZED_TIME_KEY, Long.toString(startTime+1)))); m.setAccessible(true); m.invoke(worker, values, startTime); Assert.assertEquals(values.get("1").get(0).get(FeedProvider.NORMALIZED_TIME_KEY), Long.toString(startTime)); Assert.assertEquals(values.get("2").get(0).get(FeedProvider.NORMALIZED_TIME_KEY), Long.toString(startTime+1)); }
@Test public void testDoInBackground() throws Exception { Map<FeedProvider, Long[]> times = new HashMap<FeedProvider, Long[]>(); times.put(numericProvider, new Long[]{0L,1L}); times.put(alphaProvider, new Long[]{0L,1L}); times.put(exceptionProvider, new Long[]{0L,1L}); FeedCycleRenderer worker = new FeedCycleRenderer(times, Collections.<FeedView>singleton(fv1)) { @Override public Map<String, List<Map<String,String>>> doInBackground() { return super.doInBackground(); } }; worker.cancel(true); Assert.assertTrue(worker.doInBackground().isEmpty()); worker = new FeedCycleRenderer(times, new HashSet<FeedView>(Arrays.asList(fv1, fv2, fv3, fv4, fv5))) { @Override public Map<String, List<Map<String,String>>> doInBackground() { return super.doInBackground(); } }; Map<String, List<Map<String,String>>> values = worker.doInBackground(); Assert.assertEquals(values.size(), expectedValues.size()); for (String key:expectedValues.keySet()) { Assert.assertTrue(values.containsKey(key)); } Assert.assertEquals(1, values.get(numericProvider.getSubscriptionId()).size()); Assert.assertEquals(1, values.get(alphaProvider.getSubscriptionId()).size()); Assert.assertNull(values.get(exceptionProvider.getSubscriptionId())); worker.cancel(true); worker.done(); }