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

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

            tcs.TrySetCanceled();

            var service = new TestProjector(tcs.Task);
            var task    = service.StartAsync(CancellationToken.None);

            task.IsCompleted.Should().BeTrue();
        }
        public void WhenStartedThenShouldReturnCompletedTaskIfLongRunningTaskIsIncomplete()
        {
            var tcs     = new TaskCompletionSource <object>();
            var service = new TestProjector(tcs.Task);
            var task    = service.StartAsync(CancellationToken.None);

            task.IsCompleted.Should().BeTrue();
            tcs.Task.IsCompleted.Should().BeFalse();

            // Complete the tcs task
            tcs.TrySetResult(null);
        }
        public async Task WhenStopCalledThenShouldAlsoStopBackgroundService()
        {
            var tcs     = new TaskCompletionSource <object>();
            var service = new TestProjector(tcs.Task);

            await service.StartAsync(CancellationToken.None);

            service.ExecutingTask.IsCompleted.Should().BeFalse();

            await service.StopAsync(CancellationToken.None);

            service.ExecutingTask.IsCompleted.Should().BeTrue();
        }
        public void WhenFailedThenStartShouldReturnLongRunningTask()
        {
            var tcs = new TaskCompletionSource <object>();

            tcs.TrySetException(new Exception("Failure!"));

            var service = new TestProjector(tcs.Task);

            Func <Task> act = async() => await service.StartAsync(CancellationToken.None);

            act.Should().Throw <Exception>()
            .And.Message.Should().Be("Failure!");
        }