コード例 #1
0
        private static string GetKafkaKey(ProducerPipelineContext context)
        {
            string?kafkaKeyHeaderValue = context.Envelope.Headers.GetValue(KafkaMessageHeaders.KafkaMessageKey);

            if (kafkaKeyHeaderValue != null)
            {
                return(kafkaKeyHeaderValue);
            }

            string?keyFromMessage = KafkaKeyHelper.GetMessageKey(context.Envelope.Message);

            if (keyFromMessage != null)
            {
                return(keyFromMessage);
            }

            var messageIdHeaderValue = context.Envelope.Headers.GetValue(DefaultMessageHeaders.MessageId);

            if (messageIdHeaderValue != null)
            {
                return(messageIdHeaderValue);
            }

            return(Guid.NewGuid().ToString());
        }
コード例 #2
0
 public void MessagesWithoutKey()
 {
     for (int i = 0; i < MessageCount; i++)
     {
         _ = KafkaKeyHelper.GetMessageKey(_messageWithoutKeys[i]);
     }
 }
コード例 #3
0
        public void GetMessageKey_NullMessage_NullIsReturned()
        {
            object?message = null;

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().BeNull();
        }
コード例 #4
0
        public void GetMessageKey_MessageWithoutProperties_NullIsReturned()
        {
            object message = new { };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().BeNull();
        }
コード例 #5
0
 public void MessagesWithSimpleKeyButNoValue()
 {
     for (int i = 0; i < MessageCount; i++)
     {
         MessageWithSimpleKey message = _messageWithSimpleKeys[i];
         message.MyKey = string.Empty;
         _             = KafkaKeyHelper.GetMessageKey(message);
     }
 }
コード例 #6
0
        private void SetPartitioningKey(IOutboundMessage outboundMessage)
        {
            var key = KafkaKeyHelper.GetMessageKey(outboundMessage.Content);

            if (key == null)
            {
                return;
            }

            outboundMessage.Headers.AddOrReplace(KafkaProducer.PartitioningKeyHeaderKey, key);
        }
コード例 #7
0
        public void GetMessageKey_MultipleKeyMembersMessage_NullIsReturned_WhenNullValues()
        {
            var message = new MultipleKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = null,
                Two   = null,
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().BeNull();
        }
コード例 #8
0
        public void GetMessageKey_SingleKeyMemberMessage_EmptyKeyResultsInNull()
        {
            var message = new SingleKeyMemberMessage
            {
                Id    = Guid.NewGuid(),
                One   = string.Empty,
                Two   = "2",
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().BeNull();
        }
コード例 #9
0
        public void GetMessageKey_SingleKeyMemberMessage_PropertyValueIsReturned()
        {
            var message = new SingleKeyMemberMessage
            {
                Id    = Guid.NewGuid(),
                One   = "1",
                Two   = "2",
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().Be("1");
        }
コード例 #10
0
        public void GetMessageKey_NoKeyMembersMessage_NullIsReturned()
        {
            var message = new NoKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = "1",
                Two   = "2",
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().BeNull();
        }
コード例 #11
0
        public void GetMessageKey_MultipleKeyMembersMessagesWithOneKeyEmpty_OneKeyIsReturned()
        {
            var message = new MultipleKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = "1",
                Two   = string.Empty,
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().Be("One=1");
        }
コード例 #12
0
        public void GetMessageKey_MultipleKeyMembersMessagesSecondEmpty_ComposedKeyIsReturned()
        {
            var message = new MultipleKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = string.Empty,
                Two   = "2",
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().Be("Two=2");
        }
コード例 #13
0
        public void GetMessageKey_MultipleKeyMembersMessagesWithSameKey_ComposedKeyIsReturned()
        {
            var message = new MultipleKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = "1",
                Two   = "2",
                Three = "3"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().Be("One=1,Two=2");
        }
コード例 #14
0
        public void GetMessageKey_LegacyKeyMemberAttribute_PropertyValueIsReturned()
        {
            var message = new LegacyKeyMemberAttributeMessage
            {
                Id    = Guid.NewGuid(),
                One   = "11",
                Two   = "22",
                Three = "33"
            };

            var key = KafkaKeyHelper.GetMessageKey(message);

            key.Should().Be("11");
        }
コード例 #15
0
        public void GetMessageKey_DifferentMessagesMixture_CorrectKeyIsReturned()
        {
            // This is actually to test the cache.
            var message1 = new MultipleKeyMembersMessage
            {
                Id    = Guid.NewGuid(),
                One   = "1",
                Two   = "2",
                Three = "3"
            };

            var message2 = new MultipleKeyMembersMessage
            {
                Id  = Guid.NewGuid(),
                One = "1",
                Two = "2",
            };

            var message3 = new MultipleKeyMembersMessage
            {
                Id  = Guid.NewGuid(),
                One = "1",
                Two = string.Empty,
            };

            var message4 = new MultipleKeyMembersMessage
            {
                Id  = Guid.NewGuid(),
                One = null,
                Two = "2"
            };

            var key1 = KafkaKeyHelper.GetMessageKey(message1);
            var key2 = KafkaKeyHelper.GetMessageKey(message2);
            var key3 = KafkaKeyHelper.GetMessageKey(message3);
            var key4 = KafkaKeyHelper.GetMessageKey(message4);

            key1.Should().Be("One=1,Two=2");
            key2.Should().Be("One=1,Two=2");
            key3.Should().Be("One=1");
            key4.Should().Be("Two=2");
        }