@Test public void runtimeException() { RuntimeException runtimeException = new RuntimeException(); when(platformSensors.isEmpty()).thenThrow(runtimeException); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); // verify logged not not exceptional exit verify(log).error(Mockito.anyString(), eq(runtimeException)); }
@Test public void jmxSensor() { when(platformSensors.isEmpty()).thenReturn(true); doReturn(new ArrayIterator(new IJmxSensor[] { jmxSensor })).when(jmxSensors).iterator(); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); verify(jmxSensor).update(coreService); verifyNoMoreInteractions(jmxSensor); }
@Test public void platformSensorError() { when(jmxSensors.isEmpty()).thenReturn(true); final List<IPlatformSensor> sensors = new ArrayList<IPlatformSensor>(); sensors.add(platformSensor); doAnswer(new Answer<Iterator<?>>() { @Override public Iterator<?> answer(InvocationOnMock invocation) throws Throwable { return sensors.iterator(); } }).when(platformSensors).iterator(); doThrow(RuntimeException.class).when(platformSensor).gather(); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); assertThat(sensors, is(empty())); }
@Test public void platformSensorCollect() throws InterruptedException, StorageException { when(jmxSensors.isEmpty()).thenReturn(true); doAnswer(new Answer<Iterator<?>>() { @Override public Iterator<?> answer(InvocationOnMock invocation) throws Throwable { return new ArrayIterator(new IPlatformSensor[] { platformSensor }); } }).when(platformSensors).iterator(); SystemInformationData sid = mock(SystemInformationData.class); when(platformSensor.get()).thenReturn(sid); when(disruptorStrategy.getDataBufferSize()).thenReturn(8); coreService.start(); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); sensorRefresher.run(); sensorRefresher.run(); sensorRefresher.run(); sensorRefresher.run(); verify(platformSensor).reset(); verify(platformSensor, times(5)).gather(); verify(platformSensor).get(); verifyNoMoreInteractions(platformSensor); // need to sleep a bit so handler is notified Thread.sleep(100); ArgumentCaptor<DefaultDataWrapper> captor = ArgumentCaptor.forClass(DefaultDataWrapper.class); verify(defaultDataHandler).onEvent(captor.capture(), anyLong(), eq(true)); assertThat(captor.getValue().getDefaultData(), is((DefaultData) sid)); }
@Test public void jmxSensorTwice() { when(platformSensors.isEmpty()).thenReturn(true); doAnswer(new Answer<Iterator<?>>() { @Override public Iterator<?> answer(InvocationOnMock invocation) throws Throwable { return new ArrayIterator(new IJmxSensor[] { jmxSensor }); } }).when(jmxSensors).iterator(); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); sensorRefresher.run(); verify(jmxSensor, times(2)).update(coreService); verifyNoMoreInteractions(jmxSensor); }
@Test public void platformSensor() { when(jmxSensors.isEmpty()).thenReturn(true); doAnswer(new Answer<Iterator<?>>() { @Override public Iterator<?> answer(InvocationOnMock invocation) throws Throwable { return new ArrayIterator(new IPlatformSensor[] { platformSensor }); } }).when(platformSensors).iterator(); Runnable sensorRefresher = coreService.new SensorRefresher(); sensorRefresher.run(); verify(platformSensor).reset(); verify(platformSensor).gather(); verifyNoMoreInteractions(platformSensor); }
/** * {@inheritDoc} */ @PostConstruct public void start() { // start disruptor try { startDisruptor(); } catch (Exception e) { throw new BeanInitializationException("Can not initialize disruptor.", e); } // schedule the sensor refresher runnable executorService.scheduleWithFixedDelay(new SensorRefresher(), sensorRefreshTime, sensorRefreshTime, TimeUnit.MILLISECONDS); }