public void CanUseThrowOnCancel() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); schedulerToTest.QueueWork((ct) => { while (true) { ct.ThrowIfCancellationRequested(); Thread.Sleep(1); } ; }); Thread.Sleep(500); ((IHostedService)schedulerToTest).Stop(); _logger.Verify(logger => logger.Log(LogLevel.Information, It.IsAny <EventId>(), It.IsAny <object>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.AtLeastOnce()); _logger.Verify(logger => logger.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <object>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Never()); }
public void CannotQueueWorkAfterStop() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); schedulerToTest.Stop(); Assert.Throws <InvalidOperationException>(() => schedulerToTest.QueueWork((ct) => { return; })); }
private void PersistFile() { _backgroundWorkScheduler.QueueWork((cancellationToken) => { if (!cancellationToken.IsCancellationRequested) { File.WriteAllText(_filePath, JsonConvert.SerializeObject(_items)); } }); _backgroundWorkScheduler.Start(); }
public void CanShutdownWellBehavingOnGoingWork() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); bool complete = false; schedulerToTest.QueueWork((ct) => { ct.WaitHandle.WaitOne(); complete = true; }); Thread.Sleep(500); ((IHostedService)schedulerToTest).Stop(); Assert.True(complete); }
public void CanThrowInBackgroundTask() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); schedulerToTest.QueueWork((ct) => { throw new Exception(); }); //Give time for task to actually start....yuck. Thread.Sleep(500); ((IHostedService)schedulerToTest).Stop(); //TODO: Needing to check logging is probably an indicator //that some refactoring would help. _logger.Verify(logger => logger.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <object>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.AtLeastOnce()); }
public void CannotShutdownIgnorantTaskLogsError() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); schedulerToTest.QueueWork((ct) => { while (true) { Thread.Sleep(1); } ; }); Thread.Sleep(500); ((IHostedService)schedulerToTest).Stop(); _logger.Verify(logger => logger.Log(LogLevel.Error, It.IsAny <EventId>(), It.IsAny <object>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.AtLeastOnce()); }
public void EnqueueThrowsWhenWorkNull() { var schedulerToTest = new BackgroundWorkScheduler(_logger.Object, _options.Object); Assert.Throws <ArgumentNullException>(() => schedulerToTest.QueueWork(null)); }