Search in sources :

Example 16 with AdminClientUnitTestEnv

use of org.apache.kafka.clients.admin.AdminClientUnitTestEnv in project kafka by apache.

the class GetListOffsetsCallsBenchmark method setup.

@Setup(Level.Trial)
public void setup() {
    MetadataResponseData data = new MetadataResponseData();
    List<MetadataResponseTopic> mrTopicList = new ArrayList<>();
    Set<String> topics = new HashSet<>();
    for (int topicIndex = 0; topicIndex < topicCount; topicIndex++) {
        Uuid topicId = Uuid.randomUuid();
        String topicName = "topic-" + topicIndex;
        MetadataResponseTopic mrTopic = new MetadataResponseTopic().setTopicId(topicId).setName(topicName).setErrorCode((short) 0).setIsInternal(false);
        List<MetadataResponsePartition> mrPartitionList = new ArrayList<>();
        for (int partition = 0; partition < partitionCount; partition++) {
            TopicPartition tp = new TopicPartition(topicName, partition);
            topics.add(tp.topic());
            futures.put(tp, new KafkaFutureImpl<>());
            topicPartitionOffsets.put(tp, OffsetSpec.latest());
            MetadataResponsePartition mrPartition = new MetadataResponsePartition().setLeaderId(partition % numNodes).setPartitionIndex(partition).setIsrNodes(Arrays.asList(0, 1, 2)).setReplicaNodes(Arrays.asList(0, 1, 2)).setOfflineReplicas(Collections.emptyList()).setErrorCode((short) 0);
            mrPartitionList.add(mrPartition);
        }
        mrTopic.setPartitions(mrPartitionList);
        mrTopicList.add(mrTopic);
    }
    data.setTopics(new MetadataResponseData.MetadataResponseTopicCollection(mrTopicList.listIterator()));
    long deadline = 0L;
    short version = 0;
    context = new MetadataOperationContext<>(topics, new ListOffsetsOptions(), deadline, futures);
    context.setResponse(Optional.of(new MetadataResponse(data, version)));
    AdminClientUnitTestEnv adminEnv = new AdminClientUnitTestEnv(mockCluster());
    admin = (KafkaAdminClient) adminEnv.adminClient();
}
Also used : MetadataResponseTopic(org.apache.kafka.common.message.MetadataResponseData.MetadataResponseTopic) AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) ListOffsetsOptions(org.apache.kafka.clients.admin.ListOffsetsOptions) ArrayList(java.util.ArrayList) MetadataResponsePartition(org.apache.kafka.common.message.MetadataResponseData.MetadataResponsePartition) Uuid(org.apache.kafka.common.Uuid) TopicPartition(org.apache.kafka.common.TopicPartition) MetadataResponseData(org.apache.kafka.common.message.MetadataResponseData) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) HashSet(java.util.HashSet) Setup(org.openjdk.jmh.annotations.Setup)

Example 17 with AdminClientUnitTestEnv

use of org.apache.kafka.clients.admin.AdminClientUnitTestEnv in project kafka by apache.

the class TopicAdminTest method endOffsetsShouldReturnEmptyMapWhenPartitionsSetIsNull.

