use of org.apache.pulsar.client.api.ConsumerEventListener in project incubator-pulsar by apache.
the class MembershipManagerTest method testConsumerEventListener.
@Test
public void testConsumerEventListener() throws Exception {
PulsarClient mockClient = mock(PulsarClient.class);
Consumer mockConsumer = mock(Consumer.class);
AtomicReference<ConsumerEventListener> listenerHolder = new AtomicReference<>();
when(mockClient.subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class))).thenAnswer(invocationOnMock -> {
ConsumerConfiguration conf = invocationOnMock.getArgumentAt(2, ConsumerConfiguration.class);
listenerHolder.set(conf.getConsumerEventListener());
return mockConsumer;
});
MembershipManager membershipManager = spy(new MembershipManager(workerConfig, mockClient));
assertFalse(membershipManager.isLeader());
verify(mockClient, times(1)).subscribe(eq(workerConfig.getClusterCoordinationTopic()), eq(MembershipManager.COORDINATION_TOPIC_SUBSCRIPTION), any(ConsumerConfiguration.class));
listenerHolder.get().becameActive(mockConsumer, 0);
assertTrue(membershipManager.isLeader());
listenerHolder.get().becameInactive(mockConsumer, 0);
assertFalse(membershipManager.isLeader());
}
Aggregations