/** * Tests that after maximum amount of elements is reached we move them to persist list. */ @Test public void maxElementsReached() { aggregator.maxElements = 1; TimerData timerData1 = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); TimerData timerData2 = new TimerData(new Timestamp(System.currentTimeMillis()), 100L, 200L, 300L); aggregator.processTimerData(timerData1); aggregator.processTimerData(timerData2); assertThat(aggregator.getElementCount(), is(1)); verifyZeroInteractions(entityManager); }
/** * {@inheritDoc} */ @Override public void run() { while (true) { TimerData timerData = this.mostRecentlyAdded; if (timerData != null) { if (timerData == lastChecked) { // NOPMD this.timerDataAggregator.removeAndPersistAll(); } lastChecked = timerData; } this.timerDataAggregator.saveAllInPersistList(); try { Thread.sleep(this.timerDataAggregator.cacheCleanSleepingPeriod); } catch (InterruptedException e) { Thread.interrupted(); } } }
/** * Initialize. */ @BeforeMethod public void init() { MockitoAnnotations.initMocks(this); aggregator = new TimerDataAggregator(transactionManager); aggregator.aggregationPeriod = 5l; aggregator.cacheCleanSleepingPeriod = 10; aggregator.maxElements = 100; aggregator.entityManager = entityManager; }
/** * Tests that persist list saving includes correct elements being saved. */ @Test public void saveAllInPersistList() { aggregator.maxElements = 1; TimerData timerData1 = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); TimerData timerData2 = new TimerData(new Timestamp(System.currentTimeMillis()), 100L, 200L, 300L); aggregator.processTimerData(timerData1); aggregator.processTimerData(timerData2); aggregator.saveAllInPersistList(); ArgumentCaptor<DatabaseAggregatedTimerData> argument = ArgumentCaptor.forClass(DatabaseAggregatedTimerData.class); verify(entityManager, times(1)).persist(argument.capture()); assertThat(argument.getValue(), is(instanceOf(DatabaseAggregatedTimerData.class))); assertThat(argument.getValue().getPlatformIdent(), is(timerData1.getPlatformIdent())); assertThat(argument.getValue().getSensorTypeIdent(), is(timerData1.getSensorTypeIdent())); assertThat(argument.getValue().getMethodIdent(), is(timerData1.getMethodIdent())); }
aggregator.processTimerData(timerData); aggregator.processTimerData(timerData2); aggregator.removeAndPersistAll(); verify(entityManager, timeout(10000).times(2)).persist(argThat(new ArgumentMatcher<TimerData>() { @Override
/** * {@inheritDoc} */ @Override protected void processData(DefaultData defaultData, EntityManager entityManager) { if (defaultData instanceof HttpTimerData) { try { HttpTimerData original = (HttpTimerData) defaultData; HttpInfo httpInfo = getHttpInfo(original, entityManager); HttpTimerData clone = getClone(original); clone.setHttpInfo(httpInfo); entityManager.persist(clone); } catch (SerializationException e) { log.warn("TimerDataChartingCmrProcessor failed to clone the given HttpTimerData", e); } } else { timerDataAggregator.processTimerData((TimerData) defaultData); } }
long aggregationTimestamp = getAlteredTimestamp(timerData); int cacheHash = getCacheHash(timerData.getPlatformIdent(), timerData.getMethodIdent(), aggregationTimestamp); TimerData oldest = queue.poll(); if (null != oldest) { map.remove(getCacheHash(oldest.getPlatformIdent(), oldest.getMethodIdent(), oldest.getTimeStamp().getTime())); persistList.add(oldest); count = elementCount.decrementAndGet();
processor.process(httpTimerData, entityManager); verify(timerDataAggregator, times(1)).processTimerData(timerData);
/** * Tests that if we place many time same amount of elements, maximum will not be reached. */ @Test public void noMaxElementsReached() { aggregator.maxElements = 2; TimerData timerData1 = new TimerData(new Timestamp(System.currentTimeMillis()), 10L, 20L, 30L); TimerData timerData2 = new TimerData(new Timestamp(System.currentTimeMillis()), 100L, 200L, 300L); for (int i = 0; i < 100; i++) { aggregator.processTimerData(timerData1); aggregator.processTimerData(timerData2); } assertThat(aggregator.getElementCount(), is(2)); verifyZeroInteractions(entityManager); }
when(timerData.getMethodIdent()).thenReturn(20L); aggregator.processTimerData(timerData);