SelectBrokerRouteFromLocalCache() public method

Select a broker for a given topic using the IPartitionSelector function.
Thrown if the topic metadata does not exist in the cache.
public SelectBrokerRouteFromLocalCache ( string topic, byte key = null ) : KafkaNet.BrokerRoute
topic string The topic to retreive a broker route for.
key byte The key used by the IPartitionSelector to collate to a consistent partition. Null value means key will be ignored in selection process.
return KafkaNet.BrokerRoute
        public async Task OffsetCommitShouldStoreAndReturnSuccess()
        {
            const int partitionId = 0;
            using (var router = new BrokerRouter(Options))
            {
                await router.RefreshMissingTopicMetadata(IntegrationConfig.IntegrationTopic);
                var conn = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationTopic, partitionId);

                var commit = CreateOffsetCommitRequest(IntegrationConfig.IntegrationConsumer, partitionId, 10);
                var response = conn.Connection.SendAsync(commit).Result.FirstOrDefault();

                Assert.That(response, Is.Not.Null);
                Assert.That(response.Error, Is.EqualTo((int)ErrorResponseCode.NoError));
            }
        }
        public async Task EnsureGzipCompressedMessageCanSend()
        {
            IntegrationConfig.NoDebugLog.InfoFormat(IntegrationConfig.Highlight("start EnsureGzipCompressedMessageCanSend"));
            using (var conn = GetKafkaConnection())
            {
                conn.SendAsync(new MetadataRequest
                {
                    Topics = new List<string>(new[] { IntegrationConfig.IntegrationCompressionTopic })
                })
                    .Wait(TimeSpan.FromSeconds(10));
            }

            using (var router = new BrokerRouter(_options))
            {
                IntegrationConfig.NoDebugLog.InfoFormat(IntegrationConfig.Highlight("start RefreshMissingTopicMetadata"));
                await router.RefreshMissingTopicMetadata(IntegrationConfig.IntegrationCompressionTopic);
                IntegrationConfig.NoDebugLog.InfoFormat(IntegrationConfig.Highlight("end RefreshMissingTopicMetadata"));
                var conn = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationCompressionTopic, 0);

                var request = new ProduceRequest
                {
                    Acks = 1,
                    TimeoutMS = 1000,
                    Payload = new List<Payload>
                        {
                            new Payload
                            {
                                Codec = MessageCodec.CodecGzip,
                                Topic = IntegrationConfig.IntegrationCompressionTopic,
                                Partition = 0,
                                Messages = new List<Message>
                                {
                                    new Message("0", "1"),
                                    new Message("1", "1"),
                                    new Message("2", "1")
                                }
                            }
                        }
                };
                IntegrationConfig.NoDebugLog.InfoFormat(IntegrationConfig.Highlight("start SendAsync"));
                var response = conn.Connection.SendAsync(request).Result;
                IntegrationConfig.NoDebugLog.InfoFormat("end SendAsync");
                Assert.That(response.First().Error, Is.EqualTo(0));
                IntegrationConfig.NoDebugLog.InfoFormat("start dispose");
            }
            IntegrationConfig.NoDebugLog.InfoFormat(IntegrationConfig.Highlight("end EnsureGzipCompressedMessageCanSend"));
        }
        public void OffsetFetchRequestOfNonExistingGroupShouldReturnNoError()
        {
            //From documentation: https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+ProtocolTests#AGuideToTheKafkaProtocol-OffsetFetchRequest
            //Note that if there is no offset associated with a topic-partition under that consumer group the broker does not set an error code
            //(since it is not really an error), but returns empty metadata and sets the offset field to -1.
            const int partitionId = 0;
            using (var router = new BrokerRouter(Options))
            {
                var request = CreateOffsetFetchRequest(Guid.NewGuid().ToString(), partitionId);

                var conn = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationTopic, partitionId);

                var response = conn.Connection.SendAsync(request).Result.FirstOrDefault();

                Assert.That(response, Is.Not.Null);
                Assert.That(response.Error, Is.EqualTo((int)ErrorResponseCode.NoError));
                Assert.That(response.Offset, Is.EqualTo(-1));
            }
        }
        public void OffsetCommitShouldStoreMetadata()
        {
            const int partitionId = 0;
            const long offset = 101;
            const string metadata = "metadata";

            using (var router = new BrokerRouter(Options))
            {
                var conn = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationTopic, partitionId);

                var commit = CreateOffsetCommitRequest(IntegrationConfig.IntegrationConsumer, partitionId, offset, metadata);
                var commitResponse = conn.Connection.SendAsync(commit).Result.FirstOrDefault();

                Assert.That(commitResponse, Is.Not.Null);
                Assert.That(commitResponse.Error, Is.EqualTo((int)ErrorResponseCode.NoError));

                var fetch = CreateOffsetFetchRequest(IntegrationConfig.IntegrationConsumer, partitionId);
                var fetchResponse = conn.Connection.SendAsync(fetch).Result.FirstOrDefault();

                Assert.That(fetchResponse, Is.Not.Null);
                Assert.That(fetchResponse.Error, Is.EqualTo((int)ErrorResponseCode.NoError));
                Assert.That(fetchResponse.Offset, Is.EqualTo(offset));
                Assert.That(fetchResponse.MetaData, Is.EqualTo(metadata));
            }
        }
        public void ConsumerMetadataRequestShouldReturnWithoutError()
        {
            using (var router = new BrokerRouter(Options))
            {
                var conn = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationTopic);

                var request = new ConsumerMetadataRequest { ConsumerGroup = IntegrationConfig.IntegrationConsumer };

                var response = conn.Connection.SendAsync(request).Result.FirstOrDefault();

                Assert.That(response, Is.Not.Null);
                Assert.That(response.Error, Is.EqualTo((int)ErrorResponseCode.NoError));
            }
        }
 private static string GetKey_PartitionIdChosenByKeyIsNotEqualToPartitionId(BrokerRouter router,
     int partitionId)
 {
     string key = null;
     bool partitionEqualsPartitionIdChoosesByKey = true;
     while (partitionEqualsPartitionIdChoosesByKey)
     {
         var guidKey = Guid.NewGuid();
         key = guidKey.ToString();
         int partitionIdChoosesByKey = router.SelectBrokerRouteFromLocalCache(IntegrationConfig.IntegrationTopic, guidKey.ToByteArray()).PartitionId;
         partitionEqualsPartitionIdChoosesByKey = partitionId == partitionIdChoosesByKey;
     }
     return key;
 }