Source:
void testMergeSegments_BothClaimedByProcessor() throws Exception { initProcessor(TrackingEventProcessorConfiguration.forParallelProcessing(2)); tokenStore.initializeTokenSegments(testSubject.getName(), 2); List<EventMessage<?>> handledEvents = new CopyOnWriteArrayList<>(); when(mockHandler.handle(any())).thenAnswer(i -> handledEvents.add(i.getArgument(0))); publishEvents(10); testSubject.start(); waitForActiveThreads(2); assertFalse(testSubject.processingStatus().get(0).isMerging()); assertFalse(testSubject.processingStatus().get(0).mergeCompletedPosition().isPresent()); assertWithin( 50, TimeUnit.MILLISECONDS, () -> assertTrue(testSubject.mergeSegment(0).join(), "Expected merge to succeed") ); waitForProcessingStatus(0, EventTrackerStatus::isMerging); assertTrue(testSubject.processingStatus().get(0).mergeCompletedPosition().isPresent()); assertArrayEquals(new int[]{0}, tokenStore.fetchSegments(testSubject.getName())); waitForProcessingStatus(0, EventTrackerStatus::isCaughtUp); assertWithin( 5, TimeUnit.SECONDS, () -> assertEquals( 10, handledEvents.stream().map(EventMessage::getIdentifier).distinct().count(), "Expected all 10 messages to be handled" ) ); Thread.sleep(100); assertEquals(10, handledEvents.size(), "Number of handler invocations doesn't match number of unique events"); publishEvents(1); waitForProcessingNotInStatus(0, EventTrackerStatus::isMerging); assertFalse(testSubject.processingStatus().get(0).mergeCompletedPosition().isPresent()); }

Target:
void testMergeSegments_BothClaimedByProcessor() throws Exception { initProcessor(TrackingEventProcessorConfiguration.forParallelProcessing(2)); tokenStore.initializeTokenSegments(testSubject.getName(), 2); List<EventMessage<?>> handledEvents = new CopyOnWriteArrayList<>(); int segmentId = 0; when(mockHandler.handle(any())).thenAnswer(i -> handledEvents.add(i.getArgument(0))); publishEvents(10); testSubject.start(); waitForActiveThreads(2); assertFalse(testSubject.processingStatus().get(segmentId).isMerging()); assertFalse(testSubject.processingStatus().get(segmentId).mergeCompletedPosition().isPresent()); assertWithin( 50, TimeUnit.MILLISECONDS, () -> assertTrue(testSubject.mergeSegment(segmentId).join(), "Expected merge to succeed") ); waitForProcessingStatus(segmentId, EventTrackerStatus::isMerging); assertTrue(testSubject.processingStatus().get(segmentId).mergeCompletedPosition().isPresent()); long mergeCompletedPosition = testSubject.processingStatus().get(segmentId).mergeCompletedPosition().getAsLong(); assertArrayEquals(new int[]{0}, tokenStore.fetchSegments(testSubject.getName())); waitForProcessingStatus(segmentId, EventTrackerStatus::isCaughtUp); assertWithin( 5, TimeUnit.SECONDS, () -> assertEquals( 10, handledEvents.stream().map(EventMessage::getIdentifier).distinct().count(), "Expected all 10 messages to be handled" ) ); Thread.sleep(100); assertEquals(10, handledEvents.size(), "Number of handler invocations doesn't match number of unique events"); publishEvents(1); waitForProcessingNotInStatus(segmentId, EventTrackerStatus::isMerging); assertFalse(testSubject.processingStatus().get(segmentId).mergeCompletedPosition().isPresent()); assertTrue(testSubject.processingStatus().get(segmentId).getCurrentPosition().isPresent()); assertTrue(testSubject.processingStatus().get(segmentId).getCurrentPosition().getAsLong() > mergeCompletedPosition); }