コード例 #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);
                }
        }
コード例 #2
0
        private Metadata.IResponse HandleMetadataRequest(Metadata.IRequest req)
        {
            switch (req)
            {
            case Metadata.ListTopics _:
                return(new Metadata.Topics(Try <List <TopicMetadata> >
                                           .From(() =>
                {
                    using (var adminClient = new DependentAdminClientBuilder(_consumer.Handle).Build())
                    {
                        return adminClient.GetMetadata(_settings.MetadataRequestTimeout).Topics;
                    }
                })));

            default:
                throw new InvalidOperationException($"Unknown metadata request: {req}");
            }
        }
コード例 #3
0
        public void Producer_Handles(string bootstrapServers)
        {
            LogToFile("start Producer_Handles");

            var producerConfig = new ProducerConfig {
                BootstrapServers = bootstrapServers
            };

            using (var topic = new TemporaryTopic(bootstrapServers, 1))
            {
                using (var producer1 = new ProducerBuilder <byte[], byte[]>(producerConfig).Build())
                    using (var producer2 = new DependentProducerBuilder <string, string>(producer1.Handle).Build())
                        using (var producer3 = new DependentProducerBuilder <byte[], byte[]>(producer1.Handle).Build())
                            using (var producer4 = new DependentProducerBuilder <int, string>(producer2.Handle).Build())
                                using (var producer5 = new DependentProducerBuilder <int, int>(producer3.Handle).Build())
                                    using (var producer6 = new DependentProducerBuilder <string, byte[]>(producer4.Handle).Build())
                                        using (var producer7 = new ProducerBuilder <double, double>(producerConfig).Build())
                                            using (var adminClient = new DependentAdminClientBuilder(producer7.Handle).Build())
                                            {
                                                var r1 = producer1.ProduceAsync(topic.Name, new Message <byte[], byte[]> {
                                                    Key = new byte[] { 42 }, Value = new byte[] { 33 }
                                                }).Result;
                                                Assert.Equal(new byte[] { 42 }, r1.Key);
                                                Assert.Equal(new byte[] { 33 }, r1.Value);
                                                Assert.Equal(0, r1.Offset);

                                                var r2 = producer2.ProduceAsync(topic.Name, new Message <string, string> {
                                                    Key = "hello", Value = "world"
                                                }).Result;
                                                Assert.Equal("hello", r2.Key);
                                                Assert.Equal("world", r2.Value);

                                                var r3 = producer3.ProduceAsync(topic.Name, new Message <byte[], byte[]> {
                                                    Key = new byte[] { 40 }, Value = new byte[] { 31 }
                                                }).Result;
                                                Assert.Equal(new byte[] { 40 }, r3.Key);
                                                Assert.Equal(new byte[] { 31 }, r3.Value);

                                                var r4 = producer4.ProduceAsync(topic.Name, new Message <int, string> {
                                                    Key = 42, Value = "mellow world"
                                                }).Result;
                                                Assert.Equal(42, r4.Key);
                                                Assert.Equal("mellow world", r4.Value);

                                                var r5 = producer5.ProduceAsync(topic.Name, new Message <int, int> {
                                                    Key = int.MaxValue, Value = int.MinValue
                                                }).Result;
                                                Assert.Equal(int.MaxValue, r5.Key);
                                                Assert.Equal(int.MinValue, r5.Value);

                                                var r6 = producer6.ProduceAsync(topic.Name, new Message <string, byte[]> {
                                                    Key = "yellow mould", Value = new byte[] { 69 }
                                                }).Result;
                                                Assert.Equal("yellow mould", r6.Key);
                                                Assert.Equal(new byte[] { 69 }, r6.Value);

                                                var r7 = producer7.ProduceAsync(topic.Name, new Message <double, double> {
                                                    Key = 44.0, Value = 234.4
                                                }).Result;
                                                Assert.Equal(44.0, r7.Key);
                                                Assert.Equal(234.4, r7.Value);

                                                var topicMetadata = adminClient.GetMetadata(singlePartitionTopic, TimeSpan.FromSeconds(10));
                                                Assert.Single(topicMetadata.Topics);

                                                // implicitly check this does not throw.
                                            }

                var consumerConfig = new ConsumerConfig {
                    BootstrapServers = bootstrapServers, GroupId = Guid.NewGuid().ToString()
                };

                using (var consumer = new ConsumerBuilder <byte[], byte[]>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 0));
                    var r1 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal(new byte[] { 42 }, r1.Key);
                    Assert.Equal(new byte[] { 33 }, r1.Value);
                    Assert.Equal(0, r1.Offset);
                }

                using (var consumer = new ConsumerBuilder <string, string>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 1));
                    var r2 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal("hello", r2.Key);
                    Assert.Equal("world", r2.Value);
                    Assert.Equal(1, r2.Offset);
                }

                using (var consumer = new ConsumerBuilder <byte[], byte[]>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 2));
                    var r3 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal(new byte[] { 40 }, r3.Key);
                    Assert.Equal(new byte[] { 31 }, r3.Value);
                    Assert.Equal(2, r3.Offset);
                }

                using (var consumer = new ConsumerBuilder <int, string>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 3));
                    var r4 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal(42, r4.Key);
                    Assert.Equal("mellow world", r4.Value);
                    Assert.Equal(3, r4.Offset);
                }

                using (var consumer = new ConsumerBuilder <int, int>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 4));
                    var r5 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal(int.MaxValue, r5.Key);
                    Assert.Equal(int.MinValue, r5.Value);
                    Assert.Equal(4, r5.Offset);
                }

                using (var consumer = new ConsumerBuilder <string, byte[]>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 5));
                    var r6 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal("yellow mould", r6.Key);
                    Assert.Equal(new byte[] { 69 }, r6.Value);
                    Assert.Equal(5, r6.Offset);
                }

                using (var consumer = new ConsumerBuilder <double, double>(consumerConfig).Build())
                {
                    consumer.Assign(new TopicPartitionOffset(topic.Name, 0, 6));
                    var r7 = consumer.Consume(TimeSpan.FromSeconds(10));
                    Assert.Equal(44.0, r7.Key);
                    Assert.Equal(234.4, r7.Value);
                    Assert.Equal(6, r7.Offset);
                }
            }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   Producer_Handles");
        }
        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");
        }