|
| 1 | +// Copyright 2023 Confluent Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// Refer to LICENSE for more information. |
| 16 | + |
| 17 | +using Xunit; |
| 18 | +using System; |
| 19 | +using Confluent.Kafka.Admin; |
| 20 | +using System.Collections.Generic; |
| 21 | + |
| 22 | + |
| 23 | +namespace Confluent.Kafka.UnitTests |
| 24 | +{ |
| 25 | + public class DescribeTopicsErrorTests |
| 26 | + { |
| 27 | + private readonly List<DescribeTopicsOptions> options = new List<DescribeTopicsOptions> |
| 28 | + { |
| 29 | + new DescribeTopicsOptions {}, |
| 30 | + new DescribeTopicsOptions { RequestTimeout = TimeSpan.FromMilliseconds(200) }, |
| 31 | + new DescribeTopicsOptions { IncludeAuthorizedOperations = true }, |
| 32 | + new DescribeTopicsOptions { RequestTimeout = TimeSpan.FromMilliseconds(200), IncludeAuthorizedOperations = false }, |
| 33 | + }; |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async void NullTopicCollection() |
| 37 | + { |
| 38 | + using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "localhost:90922" }).Build()) |
| 39 | + { |
| 40 | + foreach (var option in options) |
| 41 | + { |
| 42 | + await Assert.ThrowsAsync<NullReferenceException>(() => |
| 43 | + adminClient.DescribeTopicsAsync(null, option) |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public async void EmptyTopicCollection() |
| 51 | + { |
| 52 | + using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "localhost:90922" }).Build()) |
| 53 | + { |
| 54 | + foreach (var option in options) |
| 55 | + { |
| 56 | + var result = await adminClient.DescribeTopicsAsync( |
| 57 | + TopicCollection.OfTopicNames(new List<string> {}), |
| 58 | + option); |
| 59 | + Assert.Empty(result.TopicDescriptions); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + [Fact] |
| 66 | + public async void WrongTopicNames() |
| 67 | + { |
| 68 | + var wrongTopicCollections = new List<TopicCollection> |
| 69 | + { |
| 70 | + TopicCollection.OfTopicNames(new List<string> {""}), |
| 71 | + TopicCollection.OfTopicNames(new List<string> {"correct", ""}), |
| 72 | + }; |
| 73 | + using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "localhost:90922" }).Build()) |
| 74 | + { |
| 75 | + foreach (var option in options) |
| 76 | + { |
| 77 | + foreach (var collection in wrongTopicCollections) |
| 78 | + { |
| 79 | + await Assert.ThrowsAsync<KafkaException>(() => |
| 80 | + adminClient.DescribeTopicsAsync(collection, option) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public async void WrongRequestTimeoutValue() |
| 89 | + { |
| 90 | + var topicCollections = TopicCollection.OfTopicNames(new List<string> {}); |
| 91 | + var wrongRequestTimeoutValue = new DescribeTopicsOptions |
| 92 | + { |
| 93 | + RequestTimeout = TimeSpan.FromSeconds(-1) |
| 94 | + }; |
| 95 | + using (var adminClient = new AdminClientBuilder(new AdminClientConfig { BootstrapServers = "localhost:90922" }).Build()) |
| 96 | + { |
| 97 | + await Assert.ThrowsAsync<KafkaException>(() => |
| 98 | + adminClient.DescribeTopicsAsync(topicCollections, wrongRequestTimeoutValue) |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public async void LocalTimeout() |
| 105 | + { |
| 106 | + using (var adminClient = new AdminClientBuilder(new AdminClientConfig |
| 107 | + { |
| 108 | + BootstrapServers = "localhost:90922", |
| 109 | + SocketTimeoutMs = 10 |
| 110 | + }).Build()) |
| 111 | + { |
| 112 | + foreach (var option in options) |
| 113 | + { |
| 114 | + var ex = await Assert.ThrowsAsync<KafkaException>(() => |
| 115 | + adminClient.DescribeTopicsAsync( |
| 116 | + TopicCollection.OfTopicNames(new List<string> {"test"}), |
| 117 | + option) |
| 118 | + ); |
| 119 | + Assert.Equal("Failed while waiting for controller: Local: Timed out", ex.Message); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments