Search in sources :

Example 21 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class KafkaConsumerTest method verifyNoCoordinatorLookupForManualAssignmentWithSeek.

@Test
public void verifyNoCoordinatorLookupForManualAssignmentWithSeek() {
    int rebalanceTimeoutMs = 60000;
    int sessionTimeoutMs = 3000;
    int heartbeatIntervalMs = 2000;
    int autoCommitIntervalMs = 1000;
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster(topic, 1);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    PartitionAssignor assignor = new RoundRobinAssignor();
    final KafkaConsumer<String, String> consumer = newConsumer(time, client, metadata, assignor, rebalanceTimeoutMs, sessionTimeoutMs, heartbeatIntervalMs, true, autoCommitIntervalMs);
    consumer.assign(Arrays.asList(tp0));
    consumer.seekToBeginning(Arrays.asList(tp0));
    // there shouldn't be any need to lookup the coordinator or fetch committed offsets.
    // we just lookup the starting position and send the record fetch.
    client.prepareResponse(listOffsetsResponse(Collections.singletonMap(tp0, 50L), Errors.NONE));
    client.prepareResponse(fetchResponse(tp0, 50L, 5));
    ConsumerRecords<String, String> records = consumer.poll(0);
    assertEquals(5, records.count());
    assertEquals(55L, consumer.position(tp0));
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) PartitionAssignor(org.apache.kafka.clients.consumer.internals.PartitionAssignor) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Example 22 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class AbstractCoordinatorTest method setupCoordinator.

@Before
public void setupCoordinator() {
    this.mockTime = new MockTime();
    this.mockClient = new MockClient(mockTime);
    Metadata metadata = new Metadata();
    this.consumerClient = new ConsumerNetworkClient(mockClient, metadata, mockTime, RETRY_BACKOFF_MS, REQUEST_TIMEOUT_MS);
    Metrics metrics = new Metrics();
    Cluster cluster = TestUtils.singletonCluster("topic", 1);
    metadata.update(cluster, Collections.<String>emptySet(), mockTime.milliseconds());
    this.node = cluster.nodes().get(0);
    mockClient.setNode(node);
    this.coordinatorNode = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    this.coordinator = new DummyCoordinator(consumerClient, metrics, mockTime);
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Before(org.junit.Before)

Example 23 with MockClient

use of org.apache.kafka.clients.MockClient in project kafka by apache.

the class ConsumerNetworkClientTest method sendExpiry.

@Test
public void sendExpiry() throws InterruptedException {
    long unsentExpiryMs = 10;
    final AtomicBoolean isReady = new AtomicBoolean();
    final AtomicBoolean disconnected = new AtomicBoolean();
    client = new MockClient(time) {

        @Override
        public boolean ready(Node node, long now) {
            if (isReady.get())
                return super.ready(node, now);
            else
                return false;
        }

        @Override
        public boolean connectionFailed(Node node) {
            return disconnected.get();
        }
    };
    // Queue first send, sleep long enough for this to expire and then queue second send
    consumerClient = new ConsumerNetworkClient(client, metadata, time, 100, unsentExpiryMs);
    RequestFuture<ClientResponse> future1 = consumerClient.send(node, heartbeat());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    assertFalse(future1.isDone());
    time.sleep(unsentExpiryMs + 1);
    RequestFuture<ClientResponse> future2 = consumerClient.send(node, heartbeat());
    assertEquals(2, consumerClient.pendingRequestCount());
    assertEquals(2, consumerClient.pendingRequestCount(node));
    assertFalse(future2.isDone());
    // First send should have expired and second send still pending
    consumerClient.poll(0);
    assertTrue(future1.isDone());
    assertFalse(future1.succeeded());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    assertFalse(future2.isDone());
    // Enable send, the un-expired send should succeed on poll
    isReady.set(true);
    client.prepareResponse(heartbeatResponse(Errors.NONE));
    consumerClient.poll(future2);
    ClientResponse clientResponse = future2.value();
    HeartbeatResponse response = (HeartbeatResponse) clientResponse.responseBody();
    assertEquals(Errors.NONE, response.error());
    // Disable ready flag to delay send and queue another send. Disconnection should remove pending send
    isReady.set(false);
    RequestFuture<ClientResponse> future3 = consumerClient.send(node, heartbeat());
    assertEquals(1, consumerClient.pendingRequestCount());
    assertEquals(1, consumerClient.pendingRequestCount(node));
    disconnected.set(true);
    consumerClient.poll(0);
    assertTrue(future3.isDone());
    assertFalse(future3.succeeded());
    assertEquals(0, consumerClient.pendingRequestCount());
    assertEquals(0, consumerClient.pendingRequestCount(node));
}
Also used : ClientResponse(org.apache.kafka.clients.ClientResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HeartbeatResponse(org.apache.kafka.common.requests.HeartbeatResponse) Node(org.apache.kafka.common.Node) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.Test)

Aggregations

MockClient (org.apache.kafka.clients.MockClient)23 Metadata (org.apache.kafka.clients.Metadata)19 Node (org.apache.kafka.common.Node)19 MockTime (org.apache.kafka.common.utils.MockTime)19 Cluster (org.apache.kafka.common.Cluster)17 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)16 Time (org.apache.kafka.common.utils.Time)16 Test (org.junit.Test)16 HashMap (java.util.HashMap)11 LinkedHashMap (java.util.LinkedHashMap)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 TopicPartition (org.apache.kafka.common.TopicPartition)6 GroupCoordinatorResponse (org.apache.kafka.common.requests.GroupCoordinatorResponse)5 Metrics (org.apache.kafka.common.metrics.Metrics)3 AbstractRequest (org.apache.kafka.common.requests.AbstractRequest)3 Before (org.junit.Before)3 ClientRequest (org.apache.kafka.clients.ClientRequest)2 WakeupException (org.apache.kafka.common.errors.WakeupException)2 HeartbeatResponse (org.apache.kafka.common.requests.HeartbeatResponse)2 ProtocolMetadata (org.apache.kafka.common.requests.JoinGroupRequest.ProtocolMetadata)2