Example #1
0
        public static async Task KafkaSenderSendAsyncAddsSchemaIdToPayload()
        {
            var message      = "This is a message";
            var schemaId     = 100;
            var producerMock = new Mock <IProducer <string, byte[]> >();
            Message <string, byte[]>?sentMessage = null;

            producerMock
            .Setup(pm => pm.ProduceAsync(It.IsAny <string>(), It.IsAny <Message <string, byte[]> >(), It.IsAny <CancellationToken>()))
            .Callback <string, Message <string, byte[]>, CancellationToken>((str, msg, ct) => sentMessage = msg)
            .ReturnsAsync((DeliveryResult <string, byte[]>)null !);

            using var sender = new KafkaSender("name", "topic", schemaId, "servers");
            sender.Unlock()._producer = new Lazy <IProducer <string, byte[]> >(() => producerMock.Object);

            await sender.SendAsync(new SenderMessage(message)).ConfigureAwait(false);

            Assert.Equal(0, BitConverter.ToInt32(sentMessage !.Value, 0));
            Assert.Equal(schemaId, IPAddress.NetworkToHostOrder(BitConverter.ToInt32(sentMessage.Value, 1)));

            var memory = new Memory <byte>(sentMessage.Value, 5, sentMessage.Value.Length - 5);

            Assert.Equal(message, Encoding.UTF8.GetString(memory.ToArray()));
            Assert.Equal("Kafka", Encoding.UTF8.GetString(sentMessage.Headers[1].GetValueBytes()));
        }
Example #2
0
        public static async Task KafkaSenderSendAsyncSettingOriginatingSystemToKafka()
        {
            var message      = "This is a message";
            var producerMock = new Mock <IProducer <string, byte[]> >();

            producerMock
            .Setup(pm => pm.ProduceAsync(It.IsAny <string>(), It.IsAny <Message <string, byte[]> >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync((DeliveryResult <string, byte[]>)null !);

            using var sender = new KafkaSender("name", "topic", "servers");
            sender.Unlock()._producer = new Lazy <IProducer <string, byte[]> >(() => producerMock.Object);

            await sender.SendAsync(new SenderMessage(message)).ConfigureAwait(false);

            producerMock.Verify(pm => pm.ProduceAsync("topic",
                                                      It.Is <Message <string, byte[]> >(m => Encoding.UTF8.GetString(m.Value) == message && Encoding.UTF8.GetString(m.Headers[1].GetValueBytes()) == "Kafka"),
                                                      It.IsAny <CancellationToken>()));
        }