/** * Instructs the current Thread to sleep until the given deadline. This method may be overridden to check for * flags that have been set to return earlier than the given deadline. * <p> * The default implementation will sleep in blocks of 100ms, intermittently checking for the processor's state. Once * the processor stops running, this method will return immediately (after detecting the state change). * * @param millisToSleep The number of milliseconds to sleep */ protected void doSleepFor(long millisToSleep) { long deadline = System.currentTimeMillis() + millisToSleep; try { long timeLeft; while (getState().isRunning() && (timeLeft = deadline - System.currentTimeMillis()) > 0) { Thread.sleep(Math.min(timeLeft, 100)); } } catch (InterruptedException e) { logger.warn("Thread interrupted. Preparing to shut down event processor"); shutDown(); Thread.currentThread().interrupt(); } }
@After public void tearDown() { testSubject.shutDown(); eventBus.shutDown(); }
@After public void tearDown() { testSubject.shutDown(); eventBus.shutDown(); }
assertEquals(2, ackedEvents.size()); testSubject.shutDown();
} catch (InterruptedException e) { logger.error(String.format("Event processor [%s] was interrupted. Shutting down.", getName()), e); this.shutDown(); Thread.currentThread().interrupt();
testSubject.shutDown();
@Test public void testResetBeforeStartingPerformsANormalRun() throws Exception { when(mockHandler.supportsReset()).thenReturn(true); final List<String> handled = new CopyOnWriteArrayList<>(); final List<String> handledInRedelivery = new CopyOnWriteArrayList<>(); //noinspection Duplicates doAnswer(i -> { EventMessage message = i.getArgument(0); handled.add(message.getIdentifier()); if (ReplayToken.isReplay(message)) { handledInRedelivery.add(message.getIdentifier()); } return null; }).when(mockHandler).handle(any()); testSubject.start(); testSubject.shutDown(); testSubject.resetTokens(); testSubject.start(); eventBus.publish(createEvents(4)); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(4, handled.size())); assertEquals(0, handledInRedelivery.size()); assertFalse(testSubject.processingStatus().get(0).isReplaying()); }
@Test public void testResetToPositionCausesCertainEventsToBeReplayed() throws Exception { when(mockHandler.supportsReset()).thenReturn(true); final List<String> handled = new CopyOnWriteArrayList<>(); final List<String> handledInRedelivery = new CopyOnWriteArrayList<>(); //noinspection Duplicates doAnswer(i -> { EventMessage message = i.getArgument(0); handled.add(message.getIdentifier()); if (ReplayToken.isReplay(message)) { handledInRedelivery.add(message.getIdentifier()); } return null; }).when(mockHandler).handle(any()); eventBus.publish(createEvents(4)); testSubject.start(); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(4, handled.size())); testSubject.shutDown(); testSubject.resetTokens(source -> new GlobalSequenceTrackingToken(1L)); testSubject.start(); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(6, handled.size())); assertFalse(handledInRedelivery.contains(handled.get(0))); assertFalse(handledInRedelivery.contains(handled.get(1))); assertEquals(handled.subList(2, 4), handled.subList(4, 6)); assertEquals(handled.subList(4, 6), handledInRedelivery); assertTrue(testSubject.processingStatus().get(0).isReplaying()); eventBus.publish(createEvents(1)); assertWithin(1, TimeUnit.SECONDS, () -> assertFalse(testSubject.processingStatus().get(0).isReplaying())); }
@Test public void testResetCausesEventsToBeReplayed() throws Exception { when(mockHandler.supportsReset()).thenReturn(true); final List<String> handled = new CopyOnWriteArrayList<>(); final List<String> handledInRedelivery = new CopyOnWriteArrayList<>(); //noinspection Duplicates doAnswer(i -> { EventMessage message = i.getArgument(0); handled.add(message.getIdentifier()); if (ReplayToken.isReplay(message)) { handledInRedelivery.add(message.getIdentifier()); } return null; }).when(mockHandler).handle(any()); eventBus.publish(createEvents(4)); testSubject.start(); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(4, handled.size())); testSubject.shutDown(); testSubject.resetTokens(); // Resetting twice caused problems (see issue #559) testSubject.resetTokens(); testSubject.start(); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(8, handled.size())); assertEquals(handled.subList(0, 4), handled.subList(4, 8)); assertEquals(handled.subList(4, 8), handledInRedelivery); assertTrue(testSubject.processingStatus().get(0).isReplaying()); eventBus.publish(createEvents(1)); assertWithin(1, TimeUnit.SECONDS, () -> assertFalse(testSubject.processingStatus().get(0).isReplaying())); }
); testSubject.shutDown();
testSubject.start(); assertWithin(1, TimeUnit.SECONDS, () -> assertEquals(2, handled.size())); testSubject.shutDown(); testSubject.resetTokens(); testSubject.start();
/** * Instructs the current Thread to sleep until the given deadline. This method may be overridden to check for * flags that have been set to return earlier than the given deadline. * <p> * The default implementation will sleep in blocks of 100ms, intermittently checking for the processor's state. Once * the processor stops running, this method will return immediately (after detecting the state change). * * @param millisToSleep The number of milliseconds to sleep */ protected void doSleepFor(long millisToSleep) { long deadline = System.currentTimeMillis() + millisToSleep; try { long timeLeft; while (getState().isRunning() && (timeLeft = deadline - System.currentTimeMillis()) > 0) { Thread.sleep(Math.min(timeLeft, 100)); } } catch (InterruptedException e) { logger.warn("Thread interrupted. Preparing to shut down event processor"); shutDown(); Thread.currentThread().interrupt(); } }
/** * Instructs the current Thread to sleep until the given deadline. This method may be overridden to check for * flags that have been set to return earlier than the given deadline. * <p> * The default implementation will sleep in blocks of 100ms, intermittently checking for the processor's state. Once * the processor stops running, this method will return immediately (after detecting the state change). * * @param millisToSleep The number of milliseconds to sleep */ protected void doSleepFor(long millisToSleep) { long deadline = System.currentTimeMillis() + millisToSleep; try { long timeLeft; while (getState().isRunning() && (timeLeft = deadline - System.currentTimeMillis()) > 0) { Thread.sleep(Math.min(timeLeft, 100)); } } catch (InterruptedException e) { logger.warn("Thread interrupted. Preparing to shut down event processor"); shutDown(); Thread.currentThread().interrupt(); } }
} catch (InterruptedException e) { logger.error(String.format("Event processor [%s] was interrupted. Shutting down.", getName()), e); this.shutDown(); Thread.currentThread().interrupt();
} catch (InterruptedException e) { logger.error(String.format("Event processor [%s] was interrupted. Shutting down.", getName()), e); this.shutDown(); Thread.currentThread().interrupt();