Search in sources :

Example 1 with ConsumerEventListener

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());
}
Also used : ConsumerEventListener(org.apache.pulsar.client.api.ConsumerEventListener) Consumer(org.apache.pulsar.client.api.Consumer) ConsumerConfiguration(org.apache.pulsar.client.api.ConsumerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) PulsarClient(org.apache.pulsar.client.api.PulsarClient) Test(org.testng.annotations.Test)

Aggregations

AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Consumer (org.apache.pulsar.client.api.Consumer)1 ConsumerConfiguration (org.apache.pulsar.client.api.ConsumerConfiguration)1 ConsumerEventListener (org.apache.pulsar.client.api.ConsumerEventListener)1 PulsarClient (org.apache.pulsar.client.api.PulsarClient)1 Test (org.testng.annotations.Test)1