public async Task StartAsync_WithTestLockManager_TaskIsExecuted() { bool executed = false; var service = new TestDistributedBackgroundService(_ => { executed = true; return(Task.CompletedTask); }, new TestLockManager()); await service.StartAsync(CancellationToken.None); AsyncTestingUtil.Wait(() => executed); executed.Should().BeTrue(); }
public async Task StartAsync_WithDbLockManager_OnlyOneTaskIsExecutedSimultaneously() { bool executed1 = false; bool executed2 = false; var service1 = new TestDistributedBackgroundService( async stoppingToken => { executed1 = true; while (!stoppingToken.IsCancellationRequested) { await Task.Delay(10, stoppingToken); } }, _serviceProvider.GetRequiredService <DbDistributedLockManager>()); await service1.StartAsync(CancellationToken.None); await AsyncTestingUtil.WaitAsync(() => executed1); var service2 = new TestDistributedBackgroundService( _ => { 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(); }
public async Task StartAsync_WithTestLockManager_OnlyOneTaskIsExecutedSimultaneously() { var lockManager = new TestLockManager(); bool executed1 = false; bool executed2 = false; var service1 = new TestDistributedBackgroundService(async stoppingToken => { executed1 = true; while (!stoppingToken.IsCancellationRequested) { await Task.Delay(10, stoppingToken); } }, lockManager); await service1.StartAsync(CancellationToken.None); await AsyncTestingUtil.WaitAsync(() => executed1); var service2 = new TestDistributedBackgroundService(_ => { executed2 = true; return(Task.CompletedTask); }, lockManager); 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(); }