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 StopAsync_SimpleTask_ExecutionStopped()
        {
            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);

            await service.StopAsync(CancellationToken.None);

            await Task.Delay(50);

            var executionsBeforeStop = executions;

            await Task.Delay(500);

            executions.Should().Be(executionsBeforeStop);
        }
        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();
        }