C0urante commented on a change in pull request #10014: URL: https://github.com/apache/kafka/pull/10014#discussion_r568654782
########## File path: connect/runtime/src/test/java/org/apache/kafka/connect/runtime/distributed/DistributedHerderTest.java ########## @@ -2161,6 +2163,83 @@ public Boolean answer() throws Throwable { PowerMock.verifyAll(); } + @Test + public void testKeyRotationWhenWorkerBecomesLeader() throws Exception { + EasyMock.expect(member.memberId()).andStubReturn("member"); + EasyMock.expect(member.currentProtocolVersion()).andStubReturn(CONNECT_PROTOCOL_V2); + EasyMock.expect(worker.getPlugins()).andReturn(plugins); + + expectRebalance(1, Collections.emptyList(), Collections.emptyList()); + expectPostRebalanceCatchup(SNAPSHOT); + // First rebalance: poll indefinitely as no key has been read yet, so expiration doesn't come into play + member.poll(Long.MAX_VALUE); + EasyMock.expectLastCall(); + + expectRebalance(2, Collections.emptyList(), Collections.emptyList()); + SessionKey initialKey = new SessionKey(EasyMock.mock(SecretKey.class), 0); + ClusterConfigState snapshotWithKey = new ClusterConfigState(2, initialKey, Collections.singletonMap(CONN1, 3), + Collections.singletonMap(CONN1, CONN1_CONFIG), Collections.singletonMap(CONN1, TargetState.STARTED), + TASK_CONFIGS_MAP, Collections.<String>emptySet()); + expectPostRebalanceCatchup(snapshotWithKey); + // Second rebalance: poll indefinitely as worker is follower, so expiration still doesn't come into play + member.poll(Long.MAX_VALUE); + EasyMock.expectLastCall(); + + expectRebalance(2, Collections.emptyList(), Collections.emptyList(), "member", MEMBER_URL); + Capture<SessionKey> updatedKey = EasyMock.newCapture(); + configBackingStore.putSessionKey(EasyMock.capture(updatedKey)); + EasyMock.expectLastCall().andAnswer(() -> { + configUpdateListener.onSessionKeyUpdate(updatedKey.getValue()); + return null; + }); + // Third rebalance: poll for a limited time as worker has become leader and must wake up for key expiration + Capture<Long> pollTimeout = EasyMock.newCapture(); + member.poll(EasyMock.captureLong(pollTimeout)); + EasyMock.expectLastCall(); + + PowerMock.replayAll(); + + herder.tick(); + configUpdateListener.onSessionKeyUpdate(initialKey); + herder.tick(); + herder.tick(); + + assertTrue(pollTimeout.getValue() <= DistributedConfig.INTER_WORKER_KEY_TTL_MS_MS_DEFAULT); + } Review comment: There are a bunch of expectations set up with the `expectRebalance` and `expectPostRebalance` methods; I've added the `PowerMock::verifyAll` call as a result. Good catch, thanks! ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org