public async Task PauseAndResume_SimpleTask_ExecutionPausedAndResumed()
        {
            int executions = 0;

            var service = new TestRecurringDistributedBackgroundService(
                _ =>
            {
                executions++;
                return(Task.CompletedTask);
            },
                _serviceProvider.GetRequiredService <DbDistributedLockManager>());
            await service.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executions > 1);

            executions.Should().BeGreaterThan(0);

            service.Pause();
            await Task.Delay(50);

            var executionsBeforeStop = executions;

            await Task.Delay(500);

            executions.Should().Be(executionsBeforeStop);

            service.Resume();

            await AsyncTestingUtil.WaitAsync(() => executions > executionsBeforeStop);

            executions.Should().BeGreaterThan(executionsBeforeStop);
        }
        public async Task StartAsync_WithDbLockManager_OnlyOneTaskIsExecutedSimultaneously()
        {
            bool executed1 = false;
            bool executed2 = false;

            var service1 = new TestRecurringDistributedBackgroundService(stoppingToken =>
            {
                executed1 = true;
                return(Task.CompletedTask);
            }, new DbDistributedLockManager(_servicesProvider));
            await service1.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed1);

            var service2 = new TestRecurringDistributedBackgroundService(_ =>
            {
                executed2 = true;
                return(Task.CompletedTask);
            }, new DbDistributedLockManager(_servicesProvider));
            await service2.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed2, 100);

            executed1.Should().BeTrue();
            executed2.Should().BeFalse();

            await service1.StopAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed2);

            executed2.Should().BeTrue();
        }
        public async Task StartAsync_SimpleTask_TaskExecutedMultipleTimes()
        {
            int executions = 0;

            var service = new TestRecurringDistributedBackgroundService(_ =>
            {
                executions++;
                return(Task.CompletedTask);
            }, new DbDistributedLockManager(_servicesProvider));
            await service.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executions > 1);

            executions.Should().BeGreaterThan(1);
        }
        public async Task StartAsync_WithDbLockManager_TaskIsExecuted()
        {
            bool executed = false;

            var service = new TestRecurringDistributedBackgroundService(_ =>
            {
                executed = true;
                return(Task.CompletedTask);
            }, new DbDistributedLockManager(_servicesProvider));
            await service.StartAsync(CancellationToken.None);

            AsyncTestingUtil.Wait(() => executed);

            executed.Should().BeTrue();
        }
        public async Task StartAsync_SimpleTask_TaskExecutedMultipleTimes()
        {
            int executions = 0;

            var service = new TestRecurringDistributedBackgroundService(_ =>
            {
                executions++;
                return(Task.CompletedTask);
            }, new TestLockManager());
            await service.StartAsync(CancellationToken.None);

            await Task.Delay(100);

            executions.Should().BeGreaterThan(1);
        }
        public async Task StopAsync_SimpleTask_ExecutionStopped()
        {
            int executions = 0;

            var service = new TestRecurringDistributedBackgroundService(_ =>
            {
                executions++;
                return(Task.CompletedTask);
            }, new DbDistributedLockManager(_servicesProvider));
            await service.StartAsync(CancellationToken.None);

            await Task.Delay(100);

            await service.StopAsync(CancellationToken.None);

            var executionsBeforeStop = executions;

            await Task.Delay(200);

            executions.Should().Be(executionsBeforeStop);
        }
        public async Task StartAsync_WithDbLockManager_OnlyOneTaskIsExecutedSimultaneously()
        {
            bool executed1 = false;
            bool executed2 = false;

            var service1 = new TestRecurringDistributedBackgroundService(
                _ =>
            {
                executed1 = true;
                return(Task.CompletedTask);
            },
                _serviceProvider.GetRequiredService <DbDistributedLockManager>());
            await service1.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed1);

            var service2 = new TestRecurringDistributedBackgroundService(
                _ =>
            {
                executed2 = true;
                return(Task.CompletedTask);
            },
                _serviceProvider.GetRequiredService <DbDistributedLockManager>());
            await service2.StartAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed2, TimeSpan.FromMilliseconds(100));

            executed1.Should().BeTrue();
            executed2.Should().BeFalse();

            await service1.StopAsync(CancellationToken.None);

            await AsyncTestingUtil.WaitAsync(() => executed2);

            executed2.Should().BeTrue();
        }