コード例 #1
0
ファイル: AddBroker.cs プロジェクト: wppqqppq/kafka-dotnet
        public void AddBrokers(string bootstrapServers)
        {
            var producerConfig = new ProducerConfig {
                BootstrapServers = "localhost:65533"
            };

            using (var producer = new ProducerBuilder <Null, string>(producerConfig).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        var metadata = adminClient.GetMetadata(TimeSpan.FromSeconds(3));
                        Assert.True(false, "Broker should not be reached here");
                    }
                    catch (KafkaException e)
                    {
                        Assert.Equal(ErrorCode.Local_Transport, e.Error.Code);
                    }

                    // test is > 0 note == 1 since bootstrapServers could include more than one broker.
                    int brokersAdded = adminClient.AddBrokers(bootstrapServers);
                    Assert.True(brokersAdded > 0, "Should have added one broker or more");

                    brokersAdded = adminClient.AddBrokers(bootstrapServers);
                    Assert.True(brokersAdded > 0, "Should have added one broker or more (duplicates considered added)");

                    var newMetadata = adminClient.GetMetadata(TimeSpan.FromSeconds(3));
                    Assert.True(newMetadata.Brokers.Count >= 1);

                    brokersAdded = adminClient.AddBrokers("");
                    Assert.True(brokersAdded == 0, "Should not have added brokers");

                    newMetadata = adminClient.GetMetadata(TimeSpan.FromSeconds(3));
                    Assert.True(newMetadata.Brokers.Count > 0);
                }
        }
        public void AdminClient_NullReferenceChecks(string bootstrapServers)
        {
            LogToFile("start AdminClient_NullReferenceChecks");
            var    topicName1 = Guid.NewGuid().ToString();
            string nullTopic  = null;

            Exception createTopicsException     = null;
            Exception createPartitionsException = null;

            // test creating a null topic throws a related exception
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                     Name = nullTopic, NumPartitions = 1, ReplicationFactor = 1
                                                                                 } }).Wait();
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentException ex)
                    {
                        Assert.Contains("topic", ex.Message.ToLower());
                        createTopicsException = ex;
                    }
                }

            // test creating a partition with null topic throws exception
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                     Name = topicName1, NumPartitions = 1, ReplicationFactor = 1
                                                                                 } }).Wait();
                        adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> {
                            new PartitionsSpecification {
                                Topic = nullTopic, IncreaseTo = 2
                            }
                        }).Wait();
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentException ex)
                    {
                        Assert.Contains("topic", ex.Message.ToLower());
                        createPartitionsException = ex;
                    }
                }

            Assert.True(createTopicsException != null && createPartitionsException != null);
            Assert.True(createTopicsException.GetType() == createPartitionsException.GetType(), ".CreateTopic and .CreatePartition should have consistent interface for null-related exceptions.");

            // test adding a null list of brokers throws null reference exception.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.AddBrokers(null);
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentNullException ex)
                    {
                        Assert.Contains("broker", ex.Message.ToLower());
                    }
                }

            // test retrieving metadata for a null topic
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.GetMetadata(null, TimeSpan.FromSeconds(10));
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentNullException ex)
                    {
                        Assert.Contains("value cannot be null", ex.Message.ToLower());
                    }
                }

            // Deleting null topic throws exception
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.DeleteTopicsAsync(new List <string> {
                            topicName1, nullTopic
                        });
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentException ex)
                    {
                        Assert.Contains("topic", ex.Message);
                    }
                }

            // ListGroup throws exception if group is null
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    try
                    {
                        adminClient.ListGroup(null, TimeSpan.FromSeconds(10));
                        Assert.True(false, "Expected exception.");
                    }
                    catch (ArgumentNullException ex)
                    {
                        Assert.Contains("group", ex.Message);
                    }
                }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   AdminClient_NullReferenceChecks");
        }