Exemplo n.º 1
0
        public async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    await Task.Delay(_pollingInterval, stoppingToken);

                    bool success;
                    int  retrievedInstanceCount;
                    do
                    {
                        (success, retrievedInstanceCount) = await _deleteService.CleanupDeletedInstancesAsync(stoppingToken);
                    }while (success && retrievedInstanceCount == _batchSize);
                }
                catch (OperationCanceledException) when(stoppingToken.IsCancellationRequested)
                {
                    // Cancel requested.
                    throw;
                }
                catch (Exception ex)
                {
                    // The job failed.
                    _logger.LogCritical(ex, "Unhandled exception in the deleted instance cleanup worker.");
                }
            }
        }
        public async Task GivenANumberOfDeletedEntriesAndBatchSize_WhenCallingExecute_ThenDeleteShouldBeCalledCorrectNumberOfTimes(int numberOfDeletedInstances, int expectedDeleteCount)
        {
            (bool, int) GenerateCleanupDeletedInstancesAsyncResponse()
            {
                var returnValue = Math.Min(numberOfDeletedInstances, BatchSize);

                numberOfDeletedInstances = Math.Max(numberOfDeletedInstances - BatchSize, 0);

                if (numberOfDeletedInstances == 0)
                {
                    _cancellationTokenSource.Cancel();
                }

                return(true, returnValue);
            }

            _deleteService.CleanupDeletedInstancesAsync().ReturnsForAnyArgs(
                x => GenerateCleanupDeletedInstancesAsyncResponse());

            await _deletedInstanceCleanupWorker.ExecuteAsync(_cancellationTokenSource.Token);

            await _deleteService.ReceivedWithAnyArgs(expectedDeleteCount).CleanupDeletedInstancesAsync();
        }