public async Task ProcessPendingMessagesAsync_should_do_nothing_when_no_pending_messages_available() { var repo = NSubstitute.Substitute.For <IOutboxRepository>(); var publisher = NSubstitute.Substitute.For <IPublisher>(); var logger = NSubstitute.Substitute.For <ILogger <OutboxProcessor> >(); var sut = new OutboxProcessor(repo, publisher, logger); var token = new CancellationTokenSource(TimeSpan.FromSeconds(2)); try { await sut.ProcessPendingMessagesAsync(token.Token); } catch (TaskCanceledException) { } await repo.Received().ReadMessagesToProcess(Arg.Any <CancellationToken>()); await publisher.DidNotReceiveWithAnyArgs().PublishAsync(Arg.Any <IMessage>(), Arg.Any <CancellationToken>()); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { using var scope = _serviceScopeFactory.CreateScope(); var logger = scope.ServiceProvider.GetService <ILogger <OutboxDispatcher <TDbContext> > >(); var outboxDataProvider = scope.ServiceProvider.GetService <IOutboxDataProvider <TDbContext> >(); var messageProcessorFactory = scope.ServiceProvider.GetService <IOutboxMessageProcessorFactory>(); var processorLogger = scope.ServiceProvider.GetService <ILogger <OutboxProcessor <TDbContext> > >(); var outboxProcessor = new OutboxProcessor <TDbContext>(outboxDataProvider, messageProcessorFactory, processorLogger); try { await outboxProcessor.Process(); } catch (Exception e) { logger.LogError(e, e.Message); } await Task.Delay(_outboxOptions.ProcessorDelay, stoppingToken); } }
public async Task StartAsync_process_pending_messages_() { var messages = new[] { StartDummySaga.New(), StartDummySaga.New(), StartDummySaga.New() }; var repo = NSubstitute.Substitute.For <IOutboxRepository>(); repo.ReadMessagesToProcess(Arg.Any <CancellationToken>()) .ReturnsForAnyArgs(messages); var publisher = NSubstitute.Substitute.For <IPublisher>(); var options = new OutboxProcessorOptions(TimeSpan.FromSeconds(2)); var logger = NSubstitute.Substitute.For <ILogger <OutboxProcessor> >(); var sut = new OutboxProcessor(repo, publisher, options, logger); var token = new CancellationTokenSource(options.Interval); try { await sut.StartAsync(token.Token); } catch (TaskCanceledException) { } await repo.Received().ReadMessagesToProcess(Arg.Any <CancellationToken>()); foreach (var message in messages) { await publisher.Received(1) .PublishAsync(message, Arg.Any <CancellationToken>()); await repo.MarkAsSentAsync(message, Arg.Any <CancellationToken>()); } }
public PollingPublisher(IOutboxUnitOfWorkFactory unitOfWorkFactory, IProducer producer) { _outboxProcessor = new OutboxProcessor(unitOfWorkFactory, producer); }