public void AdminClient_CreatePartitions(string bootstrapServers)
        {
            LogToFile("start AdminClient_CreatePartitions");

            var topicName1 = Guid.NewGuid().ToString();
            var topicName2 = Guid.NewGuid().ToString();
            var topicName3 = Guid.NewGuid().ToString();
            var topicName4 = Guid.NewGuid().ToString();
            var topicName5 = Guid.NewGuid().ToString();
            var topicName6 = Guid.NewGuid().ToString();

            // test creating a new partition works.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                 Name = topicName1, NumPartitions = 1, ReplicationFactor = 1
                                                                             } }).Wait();
                    adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> {
                        new PartitionsSpecification {
                            Topic = topicName1, IncreaseTo = 2
                        }
                    }).Wait();

                    var dr1 = producer.ProduceAsync(new TopicPartition(topicName1, 0), new Message <Null, Null>()).Result;
                    var dr2 = producer.ProduceAsync(new TopicPartition(topicName1, 1), new Message <Null, Null>()).Result;

                    try
                    {
                        producer.ProduceAsync(new TopicPartition(topicName1, 2), new Message <Null, Null>()).Wait();
                        Assert.True(false, "expecting exception");
                    }
                    catch (AggregateException ex)
                    {
                        Assert.IsType <ProduceException <Null, Null> >(ex.InnerException);
                        Assert.True(((ProduceException <Null, Null>)ex.InnerException).Error.IsError);
                    }
                }

            // check validate only works.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                 Name = topicName2, NumPartitions = 1, ReplicationFactor = 1
                                                                             } }).Wait();
                    adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> {
                        new PartitionsSpecification {
                            Topic = topicName2, IncreaseTo = 10
                        }
                    }, new CreatePartitionsOptions {
                        ValidateOnly = true
                    }).Wait();

                    // forces a metadata request.
                    var dr1 = producer.ProduceAsync(new TopicPartition(topicName2, 0), new Message <Null, Null>()).Result;
                    try
                    {
                        // since we have metadata, this throws immediately (i.e. not wrapped in AggregateException)
                        var dr2 = producer.ProduceAsync(new TopicPartition(topicName2, 1), new Message <Null, Null>()).Result;
                        Assert.True(false, "expecting exception");
                    }
                    catch (AggregateException ex)
                    {
                        Assert.IsType <ProduceException <Null, Null> >(ex.InnerException);
                        Assert.True(((ProduceException <Null, Null>)ex.InnerException).Error.IsError);
                    }
                }

            // check valid Assignments property value works.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                 Name = topicName3, NumPartitions = 1, ReplicationFactor = 1
                                                                             } }).Wait();
                    adminClient.CreatePartitionsAsync(
                        new List <PartitionsSpecification>
                    {
                        new PartitionsSpecification {
                            Topic = topicName2, IncreaseTo = 2, ReplicaAssignments = new List <List <int> > {
                                new List <int> {
                                    0
                                }
                            }
                        }
                    },
                        new CreatePartitionsOptions {
                        ValidateOnly = true
                    }
                        ).Wait();
                }

            // check invalid Assignments property value works.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification {
                                                                                 Name = topicName4, NumPartitions = 1, ReplicationFactor = 1
                                                                             } }).Wait();

                    try
                    {
                        adminClient.CreatePartitionsAsync(
                            new List <PartitionsSpecification>
                        {
                            new PartitionsSpecification {
                                Topic = topicName2, IncreaseTo = 2, ReplicaAssignments = new List <List <int> > {
                                    new List <int> {
                                        42
                                    }
                                }
                            }
                        },
                            new CreatePartitionsOptions {
                            ValidateOnly = true
                        }
                            ).Wait();
                        Assert.True(false, "Expecting exception");
                    }
                    catch (AggregateException ex)
                    {
                        Assert.True(ex.InnerException.GetType() == typeof(CreatePartitionsException));
                        var cpe = (CreatePartitionsException)ex.InnerException;
                        Assert.Single(cpe.Results);
                        Assert.True(cpe.Results.First().Error.IsError);
                    }
                }

            // more than one.
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                adminClient.CreateTopicsAsync(new TopicSpecification[]
                {
                    new TopicSpecification {
                        Name = topicName5, NumPartitions = 1, ReplicationFactor = 1
                    },
                    new TopicSpecification {
                        Name = topicName6, NumPartitions = 1, ReplicationFactor = 1
                    }
                }
                                              ).Wait();
                Thread.Sleep(TimeSpan.FromSeconds(1));

                // just a simple check there wasn't an exception.
                adminClient.CreatePartitionsAsync(
                    new List <PartitionsSpecification>
                {
                    new PartitionsSpecification {
                        Topic = topicName5, IncreaseTo = 2
                    },
                    new PartitionsSpecification {
                        Topic = topicName6, IncreaseTo = 3
                    }
                }
                    ).Wait();
            }

            Assert.Equal(0, Library.HandleCount);
            LogToFile("end   AdminClient_CreatePartitions");
        }
        public void AdminClient_CreateTopics(string bootstrapServers)
        {
            LogToFile("start AdminClient_CreateTopics");

            var topicName1 = Guid.NewGuid().ToString();
            var topicName2 = Guid.NewGuid().ToString();
            var topicName3 = Guid.NewGuid().ToString();
            var topicName4 = Guid.NewGuid().ToString();
            var topicName5 = Guid.NewGuid().ToString();

            // test
            //  - construction of admin client from configuration.
            //  - creation of more than one topic.
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                adminClient.CreateTopicsAsync(
                    new TopicSpecification[]
                {
                    new TopicSpecification {
                        Name = topicName1, NumPartitions = 2, ReplicationFactor = 1
                    },
                    new TopicSpecification {
                        Name = topicName2, NumPartitions = 12, ReplicationFactor = 1
                    }
                }
                    ).Wait();
            }

            // test
            //  - construction of admin client from a producer handle
            //  - creation of topic
            //  - producing to created topics works.
            using (var producer = new ProducerBuilder <Null, Null>(new ProducerConfig {
                BootstrapServers = bootstrapServers
            }).Build())
                using (var adminClient2 = new DependentAdminClientBuilder(producer.Handle).Build())
                {
                    adminClient2.CreateTopicsAsync(
                        new List <TopicSpecification> {
                        new TopicSpecification {
                            Name = topicName3, NumPartitions = 24, ReplicationFactor = 1
                        }
                    }).Wait();

                    var deliveryReport1 = producer.ProduceAsync(topicName1, new Message <Null, Null>()).Result;
                    var deliveryReport2 = producer.ProduceAsync(topicName2, new Message <Null, Null>()).Result;
                    var deliveryReport3 = producer.ProduceAsync(topicName3, new Message <Null, Null>()).Result;

                    Assert.Equal(topicName1, deliveryReport1.Topic);
                    Assert.Equal(topicName2, deliveryReport2.Topic);
                    Assert.Equal(topicName3, deliveryReport3.Topic);
                }

            // test
            //  - create topic with same name as existing topic
            //  - as well as another topic that does exist (and for which create should succeed).
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                try
                {
                    adminClient.CreateTopicsAsync(new List <TopicSpecification>
                    {
                        new TopicSpecification {
                            Name = topicName3, NumPartitions = 1, ReplicationFactor = 1
                        },
                        new TopicSpecification {
                            Name = topicName4, NumPartitions = 1, ReplicationFactor = 1
                        }
                    }
                                                  ).Wait();
                    Assert.True(false, "Expect CreateTopics request to throw an exception.");
                }

                // if awaited, the CreateTopicsException is not wrapped.
                // this is an annoyance if used synchronously, but not atypical.
                catch (AggregateException ex)
                {
                    Assert.True(ex.InnerException.GetType() == typeof(CreateTopicsException));
                    var cte = (CreateTopicsException)ex.InnerException;
                    Assert.Equal(2, cte.Results.Count);
                    Assert.Single(cte.Results.Where(r => r.Error.IsError));
                    Assert.Single(cte.Results.Where(r => !r.Error.IsError));
                    Assert.Equal(topicName3, cte.Results.Where(r => r.Error.IsError).First().Topic);
                    Assert.Equal(topicName4, cte.Results.Where(r => !r.Error.IsError).First().Topic);
                }
            }

            // test
            //  - validate only
            using (var adminClient = new AdminClientBuilder(new AdminClientConfig {
                BootstrapServers = bootstrapServers
            }).Build())
            {
                adminClient.CreateTopicsAsync(
                    new List <TopicSpecification> {
                    new TopicSpecification {
                        Name = topicName5, NumPartitions = 1, ReplicationFactor = 1
                    }
                },
                    new CreateTopicsOptions {
                    ValidateOnly = true, RequestTimeout = TimeSpan.FromSeconds(30)
                }
                    ).Wait();

                // creating for real shouldn't throw exception.
                adminClient.CreateTopicsAsync(
                    new List <TopicSpecification> {
                    new TopicSpecification {
                        Name = topicName5, NumPartitions = 1, ReplicationFactor = 1
                    }
                }
                    ).Wait();
            }

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