@Test(expectedExceptions = BeanInitializationException.class) public void bufferSizeNotPowerOf2() throws InterruptedException, StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(5); coreService.start(); }
@Test public void sensorRefresherScheduled() throws StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(8); coreService.start(); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(executorService).scheduleWithFixedDelay(captor.capture(), Mockito.anyLong(), Mockito.anyLong(), Mockito.<TimeUnit> any()); assertThat(captor.getValue(), is(instanceOf(SensorRefresher.class))); }
@Test public void stop() throws Exception { when(disruptorStrategy.getDataBufferSize()).thenReturn(8); when(executorService.awaitTermination(anyLong(), Mockito.<TimeUnit> any())).thenReturn(true); coreService.start(); coreService.stop(); verify(executorService).scheduleWithFixedDelay(Mockito.<Runnable> any(), anyLong(), anyLong(), Mockito.<TimeUnit> any()); verify(executorService).shutdown(); verify(executorService).awaitTermination(anyLong(), Mockito.<TimeUnit> any()); verifyNoMoreInteractions(executorService); }
@Test public void noAddOnShutdown() throws InterruptedException, StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(8); coreService.start(); coreService.stop(); coreService.addDefaultData(data); // need to sleep a bit so handler is notified Thread.sleep(100); verifyNoMoreInteractions(defaultDataHandler); }
@Test public void happyPath() throws InterruptedException, StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(8); coreService.start(); coreService.addDefaultData(data); // 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(data)); }
@Test public void stopTwice() throws Exception { when(disruptorStrategy.getDataBufferSize()).thenReturn(8); when(executorService.awaitTermination(anyLong(), Mockito.<TimeUnit> any())).thenReturn(true); coreService.start(); coreService.stop(); coreService.stop(); verify(executorService).scheduleWithFixedDelay(Mockito.<Runnable> any(), anyLong(), anyLong(), Mockito.<TimeUnit> any()); verify(executorService).shutdown(); verify(executorService).awaitTermination(anyLong(), Mockito.<TimeUnit> any()); verifyNoMoreInteractions(executorService); } }
@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 capacityReached() throws InterruptedException, StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(2); // slow down the wrapper so we get capacity error doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Thread.sleep(1); return null; } }).when(defaultDataHandler).onEvent(Mockito.<DefaultDataWrapper> any(), anyLong(), anyBoolean()); coreService.start(); coreService.addDefaultData(data); coreService.addDefaultData(data); coreService.addDefaultData(data); coreService.addDefaultData(data); // we should report 2 times verify(statsLogger, times(2)).dataDropped(1); }