@Test
public void testMaybeLeaveGroup() {

    final String consumerId = "consumer";
    subscriptions.subscribe(Arrays.asList(topicName), rebalanceListener);
    subscriptions.needReassignment();
    client.prepareResponse(consumerMetadataResponse(node, Errors.NONE.code()));
    coordinator.ensureCoordinatorReady();
    client.prepareResponse(joinGroupFollowerResponse(1, consumerId, "leader", Errors.NONE.code()));
    client.prepareResponse(syncGroupResponse(Arrays.asList(tp), Errors.NONE.code()));
    coordinator.ensurePartitionAssignment();
    final AtomicBoolean received = new AtomicBoolean(false);
    client.prepareResponse(new MockClient.RequestMatcher() {
        @Override
        public boolean matches(ClientRequest request) {
            received.set(true);
            LeaveGroupRequest leaveRequest = new LeaveGroupRequest(request.request().body());
            return leaveRequest.memberId().equals(consumerId) &&
                    leaveRequest.groupId().equals(groupId);
        }
    }, new LeaveGroupResponse(Errors.NONE.code()).toStruct());
    coordinator.maybeLeaveGroup();
    assertTrue(received.get());
    assertEquals(JoinGroupRequest.UNKNOWN_MEMBER_ID, coordinator.memberId);
    assertEquals(OffsetCommitRequest.DEFAULT_GENERATION_ID, coordinator.generation);
}