/** * {@inheritDoc} */ @Override public DefaultDataWrapper newInstance() { return new DefaultDataWrapper(); }
/** * {@inheritDoc} */ @PostConstruct public void afterPropertiesSet() throws Exception { doRegistration(); }
/** * {@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); }
@Test public void notEndOfBatch() throws ServerUnavailableException { when(wrapper.getDefaultData()).thenReturn(defaultData); when(connection.isConnected()).thenReturn(true); handler.onEvent(wrapper, 0L, false); verifyZeroInteractions(connection, statsLogger); }
@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 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 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); }
@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); }
@AfterMethod public void stop() { coreService.stop(); }
/** * {@inheritDoc} */ @Override @PreDestroy public void unregisterPlatform() { if (connection.isConnected() && isPlatformRegistered()) { try { connection.unregister(platformId); connection.disconnect(); platformId = -1; } catch (Throwable e) { // NOPMD log.warn("Could not un-register the platform."); } } }
@Test(expectedExceptions = BeanInitializationException.class) public void bufferSizeNotPowerOf2() throws InterruptedException, StorageException { when(disruptorStrategy.getDataBufferSize()).thenReturn(5); coreService.start(); }
/** * Test that unregister will not be called if there is no active connection to the server * and registration is not done at first place. */ @Test public void noUnregisterPlatform() throws Exception { // no unregister if no connection when(connection.isConnected()).thenReturn(false); platformManager.unregisterPlatform(); // no unregister if registration is not done at the first place when(connection.isConnected()).thenReturn(true); platformManager.unregisterPlatform(); verify(connection, times(0)).unregister(anyLong()); }
/** * {@inheritDoc} */ @PreDestroy public void stop() { if (shutdown) { return; } // mark shutdown started shutdown = true; // shutdown disruptor stopDisruptor(); // kill executor service ExecutorServiceUtils.shutdownExecutor(executorService, 5L, TimeUnit.SECONDS); }
/** * {@inheritDoc} */ @Override public void addDefaultData(DefaultData defaultData) { // ignore any data adding if we are in the shutdown phase if (shutdown) { return; } try { // grab the next sequence, never wait for it to be available so that that we don't block long sequence = ringBuffer.tryNext(); try { // get the entry in the disruptor for the sequence and simply change the reference DefaultDataWrapper defaultDataWrapper = ringBuffer.get(sequence); defaultDataWrapper.setDefaultData(defaultData); } finally { ringBuffer.publish(sequence); } } catch (InsufficientCapacityException e) { // pass the dropped count statsLogger.dataDropped(1); } }
/** * Starts the disruptor. * * @throws StorageException * If buffer size can not be read from configuration. */ @SuppressWarnings("unchecked") private void startDisruptor() throws StorageException { // Specify the size of the ring buffer, must be power of 2. int bufferSize = disruptorStrategy.getDataBufferSize(); // define thread factory and initialize disruptor ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("inspectit-disruptor-thread-%d").setDaemon(true).build(); disruptor = new Disruptor<DefaultDataWrapper>(new DefaultDataFactory(), bufferSize, threadFactory, ProducerType.MULTI, new BlockingWaitStrategy()); // Connect the handler disruptor.handleEventsWith(defaultDataHandler); // Start the Disruptor, starts all threads running disruptor.start(); // Get the ring buffer from the Disruptor to be used for publishing. ringBuffer = disruptor.getRingBuffer(); }
@Test public void notConnected() throws ServerUnavailableException { when(wrapper.getDefaultData()).thenReturn(defaultData); when(connection.isConnected()).thenReturn(false); handler.onEvent(wrapper, 0L, true); verify(statsLogger).dataDropped(1); verify(connection).isConnected(); verifyNoMoreInteractions(connection, statsLogger); }
@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 noRepeat() throws ServerUnavailableException { DefaultData defaultData2 = mock(DefaultData.class); when(wrapper.getDefaultData()).thenReturn(defaultData).thenReturn(defaultData2); when(connection.isConnected()).thenReturn(true); handler.onEvent(wrapper, 0L, true); handler.onEvent(wrapper, 0L, true); verify(connection, times(2)).isConnected(); verify(connection, times(2)).sendDataObjects(Mockito.<List<DefaultData>> any()); verifyNoMoreInteractions(connection); verifyZeroInteractions(statsLogger); assertThat(sent, hasSize(2)); assertThat(sent, hasItems(defaultData, defaultData2)); }
@Test public void happyPath() throws ServerUnavailableException { when(wrapper.getDefaultData()).thenReturn(defaultData); when(connection.isConnected()).thenReturn(true); handler.onEvent(wrapper, 0L, true); verify(connection).isConnected(); verify(connection).sendDataObjects(Mockito.<List<DefaultData>> any()); verifyNoMoreInteractions(connection); verifyZeroInteractions(statsLogger); assertThat(sent, hasSize(1)); assertThat(sent, hasItem(defaultData)); }
@Test public void correctOrder() throws ServerUnavailableException { DefaultData defaultData2 = mock(DefaultData.class); when(wrapper.getDefaultData()).thenReturn(defaultData).thenReturn(defaultData2); when(connection.isConnected()).thenReturn(true); handler.onEvent(wrapper, 0L, false); handler.onEvent(wrapper, 0L, true); verify(connection).isConnected(); verify(connection).sendDataObjects(Mockito.<List<DefaultData>> any()); verifyNoMoreInteractions(connection); verifyZeroInteractions(statsLogger); assertThat(sent, hasSize(2)); assertThat(sent, hasItems(defaultData, defaultData2)); }