Search in sources :

Example 41 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class RetryTest method test.

@Test
public void test() throws ExecutionException {
    Exception[] attempts = new Exception[] { new IOException("pretend connect error"), new IOException("pretend timeout error"), new IOException("pretend read error"), // success!
    null };
    long retryWaitMs = 1000;
    long maxWaitMs = 10000;
    Retryable<String> call = createRetryable(attempts);
    Time time = new MockTime(0, 0, 0);
    assertEquals(0L, time.milliseconds());
    Retry<String> r = new Retry<>(time, retryWaitMs, maxWaitMs);
    r.execute(call);
    long secondWait = retryWaitMs * 2;
    long thirdWait = retryWaitMs * 4;
    long totalWait = retryWaitMs + secondWait + thirdWait;
    assertEquals(totalWait, time.milliseconds());
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 42 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class RetryTest method testIOExceptionFailure.

@Test
public void testIOExceptionFailure() {
    Exception[] attempts = new Exception[] { new IOException("pretend connect error"), new IOException("pretend timeout error"), new IOException("pretend read error"), new IOException("pretend another read error") };
    long retryWaitMs = 1000;
    long maxWaitMs = 1000 + 2000 + 3999;
    Retryable<String> call = createRetryable(attempts);
    Time time = new MockTime(0, 0, 0);
    assertEquals(0L, time.milliseconds());
    Retry<String> r = new Retry<>(time, retryWaitMs, maxWaitMs);
    assertThrows(ExecutionException.class, () -> r.execute(call));
    assertEquals(maxWaitMs, time.milliseconds());
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 43 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class RetryTest method testUseMaxTimeout.

@Test
public void testUseMaxTimeout() throws IOException {
    Exception[] attempts = new Exception[] { new IOException("pretend connect error"), new IOException("pretend timeout error"), new IOException("pretend read error") };
    long retryWaitMs = 5000;
    long maxWaitMs = 5000;
    Retryable<String> call = createRetryable(attempts);
    Time time = new MockTime(0, 0, 0);
    assertEquals(0L, time.milliseconds());
    Retry<String> r = new Retry<>(time, retryWaitMs, maxWaitMs);
    assertThrows(ExecutionException.class, () -> r.execute(call));
    assertEquals(maxWaitMs, time.milliseconds());
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) IOException(java.io.IOException) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 44 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class KafkaAdminClientTest method testListConsumerGroupsMetadataFailure.

@Test
public void testListConsumerGroupsMetadataFailure() throws Exception {
    final Cluster cluster = mockCluster(3, 0);
    final Time time = new MockTime();
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRIES_CONFIG, "0")) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        // Empty metadata causes the request to fail since we have no list of brokers
        // to send the ListGroups requests to
        env.kafkaClient().prepareResponse(RequestTestUtils.metadataResponse(Collections.emptyList(), env.cluster().clusterResource().clusterId(), -1, Collections.emptyList()));
        final ListConsumerGroupsResult result = env.adminClient().listConsumerGroups();
        TestUtils.assertFutureError(result.all(), KafkaException.class);
    }
}
Also used : Cluster(org.apache.kafka.common.Cluster) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 45 with Time

use of org.apache.kafka.common.utils.Time in project kafka by apache.

the class KafkaAdminClientTest method testDeleteConsumerGroupOffsetsNumRetries.

@Test
public void testDeleteConsumerGroupOffsetsNumRetries() throws Exception {
    final Cluster cluster = mockCluster(3, 0);
    final Time time = new MockTime();
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(time, cluster, AdminClientConfig.RETRIES_CONFIG, "0")) {
        final TopicPartition tp1 = new TopicPartition("foo", 0);
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        env.kafkaClient().prepareResponse(prepareOffsetDeleteResponse(Errors.NOT_COORDINATOR));
        env.kafkaClient().prepareResponse(prepareFindCoordinatorResponse(Errors.NONE, env.cluster().controller()));
        final DeleteConsumerGroupOffsetsResult result = env.adminClient().deleteConsumerGroupOffsets(GROUP_ID, Stream.of(tp1).collect(Collectors.toSet()));
        TestUtils.assertFutureError(result.all(), TimeoutException.class);
    }
}
Also used : TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) MockTime(org.apache.kafka.common.utils.MockTime) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

Time (org.apache.kafka.common.utils.Time)125 MockTime (org.apache.kafka.common.utils.MockTime)107 Test (org.junit.jupiter.api.Test)63 MockClient (org.apache.kafka.clients.MockClient)55 HashMap (java.util.HashMap)53 Cluster (org.apache.kafka.common.Cluster)41 Test (org.junit.Test)40 Node (org.apache.kafka.common.Node)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)31 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 Metadata (org.apache.kafka.clients.Metadata)28 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)25 TopicPartition (org.apache.kafka.common.TopicPartition)22 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)21 LogContext (org.apache.kafka.common.utils.LogContext)17 Map (java.util.Map)14 Properties (java.util.Properties)14 MetricName (org.apache.kafka.common.MetricName)14 ExecutionException (java.util.concurrent.ExecutionException)13