Ejemplo n.º 1
0
        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 AdminClient(producer.Handle))
                {
                    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);
                }
        }