forked from confluentinc/confluent-kafka-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddBroker.cs
111 lines (96 loc) · 4.21 KB
/
AddBroker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2016-2017 Confluent Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Refer to LICENSE for more information.
using System;
using System.Text;
using System.Collections.Generic;
using System.Threading.Tasks;
using Confluent.Kafka.Serialization;
using Xunit;
namespace Confluent.Kafka.IntegrationTests
{
public static partial class Tests
{
/// <summary>
/// Test that produces a message then consumes it.
/// </summary>
[Theory, MemberData(nameof(KafkaParameters))]
public static void AddBrokers(string bootstrapServers, string singlePartitionTopic, string partitionedTopic)
{
// This test assumes broker v0.10.0 or higher:
// https://github.com/edenhill/librdkafka/wiki/Broker-version-compatibility
// This test does a broker metadata request, as it's an easy way to see
// if we are connected to broker. It's not really the best way to test this
// (ideally, we would get from a working list of brokers to all brokers
// that have changed IP) but this will be good enough.
var producerConfig = new Dictionary<string, object>
{
{ "bootstrap.servers", "unknown" },
{ "api.version.request", true }
};
var consumerConfig = new Dictionary<string, object>
{
{ "group.id", Guid.NewGuid().ToString() },
{ "bootstrap.servers", "unknown" },
{ "session.timeout.ms", 6000 },
{ "api.version.request", true }
};
using (var typedProducer = new Producer<Null, string>(producerConfig, null, new StringSerializer(Encoding.UTF8)))
{
TestMetadata(
() => typedProducer.GetMetadata(false, null, TimeSpan.FromSeconds(3)),
typedProducer.AddBrokers);
}
using (var producer = new Producer(producerConfig))
{
TestMetadata(
() => producer.GetMetadata(false, null, TimeSpan.FromSeconds(3)),
producer.AddBrokers);
}
using (var consumer = new Consumer(consumerConfig))
{
TestMetadata(
() => consumer.GetMetadata(false, TimeSpan.FromSeconds(3)),
consumer.AddBrokers);
}
using (var typedConsumer = new Consumer<Null, Null>(consumerConfig, new NullDeserializer(), new NullDeserializer()))
{
TestMetadata(
() => typedConsumer.GetMetadata(false, TimeSpan.FromSeconds(3)),
typedConsumer.AddBrokers);
}
void TestMetadata(Func<Metadata> getMetadata, Func<string, int> addBrokers)
{
try
{
var metadata = getMetadata();
Assert.True(false, "Broker should not be reached here");
}
catch (KafkaException e)
{
Assert.Equal(ErrorCode.Local_Transport, e.Error.Code);
}
int brokersAdded = addBrokers(bootstrapServers);
Assert.True(brokersAdded > 0, "Should have added one broker or more");
brokersAdded = addBrokers(bootstrapServers);
Assert.True(brokersAdded > 0, "Should have added one broker or more (duplicates considered added)");
var newMetadata = getMetadata();
Assert.True(newMetadata.Brokers.Count > 0);
brokersAdded = addBrokers("");
Assert.True(brokersAdded == 0, "Should not have added brokers");
}
}
}
}