void removePartitions(Collection<TopicPartition> revokedPartitions) { revokedPartitions.forEach((key) -> { PartitionProcessor processor = processors.get(key); if (processor == null) { return; // idempotent } if (!processor.isTerminated()) { throw new IllegalStateException("Processor must be terminated before removing it."); } logger.debug("Removing PartitionProcessor for partition {}", key); processors.remove(key); }); }
@Test public void basicLifecycle() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); assertFalse(processor.isTerminated()); assertFalse(processor.isPaused()); assertTrue(processor.shouldResume()); assertEquals(0, processor.numberOfUnprocessedMessages()); assertFalse(processor.hasUncommittedMessages()); assertEquals(-1, processor.getCommitOffsetAndClear()); processor.waitForHandlersToTerminate(1); assertTrue(processor.isTerminated()); assertEquals(0, processor.numberOfUnprocessedMessages()); assertFalse(processor.hasUncommittedMessages()); assertEquals(-1, processor.getCommitOffsetAndClear()); }
@Test public void shutDownEvenIfHandlerIsStillActive() throws InterruptedException { PartitionProcessor processor = givenAPartionProcessor(); ConsumerRecord<String, byte[]> record = testRecordWithOffset(999); processor.enqueue(record); (getTestHandler(processor)).onMessageCalled.await(); processor.stopProcessing(); assertFalse(processor.isTerminated()); processor.waitForHandlersToTerminate(10); // should log a warn about still running handlers assertTrue(processor.isTerminated()); assertFalse(processor.hasUncommittedMessages()); // If handler finishes, it will mark message as committable, but at this time, the processor is probably already removed. (getTestHandler(processor)).blockReturnFromOnMessage.countDown(); shortSleep(); // allow handler to continue assertTrue(processor.hasUncommittedMessages()); }