public async Task CleanProcessedAsync_should_clear_processed_messages() { var(db, _) = _fixture.CreateDbContext(); var sut = CreateSut(db); var messages = new[] { StartDummySaga.New(), StartDummySaga.New(), StartDummySaga.New() }; foreach (var message in messages) { await sut.AppendAsync(message); } var processedMessage = StartDummySaga.New(); await sut.AppendAsync(processedMessage); var lockId = await sut.LockAsync(processedMessage); await sut.ReleaseAsync(processedMessage, lockId); var filter = Builders <Entities.OutboxMessage> .Filter.Eq(e => e.Id, processedMessage.Id); var count = await db.Outbox.CountDocumentsAsync(filter); count.Should().Be(1); await sut.CleanProcessedAsync(); count = await db.Outbox.CountDocumentsAsync(filter); count.Should().Be(0); }
public async Task RunAsync_should_throw_SagaNotFoundException_if_saga_cannot_be_build() { var message = new StartDummySaga(Guid.NewGuid(), Guid.NewGuid()); var messageContext = NSubstitute.Substitute.For <IMessageContext <StartDummySaga> >(); messageContext.Message.Returns(message); var sagaFactory = NSubstitute.Substitute.For <ISagaFactory <DummySaga, DummySagaState> >(); var sagaStateService = NSubstitute.Substitute.For <ISagaStateService <DummySaga, DummySagaState> >(); var state = new DummySagaState(message.CorrelationId); sagaStateService.GetAsync(messageContext, Arg.Any <CancellationToken>()) .Returns((state, Guid.NewGuid())); var logger = NSubstitute.Substitute.For <ILogger <SagaRunner <DummySaga, DummySagaState> > >(); var uow = NSubstitute.Substitute.For <IUnitOfWork>(); var sut = new SagaRunner <DummySaga, DummySagaState>(sagaFactory, sagaStateService, uow, logger); await Assert.ThrowsAsync <SagaNotFoundException>(() => sut.RunAsync(messageContext, CancellationToken.None)); }
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 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>()); foreach (var message in messages) { await publisher.Received(1) .PublishAsync(message, Arg.Any <CancellationToken>()); await repo.ReleaseAsync(message, Arg.Any <Guid>(), Arg.Any <CancellationToken>()); } }
public async Task LockAsync_should_throw_if_message_not_found() { var message = StartDummySaga.New(); var sut = CreateSut(); await Assert.ThrowsAsync <LockException>(async() => await sut.LockAsync(message)); }
public async Task ReleaseAsync_should_throw_if_message_not_appended() { var message = StartDummySaga.New(); var sut = CreateSut(); await Assert.ThrowsAsync <ArgumentException>(async() => await sut.ReleaseAsync(message, Guid.NewGuid())); }