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())); }
public static void KafkaSenderDispose() { var producerMock = new Mock <IProducer <string, byte[]> >(); producerMock.Setup(pm => pm.Flush(It.IsAny <TimeSpan>())); producerMock.Setup(pm => pm.Dispose()); using var sender = new KafkaSender("name", "topic", "servers"); var senderUnlocked = sender.Unlock(); senderUnlocked._producer = new Lazy <IProducer <string, byte[]> >(() => producerMock.Object); _ = senderUnlocked._producer.Value; senderUnlocked.Dispose(); producerMock.Verify(pm => pm.Flush(It.IsAny <TimeSpan>()), Times.Once); producerMock.Verify(pm => pm.Dispose(), Times.Once); }
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>())); }
public static void CallAddStatisticsEmittedHandlerForSender() { using var sender = new KafkaSender("NAME", "TOPIC", 1, "SERVERS"); var statsData = "STATS!"; var callCount = 0; void Handler(object?s, string stats) { s.Should().BeSameAs(sender); stats.Should().Be(statsData); callCount++; } sender.AddStatisticsEmittedHandler(Handler); sender.Unlock().OnStatisticsEmitted(null, statsData); callCount.Should().Be(1, "Event handler should have been called"); }