public void TestSlowCancellation()
        {
            MessageLog log = new MessageLog();

            NodeServiceCollection services = new NodeServiceCollection();

            TestNodeServiceFactory service1 = new TestNodeServiceFactory(log, TimeSpan.FromMilliseconds(200), false);
            TestNodeServiceFactory service2 = new TestNodeServiceFactory(log, TimeSpan.FromMilliseconds(200), false);

            services.AddFactory(service1);
            services.AddFactory(service2);

            CancellationTokenSource cts = new CancellationTokenSource();

            services.CreateServices(null, cts.Token);

            services.Start();

            cts.Cancel();

            Stopwatch sw = Stopwatch.StartNew();

            Assert.False(services.Join(TimeSpan.FromMilliseconds(150)));

            Assert.That(sw.Elapsed, Is.GreaterThanOrEqualTo(TimeSpan.FromMilliseconds(130)));
            Assert.That(sw.Elapsed, Is.LessThanOrEqualTo(TimeSpan.FromMilliseconds(170)));

            Assert.That(log.GetLog(), Is.EqualTo(new string[]
            {
                "Service created.",
                "Service created.",
                "Service started.",
                "Service started."
            }));

            Assert.True(services.Join(TimeSpan.FromMilliseconds(100)));

            Assert.That(sw.Elapsed, Is.GreaterThanOrEqualTo(TimeSpan.FromMilliseconds(180)));
            Assert.That(sw.Elapsed, Is.LessThanOrEqualTo(TimeSpan.FromMilliseconds(220)));

            services.DisposeServices();

            Assert.That(log.GetLog(), Is.EqualTo(new string[]
            {
                "Service created.",
                "Service created.",
                "Service started.",
                "Service started.",
                "Service stopped.",
                "Service stopped.",
                "Service disposed.",
                "Service disposed."
            }));
        }
        public void TestBrokenDispose()
        {
            MessageLog log = new MessageLog();

            NodeServiceCollection services = new NodeServiceCollection();

            TestNodeServiceFactory service1 = new TestNodeServiceFactory(log, TimeSpan.Zero, true);
            TestNodeServiceFactory service2 = new TestNodeServiceFactory(log, TimeSpan.Zero, true);

            services.AddFactory(service1);
            services.AddFactory(service2);

            CancellationTokenSource cts = new CancellationTokenSource();

            services.CreateServices(null, cts.Token);

            services.Start();

            //Sleep to avoid arbitrary order of start and stop events.
            Thread.Sleep(50);

            cts.Cancel();

            Assert.True(services.Join(TimeSpan.FromMilliseconds(100)));

            services.DisposeServices();

            Assert.That(log.GetLog(), Is.EqualTo(new string[]
            {
                "Service created.",
                "Service created.",
                "Service started.",
                "Service started.",
                "Service stopped.",
                "Service stopped.",
                "Service disposal failed.",
                "Service disposal failed."
            }));
        }