public async Task Complete_Sets_The_MigrationStatus_Correctly()
        {
            // Arrange
            var endpointName   = nameof(Marking_A_Batch_As_Complete_Updates_The_Status_Correctly);
            var timeoutsSource = new AspTimeoutsSource(connectionString, 1, containerName, fakeEndpointName, fakeEndpointTimeoutTableName, tablePrefix: tableNamePrefix);
            var runParameters  = new Dictionary <string, string> {
                { "Test", "TestValue" }
            };
            var cutOffDate = DateTime.UtcNow;

            var endpointTimeoutTableName = tableClient.GetTableReference($"{tableNamePrefix}{fakeEndpointTimeoutTableName}");
            await endpointTimeoutTableName.CreateIfNotExistsAsync();

            for (var x = 0; x < 3; x++)
            {
                var dateTime = cutOffDate.AddDays(random.Next(1, 5));

                var entity = new TimeoutDataEntity(dateTime.ToString(AspConstants.PartitionKeyScope), Guid.NewGuid().ToString())
                {
                    OwningTimeoutManager = endpointName,
                    Destination          = endpointName,
                    SagaId       = Guid.NewGuid(),
                    StateAddress = x.ToString(),
                    Time         = dateTime,
                    Headers      = "Headers",
                };

                await endpointTimeoutTableName.ExecuteAsync(TableOperation.Insert(entity));
            }

            var currentMigration = await timeoutsSource.Prepare(cutOffDate, endpointName, runParameters);

            for (var x = 0; x < currentMigration.NumberOfBatches; x++)
            {
                await timeoutsSource.MarkBatchAsCompleted(x + 1);
            }

            // Act
            await timeoutsSource.Complete();

            // Assert
            var loadedMigrationAfterCompletion = await timeoutsSource.TryLoadOngoingMigration();

            Assert.IsNull(loadedMigrationAfterCompletion);
        }
        public async Task Marking_A_Batch_As_Complete_Updates_The_Status_Correctly(BatchState batchState)
        {
            // Arrange
            var endpointName   = nameof(Marking_A_Batch_As_Complete_Updates_The_Status_Correctly);
            var timeoutsSource = new AspTimeoutsSource(connectionString, 1, containerName, fakeEndpointName, fakeEndpointTimeoutTableName, tablePrefix: tableNamePrefix);
            var runParameters  = new Dictionary <string, string> {
                { "Test", "TestValue" }
            };
            var cutOffDate = DateTime.UtcNow;

            var endpointTimeoutTableName = tableClient.GetTableReference($"{tableNamePrefix}{fakeEndpointTimeoutTableName}");
            await endpointTimeoutTableName.CreateIfNotExistsAsync();

            for (var x = 0; x < 3; x++)
            {
                var dateTime = cutOffDate.AddDays(random.Next(1, 5));
                var entity   = new TimeoutDataEntity(dateTime.ToString(AspConstants.PartitionKeyScope), Guid.NewGuid().ToString())
                {
                    OwningTimeoutManager = endpointName,
                    Destination          = endpointName,
                    SagaId       = Guid.NewGuid(),
                    StateAddress = x.ToString(),
                    Time         = dateTime,
                    Headers      = "Headers",
                };

                await endpointTimeoutTableName.ExecuteAsync(TableOperation.Insert(entity));
            }

            await timeoutsSource.Prepare(cutOffDate, endpointName, runParameters);

            // Act
            switch (batchState)
            {
            case BatchState.Staged:
                await timeoutsSource.MarkBatchAsStaged(1);

                break;

            case BatchState.Completed:
                await timeoutsSource.MarkBatchAsCompleted(1);

                break;

            case BatchState.Pending:
                break;

            default:
                return;
            }

            // Assert
            var query = new TableQuery <MigratedTimeoutDataEntity>()
                        .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, 1.ToString(CultureInfo.InvariantCulture)))
                        .Take(1);

            var migrationTableName = tableClient.GetTableReference($"{tableNamePrefix}{AspConstants.MigrationTableName}");

            var timeoutDataEntities = await migrationTableName.ExecuteQuerySegmentedAsync(query, null);

            Assert.True(timeoutDataEntities.All(t => t.BatchState == batchState), $"Expected all TimeoutEntity rows to have the batch state set to {batchState}");
        }