public async Task Complete_Errors() { await GenericTester .Test() .AddSingletonService(AgentTester.BuildConfiguration <Startup>("Beef")) .RunAsync(async() => { var config = ExecutionContext.GetService <IConfiguration>(); var db = new CdcDb(config.GetConnectionString("BeefDemo")); var script = "DELETE FROM [DemoCdc].[CdcIdentifierMapping]" + Environment.NewLine + "DELETE FROM [DemoCdc].[ContactOutbox]" + Environment.NewLine + "DECLARE @Lsn BINARY(10)" + Environment.NewLine + "SET @Lsn = sys.fn_cdc_get_max_lsn()" + Environment.NewLine + "INSERT INTO [DemoCdc].[ContactOutbox] ([CreatedDate], [ContactMinLsn], [ContactMaxLsn], [AddressMinLsn], [AddressMaxLsn], [IsComplete], [CompletedDate], [HasDataLoss]) VALUES('2021-01-01T00:00:00', @Lsn, @Lsn, @Lsn, @Lsn, 1, '2021-01-01T00:00:00', 0)" + Environment.NewLine + "SELECT TOP 1 * FROM [DemoCdc].[ContactOutbox]"; var outbox = await db.SqlStatement(script).SelectSingleAsync(DatabaseMapper.CreateAuto <CdcOutbox>()).ConfigureAwait(false); var cdc = new ContactCdcData(db, new NullEventPublisher(), ExecutionContext.GetService <ILogger <ContactCdcData> >(), new StringIdentifierGenerator()); // Attempt to execute where already complete. var cdor = await cdc.CompleteAsync(outbox.Id, new List <CdcTracker>()).ConfigureAwait(false); WriteResult(cdor); Assert.NotNull(cdor); Assert.IsFalse(cdor.IsSuccessful); Assert.NotNull(cdor.Exception); Assert.IsInstanceOf <BusinessException>(cdor.Exception); // Attempt to execute the CdcData with an invalid identifier. cdor = await cdc.CompleteAsync(outbox.Id + 1, new List <CdcTracker>()).ConfigureAwait(false); WriteResult(cdor); Assert.NotNull(cdor); Assert.IsFalse(cdor.IsSuccessful); Assert.NotNull(cdor.Exception); Assert.IsInstanceOf <NotFoundException>(cdor.Exception); // Make it incomplete and complete it. script = $"UPDATE [DemoCdc].[ContactOutbox] SET [IsComplete] = 0 WHERE [OutboxId] = {outbox.Id}"; await db.SqlStatement(script).NonQueryAsync().ConfigureAwait(false); cdor = await cdc.CompleteAsync(outbox.Id, new List <CdcTracker>()).ConfigureAwait(false); WriteResult(cdor); Assert.NotNull(cdor); Assert.IsTrue(cdor.IsSuccessful); Assert.IsNotNull(cdor.Outbox); Assert.AreEqual(true, cdor.Outbox.IsComplete); }); }