@Test
public void endOffsetsShouldReturnEmptyMapWhenPartitionsSetIsNull() {
    String topicName = "myTopic";
    Cluster cluster = createCluster(1, topicName, 1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        Map<TopicPartition, Long> offsets = admin.endOffsets(Collections.emptySet());
        assertTrue(offsets.isEmpty());
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 18 with AdminClientUnitTestEnv

use of org.apache.kafka.clients.admin.AdminClientUnitTestEnv in project kafka by apache.

the class TopicAdminTest method returnEmptyWithClusterAuthorizationFailureOnCreate.

@Test
public void returnEmptyWithClusterAuthorizationFailureOnCreate() {
    final NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().prepareResponse(createTopicResponseWithClusterAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        assertFalse(admin.createTopic(newTopic));
        env.kafkaClient().prepareResponse(createTopicResponseWithClusterAuthorizationException(newTopic));
        assertTrue(admin.createOrFindTopics(newTopic).isEmpty());
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 19 with AdminClientUnitTestEnv

use of org.apache.kafka.clients.admin.AdminClientUnitTestEnv in project kafka by apache.

the class TopicAdminTest method endOffsetsShouldReturnOffsetsForMultiplePartitions.

@Test
public void endOffsetsShouldReturnOffsetsForMultiplePartitions() {
    String topicName = "myTopic";
    TopicPartition tp1 = new TopicPartition(topicName, 0);
    TopicPartition tp2 = new TopicPartition(topicName, 1);
    Set<TopicPartition> tps = new HashSet<>(Arrays.asList(tp1, tp2));
    long offset1 = 1001;
    long offset2 = 1002;
    Cluster cluster = createCluster(1, topicName, 2);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
        env.kafkaClient().prepareResponse(prepareMetadataResponse(cluster, Errors.NONE));
        env.kafkaClient().prepareResponse(listOffsetsResult(tp1, offset1, tp2, offset2));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        Map<TopicPartition, Long> offsets = admin.endOffsets(tps);
        assertEquals(2, offsets.size());
        assertEquals(Long.valueOf(offset1), offsets.get(tp1));
        assertEquals(Long.valueOf(offset2), offsets.get(tp2));
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) TopicPartition(org.apache.kafka.common.TopicPartition) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with AdminClientUnitTestEnv

use of org.apache.kafka.clients.admin.AdminClientUnitTestEnv in project kafka by apache.

the class TopicAdminTest method verifyingTopicCleanupPolicyShouldReturnFalseWhenTopicAuthorizationError.

@Test
public void verifyingTopicCleanupPolicyShouldReturnFalseWhenTopicAuthorizationError() {
    final NewTopic newTopic = TopicAdmin.defineTopic("myTopic").partitions(1).compacted().build();
    Cluster cluster = createCluster(1);
    try (AdminClientUnitTestEnv env = new AdminClientUnitTestEnv(new MockTime(), cluster)) {
        env.kafkaClient().prepareResponse(describeConfigsResponseWithTopicAuthorizationException(newTopic));
        TopicAdmin admin = new TopicAdmin(null, env.adminClient());
        boolean result = admin.verifyTopicCleanupPolicyOnlyCompact("myTopic", "worker.topic", "purpose");
        assertFalse(result);
    }
}
Also used : AdminClientUnitTestEnv(org.apache.kafka.clients.admin.AdminClientUnitTestEnv) Cluster(org.apache.kafka.common.Cluster) NewTopic(org.apache.kafka.clients.admin.NewTopic) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Aggregations

AdminClientUnitTestEnv (org.apache.kafka.clients.admin.AdminClientUnitTestEnv)24 Cluster (org.apache.kafka.common.Cluster)23 Test (org.junit.Test)23 MockTime (org.apache.kafka.common.utils.MockTime)21 NewTopic (org.apache.kafka.clients.admin.NewTopic)15 TopicPartition (org.apache.kafka.common.TopicPartition)9 ConnectException (org.apache.kafka.connect.errors.ConnectException)6 Config (org.apache.kafka.clients.admin.Config)4 TopicConfig (org.apache.kafka.common.config.TopicConfig)4 TimeoutException (org.apache.kafka.common.errors.TimeoutException)4 UnsupportedVersionException (org.apache.kafka.common.errors.UnsupportedVersionException)4 ExecutionException (java.util.concurrent.ExecutionException)3 ConfigException (org.apache.kafka.common.config.ConfigException)3 ClusterAuthorizationException (org.apache.kafka.common.errors.ClusterAuthorizationException)3 TopicAuthorizationException (org.apache.kafka.common.errors.TopicAuthorizationException)3 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 ListOffsetsOptions (org.apache.kafka.clients.admin.ListOffsetsOptions)1 Uuid (org.apache.kafka.common.Uuid)1 MetadataResponseData (org.apache.kafka.common.message.MetadataResponseData)1