public async Task StopAsyncWithoutStartAsyncNoops()
        {
            var tcs     = new TaskCompletionSource <object>();
            var service = new MyBackgroundService(tcs.Task);

            await service.StopAsync(CancellationToken.None);
        }
        public async Task StartReturnsLongRunningTaskIfFailed()
        {
            var tcs = new TaskCompletionSource <object>();

            tcs.TrySetException(new Exception("fail!"));
            var service = new MyBackgroundService(tcs.Task);

            var exception = await Assert.ThrowsAsync <Exception>(() => service.StartAsync(CancellationToken.None));

            Assert.Equal("fail!", exception.Message);
        }
        public void StartReturnsCompletedTaskIfCancelled()
        {
            var tcs = new TaskCompletionSource <object>();

            tcs.TrySetCanceled();
            var service = new MyBackgroundService(tcs.Task);

            var task = service.StartAsync(CancellationToken.None);

            Assert.True(task.IsCompleted);
        }
        public async Task StopAsyncStopsBackgroundService()
        {
            var tcs     = new TaskCompletionSource <object>();
            var service = new MyBackgroundService(tcs.Task);

            await service.StartAsync(CancellationToken.None);

            Assert.False(service.ExecuteTask.IsCompleted);

            await service.StopAsync(CancellationToken.None);

            Assert.True(service.ExecuteTask.IsCompleted);
        }
        public void StartReturnsCompletedTaskIfLongRunningTaskIsIncomplete()
        {
            var tcs     = new TaskCompletionSource <object>();
            var service = new MyBackgroundService(tcs.Task);

            var task = service.StartAsync(CancellationToken.None);

            Assert.True(task.IsCompleted);
            Assert.False(tcs.Task.IsCompleted);

            // Complete the task
            tcs.TrySetResult(null);
        }
 public MyClass(IHostedServiceAccessor <MyBackgroundService> accessor)
 {
     _service = accessor.Service ?? throw new ArgumentNullException(nameof(accessor));
 }