public void when_stop_and_fail_on_commit_it_should_log_exception_but_not_throw() { var container = new Container().AddLogging(_testOutput); var tokenSource = new CancellationTokenSource(); var cancellationToken = tokenSource.Token; var consumerMock = new Mock <IConsumer <int, string> >(); consumerMock.SetupGet(consumer => consumer.Name).Returns(string.Empty); consumerMock.SetupGet(consumer => consumer.MemberId).Returns(string.Empty); consumerMock.Setup(consumer => consumer.Consume(cancellationToken)).Returns(() => MakeConsumeResult()); consumerMock.Setup(consumer => consumer.Close()).Throws <Exception>(); var apiConsumer = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); apiConsumer.Start(result => Task.CompletedTask, cancellationToken); tokenSource.Cancel(); Action sut = () => apiConsumer.Stop(); sut.Should().NotThrow("no exception should be thrown even some occurred"); consumerMock.Verify(consumer => consumer.Close(), Times.Once()); var logEvents = InMemorySink.Instance.LogEvents.ToList(); logEvents.Any(log => log.Level == LogEventLevel.Information && log.RenderMessage().Contains("error")) .Should().BeTrue("exception should be logged"); }
public async Task when_kafka_exception_thrown_in_start_it_should_fail() { var container = new Container().AddLogging(_testOutput); var committed = 0; var tokenSource = new CancellationTokenSource(); var consumerMock = new Mock <IConsumer <int, string> >(); consumerMock .Setup(consumer => consumer.Consume(tokenSource.Token)) .Throws(new ConsumeException(new ConsumeResult <byte[], byte[]>(), new Error(ErrorCode.InvalidMsg, "Test Kafka error"))); consumerMock.Setup(consumer => consumer.Commit(It.IsAny <ConsumeResult <int, string> >())).Callback(() => committed++); var received = 0; Func <ConsumeResult <int, string>, Task> onMessageReceived = consumeResult => { received++; return(Task.CompletedTask); }; var apiConsumer = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); var result = apiConsumer.Start(onMessageReceived, tokenSource.Token); await result; // result.IsFaulted.Should().BeTrue(); received.Should().Be(expected: 0); committed.Should().Be(expected: 0, "no messages should be committed"); }
public async Task when_start_with_valid_arguments_it_should_commit_kafka_message() { var container = new Container().AddLogging(_testOutput); var committed = 0; var tokenSource = new CancellationTokenSource(); var consumerMock = new Mock <IConsumer <int, string> >(); consumerMock.Setup(consumer => consumer.Consume(tokenSource.Token)).Returns(() => MakeConsumeResult()); consumerMock.Setup(consumer => consumer.Commit(It.IsAny <ConsumeResult <int, string> >())).Callback(() => committed++); var received = 0; const int numberOfMessages = 2; Func <ConsumeResult <int, string>, Task> onMessageReceived = result => { received++; if (received == numberOfMessages) { tokenSource.Cancel(); } return(Task.CompletedTask); }; var sut = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); await sut.Start(onMessageReceived, tokenSource.Token); received.Should().Be(numberOfMessages); committed.Should().Be(numberOfMessages, "all messages should be committed"); }
public async Task when_start_and_eof_it_should_skip_message() { var container = new Container().AddLogging(_testOutput); var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(value: 500)); var consumerMock = new Mock <IConsumer <int, string> >(); var consumeResult = MakeConsumeResult(result => result.IsPartitionEOF = true); consumerMock .Setup(consumer => consumer.Consume(tokenSource.Token)) .Returns(() => consumeResult); var received = 0; Func <ConsumeResult <int, string>, Task> onMessageReceived = result => { received++; return(Task.CompletedTask); }; var sut = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); await sut.Start(onMessageReceived, tokenSource.Token); received.Should().Be(expected: 0); }
public void when_dispose_and_exception_thrown_it_should_ignore_them() { var container = new Container().AddLogging(_testOutput); var consumerMock = new Mock <IConsumer <int, string> >(); consumerMock.Setup(consumer => consumer.Commit()).Throws <Exception>(); var apiConsumer = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); Action sut = () => apiConsumer.Dispose(); sut.Should().NotThrow("Exceptions should be ignored."); consumerMock.Setup(consumer => consumer.Close()).Throws <Exception>(); sut.Should().NotThrow("Exceptions should be ignored."); }
public void when_dispose_it_should_commit_offsets_close_connection_and_dispose_consumer() { var tokenSource = new CancellationTokenSource(); var cancellationToken = tokenSource.Token; var container = new Container().AddLogging(_testOutput); var consumerMock = new Mock <IConsumer <int, string> >(); consumerMock.SetupGet(consumer => consumer.Name).Returns(string.Empty); consumerMock.SetupGet(consumer => consumer.MemberId).Returns(string.Empty); var loggerFactoryMock = new Mock <ILoggerFactory>(); loggerFactoryMock.Setup(factory => factory.CreateLogger(It.IsAny <string>())).Returns(Mock.Of <ILogger>); var sut = new DefaultApiConsumer <int, string>( Mock.Of <IIngressApi>(), consumerMock.Object, container.GetInstance <ILogger <DefaultApiConsumer <int, string> > >()); sut.Start(result => Task.CompletedTask, cancellationToken); tokenSource.Cancel(); sut.Dispose(); consumerMock.Verify(consumer => consumer.Close(), Times.Once()); consumerMock.Verify(consumer => consumer.Dispose(), Times.Once()); }