public void ProduceAsyncShouldRethrowProducerException() { // Arrange var topic = "my-topic"; var message = new AgnosticMessage <string> { Key = "msg-id", Payload = "msg-content" }; var producerMock = new Mock <IProducer <string, string> >(); producerMock.Setup(producer => producer.ProduceAsync(topic, It.Is <Message <string, string> >(msg => msg.Key == message.Key))) .ThrowsAsync(new ProduceException <string, string>(new Error(ErrorCode.BrokerNotAvailable), null)); var loggerMock = new Mock <ILogger <KafkaProducerWrapper <string> > >(); using (var kafkaProducerWrapper = new KafkaProducerWrapper <string>(producerMock.Object, loggerMock.Object)) { // Act Func <Task> methodCall = () => kafkaProducerWrapper.ProduceAsync(topic, message); // Assert methodCall.Should().Throw <ProduceException <string, string> >(); } }
public void ProduceAsyncShouldSendMessageToKafkaTopic() { // Arrange var topic = "my-topic"; var message = new AgnosticMessage <string> { Key = "msg-id", Payload = "msg-content" }; var producerMock = new Mock <IProducer <string, string> >(); producerMock.Setup(producer => producer.ProduceAsync(topic, It.Is <Message <string, string> >(msg => msg.Key == message.Key))) .ReturnsAsync(new DeliveryResult <string, string>()); var loggerMock = new Mock <ILogger <KafkaProducerWrapper <string> > >(); // Act using (var kafkaProducerWrapper = new KafkaProducerWrapper <string>(producerMock.Object, loggerMock.Object)) { kafkaProducerWrapper.ProduceAsync(topic, message).Wait(); } // Assert producerMock.Verify(producer => producer.ProduceAsync(topic, It.Is <Message <string, string> >(msg => msg.Key == message.Key)), Times.Once); }
private Message <string, TPayload> MapFromAgnosticMessage(AgnosticMessage <TPayload> agnosticMessage) { return(new Message <string, TPayload> { Key = agnosticMessage.Key, Value = agnosticMessage.Payload }); }
public async Task ExecuteAsync() { for (int i = 0; i < settings.Value.ElementsToProduce; i++) { var message = new AgnosticMessage <string> { Key = $"msg-{i}", Payload = $"msg-{i}-content" }; await producer.ProduceAsync("text", message); logger.LogInformation($"String sent: {message.Payload}"); } }
public async Task ExecuteAsync() { foreach (var filePath in fileClient.GetFiles(settings.Value.InputDirectory)) { var message = new AgnosticMessage <byte[]> { Key = filePath.Split('/').Last(), Payload = fileClient.Read(filePath) }; await producer.ProduceAsync("binary", message); logger.LogInformation($"Binary file read and sent: {message.Key}"); } }
public async Task ProduceAsync(string topic, AgnosticMessage <TPayload> message) { var kafkaMessage = MapFromAgnosticMessage(message); try { var result = await kafkaProducer.ProduceAsync(topic, kafkaMessage); logger.LogInformation($"Message produced to: {result.TopicPartitionOffset}"); } catch (ProduceException <string, TPayload> ex) { logger.LogError($"Failed to produce message: {ex.Error.Reason}"); throw; } }
public async Task ExecuteAsync() { for (int i = 0; i < settings.Value.ElementsToProduce; i++) { var domainObject = new SampleComplexObject(i); var message = new AgnosticMessage <string> { Key = domainObject.Id.ToString(), Payload = JsonConvert.SerializeObject(domainObject) //TODO: change serialization method }; await producer.ProduceAsync("json", message); logger.LogInformation($"JSON object sent: {domainObject.GetFullDescription()}"); } }
public void ExecuteShouldCreateAFileWhenAMessageIsConsumed() { // Arrange var outputDirectory = "/my-var"; var optionsMock = new Mock <IOptions <ApplicationSettings> >(); optionsMock.SetupGet(options => options.Value) .Returns(new ApplicationSettings { OutputDirectory = outputDirectory }); var consumedMessageMock = new AgnosticMessage <byte[]> { Key = "file1", Payload = new byte[] {} }; var agnosticConsumerMock = new Mock <IAgnosticConsumer <byte[]> >(); agnosticConsumerMock.Setup(a => a.Consume("binary", It.IsAny <Action <AgnosticMessage <byte[]> > >())) .Callback <string, Action <AgnosticMessage <byte[]> > >((_, action) => { action(consumedMessageMock); }); var fileClientMock = new Mock <IFileClient>(); var loggerMock = new Mock <ILogger <ConsumeBinary> >(); var consumeBinaryUseCase = new ConsumeBinary(agnosticConsumerMock.Object, fileClientMock.Object, optionsMock.Object, loggerMock.Object); // Act consumeBinaryUseCase.Execute(); // Assert fileClientMock.Verify(fileClient => fileClient.Create(outputDirectory, consumedMessageMock.Key, consumedMessageMock.Payload), Times.Once); }