public async Task CancelsStopWhenAnyThrows()
        {
            var service1 = Substitute.For <IBackgroundService>();

            service1.StopAsync(Arg.Any <CancellationToken>())
            .Returns(
                async ci =>
            {
                await Task.Delay(500);
                ci.ArgAt <CancellationToken>(0)
                .IsCancellationRequested.Should()
                .BeTrue();
            });

            var service2 = Substitute.For <IBackgroundService>();

            service2.StopAsync(Arg.Any <CancellationToken>())
            .Throws(new InvalidOperationException());

            var host = new BackgroundServiceHost(
                new[] { service1, service2 },
                Substitute.For <ILogger <BackgroundServiceHost> >());

            await Assert.ThrowsAsync <InvalidOperationException>(
                async() => await host.StopAsync(CancellationToken.None));
        }
        public async Task StopThrowsForAny()
        {
            var service1 = Substitute.For <IBackgroundService>();

            service1.StopAsync(Arg.Any <CancellationToken>())
            .Throws(new InvalidOperationException());

            var service2 = Substitute.For <IBackgroundService>();

            var host = new BackgroundServiceHost(
                new[] { service1, service2 },
                Substitute.For <ILogger <BackgroundServiceHost> >());

            await Assert.ThrowsAsync <InvalidOperationException>(
                async() => await host.StopAsync(CancellationToken.None));
        }
        public async Task StopsAll()
        {
            var service1 = Substitute.For <IBackgroundService>();
            var service2 = Substitute.For <IBackgroundService>();

            var host = new BackgroundServiceHost(
                new[] { service1, service2 },
                Substitute.For <ILogger <BackgroundServiceHost> >());

            await host.StopAsync(CancellationToken.None);

            await service1.Received(1)
            .StopAsync(Arg.Any <CancellationToken>());

            await service2.Received(1)
            .StopAsync(Arg.Any <CancellationToken>());
        }