Search in sources :

Example 1 with LeaveGroupRequest

use of org.apache.kafka.common.requests.LeaveGroupRequest in project kafka by apache.

the class ConsumerCoordinatorTest method testMaybeLeaveGroup.

@Test
public void testMaybeLeaveGroup() {
    final String consumerId = "consumer";
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE));
    client.prepareResponse(syncGroupResponse(singletonList(t1p), Errors.NONE));
    coordinator.joinGroupIfNeeded();
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            received.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.memberId().equals(consumerId) && leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE));
    coordinator.maybeLeaveGroup();
    assertTrue(received.get());
    AbstractCoordinator.Generation generation = coordinator.generation();
    assertNull(generation);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 2 with LeaveGroupRequest

use of org.apache.kafka.common.requests.LeaveGroupRequest in project kafka by apache.

the class ConsumerCoordinatorTest method testLeaveGroupOnClose.

@Test
public void testLeaveGroupOnClose() {
    final String consumerId = "consumer";
    subscriptions.subscribe(singleton(topic1), rebalanceListener);
    client.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
    coordinator.ensureCoordinatorReady();
    client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE));
    client.prepareResponse(syncGroupResponse(singletonList(t1p), Errors.NONE));
    coordinator.joinGroupIfNeeded();
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            received.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.memberId().equals(consumerId) && leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE));
    coordinator.close(0);
    assertTrue(received.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 3 with LeaveGroupRequest

use of org.apache.kafka.common.requests.LeaveGroupRequest in project kafka by apache.

the class ConsumerCoordinatorTest method gracefulCloseTest.

private void gracefulCloseTest(ConsumerCoordinator coordinator, boolean dynamicAssignment) throws Exception {
    final AtomicBoolean commitRequested = new AtomicBoolean();
    final AtomicBoolean leaveGroupRequested = new AtomicBoolean();
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            commitRequested.set(true);
            OffsetCommitRequest commitRequest = (OffsetCommitRequest) body;
            return commitRequest.groupId().equals(groupId);
        }
    }, new OffsetCommitResponse(new HashMap<TopicPartition, Errors>()));
    client.prepareResponse(new MockClient.RequestMatcher() {

        @Override
        public boolean matches(AbstractRequest body) {
            leaveGroupRequested.set(true);
            LeaveGroupRequest leaveRequest = (LeaveGroupRequest) body;
            return leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE));
    coordinator.close();
    assertTrue("Commit not requested", commitRequested.get());
    if (dynamicAssignment)
        assertTrue("Leave group not requested", leaveGroupRequested.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaveGroupResponse(org.apache.kafka.common.requests.LeaveGroupResponse) OffsetCommitRequest(org.apache.kafka.common.requests.OffsetCommitRequest) HashMap(java.util.HashMap) OffsetCommitResponse(org.apache.kafka.common.requests.OffsetCommitResponse) AbstractRequest(org.apache.kafka.common.requests.AbstractRequest) LeaveGroupRequest(org.apache.kafka.common.requests.LeaveGroupRequest) MockClient(org.apache.kafka.clients.MockClient)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 MockClient (org.apache.kafka.clients.MockClient)3 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)3 LeaveGroupRequest (org.apache.kafka.common.requests.LeaveGroupRequest)3 LeaveGroupResponse (org.apache.kafka.common.requests.LeaveGroupResponse)3 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 OffsetCommitRequest (org.apache.kafka.common.requests.OffsetCommitRequest)1 OffsetCommitResponse (org.apache.kafka.common.requests.OffsetCommitResponse)1