private async Task RenewAsync(IWorkProvider provider, WorkItem item, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { try { await Task.Delay(TimeSpan.FromSeconds(_quidjiboConfiguration.LockInterval), cancellationToken); await provider.RenewAsync(item, cancellationToken); _logger.LogDebug("Renewed : {0}", item.Id); } catch (OperationCanceledException) { // ignore OperationCanceledExceptions } } }
public async Task WorkLoopAsyncTest(int throttle, bool singleLoop) { // Arrange var testQueue = new ConcurrentQueue <WorkItem>(); var completedWork = new ConcurrentBag <WorkItem>(); var defaultItems = GenFu.GenFu.ListOf <WorkItem>(); defaultItems.ForEach(x => { x.Queue = "default"; testQueue.Enqueue(x); }); var primaryItems = GenFu.GenFu.ListOf <WorkItem>(); primaryItems.ForEach(x => { x.Queue = "primary"; testQueue.Enqueue(x); }); var secondaryItems = GenFu.GenFu.ListOf <WorkItem>(); secondaryItems.ForEach(x => { x.Queue = "secondary"; testQueue.Enqueue(x); }); _quidjiboConfiguration.EnableWorker.Returns(true); _quidjiboConfiguration.Throttle.Returns(throttle); _quidjiboConfiguration.LockInterval.Returns(60); _quidjiboConfiguration.SingleLoop.Returns(singleLoop); _quidjiboConfiguration.Queues.Returns(new[] { "default", "primary", "secondary" }); _workProvider.ReceiveAsync(Arg.Any <string>(), Arg.Any <CancellationToken>()) .Returns(x => testQueue.TryDequeue(out var item) ? Task.FromResult(new List <WorkItem> { item }) : Task.FromResult(new List <WorkItem>(0))); _workProvider.RenewAsync(Arg.Any <WorkItem>(), Arg.Any <CancellationToken>()).Returns(Task.FromResult(DateTime.UtcNow.AddMinutes(1))); _workProvider.CompleteAsync(Arg.Any <WorkItem>(), Arg.Any <CancellationToken>()) .Returns(x => { completedWork.Add(x.Arg <WorkItem>()); return(Task.CompletedTask); }); // Act _sut.Start(); // Assert while (!testQueue.IsEmpty && !_cts.IsCancellationRequested) { } testQueue.Count.Should().Be(0); if (singleLoop) { await _workProviderFactory.Received(1).CreateAsync(Arg.Any <string>(), Arg.Any <CancellationToken>()); } else { await _workProviderFactory.Received(1).CreateAsync(Arg.Is <string>(x => x == "default"), Arg.Any <CancellationToken>()); await _workProviderFactory.Received(1).CreateAsync(Arg.Is <string>(x => x == "primary"), Arg.Any <CancellationToken>()); await _workProviderFactory.Received(1).CreateAsync(Arg.Is <string>(x => x == "secondary"), Arg.Any <CancellationToken>()); } await _pipeline.ReceivedWithAnyArgs(75).StartAsync(Arg.Any <IQuidjiboContext>(), Arg.Any <CancellationToken>()); }