public virtual void TestStopTwice()
        {
            BreakableService svc = new BreakableService();

            svc.Init(new Configuration());
            svc.Start();
            svc.Stop();
            AssertStateCount(svc, Service.STATE.Stopped, 1);
            svc.Stop();
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }
        public virtual void TestServiceNotificationsStopOnceUnregistered()
        {
            BreakableService             svc      = new BreakableService(false, false, false);
            BreakableStateChangeListener listener = new BreakableStateChangeListener();

            svc.RegisterServiceListener(listener);
            svc.Init(new Configuration());
            AssertEventCount(listener, 1);
            svc.UnregisterServiceListener(listener);
            svc.Start();
            AssertEventCount(listener, 1);
            svc.Stop();
            AssertEventCount(listener, 1);
            svc.Stop();
        }
        public virtual void TestListenerChain()
        {
            //create and register the listeners
            LoggingStateChangeListener logListener = new LoggingStateChangeListener();

            Register(logListener);
            BreakableStateChangeListener l0 = new BreakableStateChangeListener("l0");

            Register(l0);
            listener.SetFailingState(Service.STATE.Started);
            Register();
            BreakableStateChangeListener l3 = new BreakableStateChangeListener("l3");

            Register(l3);
            //create and init a service.
            BreakableService service = new BreakableService();

            service.Init(new Configuration());
            AssertServiceStateInited(service);
            AssertListenerState(l0, Service.STATE.Inited);
            AssertListenerState(listener, Service.STATE.Inited);
            AssertListenerState(l3, Service.STATE.Inited);
            service.Start();
            //expect that listener l1 and the failing listener are in start, but
            //not the final one
            AssertServiceStateStarted(service);
            AssertListenerState(l0, Service.STATE.Started);
            AssertListenerEventCount(l0, 2);
            AssertListenerState(listener, Service.STATE.Started);
            AssertListenerEventCount(listener, 2);
            //this is the listener that is not expected to have been invoked
            AssertListenerState(l3, Service.STATE.Inited);
            AssertListenerEventCount(l3, 1);
            //stop the service
            service.Stop();
            //listeners are all updated
            AssertListenerEventCount(l0, 3);
            AssertListenerEventCount(listener, 3);
            AssertListenerEventCount(l3, 2);
            //can all be unregistered in any order
            Unregister(logListener);
            Unregister(l0);
            Unregister(l3);
            //check that the listeners are all unregistered, even
            //though they were registered in a different order.
            //rather than do this by doing unregister checks, a new service is created
            service = new BreakableService();
            //this service is initialized
            service.Init(new Configuration());
            //it is asserted that the event count has not changed for the unregistered
            //listeners
            AssertListenerEventCount(l0, 3);
            AssertListenerEventCount(l3, 2);
            //except for the one listener that was not unregistered, which
            //has incremented by one
            AssertListenerEventCount(listener, 4);
        }
        public virtual void TestStopUnstarted()
        {
            BreakableService svc = new BreakableService();

            svc.Stop();
            AssertServiceStateStopped(svc);
            AssertStateCount(svc, Service.STATE.Inited, 0);
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }
        public virtual void TestServiceFailingNotifications()
        {
            BreakableService             svc      = new BreakableService(false, false, false);
            BreakableStateChangeListener listener = new BreakableStateChangeListener();

            listener.SetFailingState(Service.STATE.Started);
            svc.RegisterServiceListener(listener);
            svc.Init(new Configuration());
            AssertEventCount(listener, 1);
            //start this; the listener failed but this won't show
            svc.Start();
            //counter went up
            AssertEventCount(listener, 2);
            Assert.Equal(1, listener.GetFailureCount());
            //stop the service -this doesn't fail
            svc.Stop();
            AssertEventCount(listener, 3);
            Assert.Equal(1, listener.GetFailureCount());
            svc.Stop();
        }
        public virtual void TestServiceNotificationsUnregisterDuringCallback()
        {
            BreakableService             svc      = new BreakableService(false, false, false);
            BreakableStateChangeListener listener = new TestServiceLifecycle.SelfUnregisteringBreakableStateChangeListener
                                                        ();
            BreakableStateChangeListener l2 = new BreakableStateChangeListener();

            svc.RegisterServiceListener(listener);
            svc.RegisterServiceListener(l2);
            svc.Init(new Configuration());
            AssertEventCount(listener, 1);
            AssertEventCount(l2, 1);
            svc.UnregisterServiceListener(listener);
            svc.Start();
            AssertEventCount(listener, 1);
            AssertEventCount(l2, 2);
            svc.Stop();
            AssertEventCount(listener, 1);
            svc.Stop();
        }
        public virtual void TestFailingStop()
        {
            BreakableService svc = new BreakableService(false, false, true);

            svc.Init(new Configuration());
            svc.Start();
            try
            {
                svc.Stop();
                NUnit.Framework.Assert.Fail("Expected a failure, got " + svc);
            }
            catch (BreakableService.BrokenLifecycleEvent)
            {
            }
            //expected
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }
        public virtual void TestEventHistory()
        {
            Register();
            BreakableService service = new BreakableService();

            AssertListenerState(listener, Service.STATE.Notinited);
            Assert.Equal(0, listener.GetEventCount());
            service.Init(new Configuration());
            AssertListenerState(listener, Service.STATE.Inited);
            NUnit.Framework.Assert.AreSame(service, listener.GetLastService());
            AssertListenerEventCount(listener, 1);
            service.Start();
            AssertListenerState(listener, Service.STATE.Started);
            AssertListenerEventCount(listener, 2);
            service.Stop();
            AssertListenerState(listener, Service.STATE.Stopped);
            AssertListenerEventCount(listener, 3);
        }
        public virtual void TestWalkthrough()
        {
            BreakableService svc = new BreakableService();

            AssertServiceStateCreated(svc);
            AssertStateCount(svc, Service.STATE.Notinited, 1);
            AssertStateCount(svc, Service.STATE.Inited, 0);
            AssertStateCount(svc, Service.STATE.Started, 0);
            AssertStateCount(svc, Service.STATE.Stopped, 0);
            svc.Init(new Configuration());
            AssertServiceStateInited(svc);
            AssertStateCount(svc, Service.STATE.Inited, 1);
            svc.Start();
            AssertServiceStateStarted(svc);
            AssertStateCount(svc, Service.STATE.Started, 1);
            svc.Stop();
            AssertServiceStateStopped(svc);
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }
        /// <exception cref="System.Exception"/>
        public virtual void TestAddStoppedChildBeforeInit()
        {
            CompositeService parent = new CompositeService("parent");
            BreakableService child  = new BreakableService();

            child.Init(new Configuration());
            child.Start();
            child.Stop();
            TestCompositeService.AddSiblingService.AddChildToService(parent, child);
            try
            {
                parent.Init(new Configuration());
                NUnit.Framework.Assert.Fail("Expected an exception, got " + parent);
            }
            catch (ServiceStateException)
            {
            }
            //expected
            parent.Stop();
        }
        public virtual void TestListenerFailure()
        {
            listener.SetFailingState(Service.STATE.Inited);
            Register();
            BreakableStateChangeListener l2 = new BreakableStateChangeListener();

            Register(l2);
            BreakableService service = new BreakableService();

            service.Init(new Configuration());
            //expected notifications to fail
            //still should record its invocation
            AssertListenerState(listener, Service.STATE.Inited);
            AssertListenerEventCount(listener, 1);
            //and second listener didn't get notified of anything
            AssertListenerEventCount(l2, 0);
            //service should still consider itself started
            AssertServiceStateInited(service);
            service.Start();
            service.Stop();
        }
        public virtual void TestStopFailedInit()
        {
            BreakableService svc = new BreakableService(true, false, false);

            AssertServiceStateCreated(svc);
            try
            {
                svc.Init(new Configuration());
                NUnit.Framework.Assert.Fail("Expected a failure, got " + svc);
            }
            catch (BreakableService.BrokenLifecycleEvent)
            {
            }
            //expected
            //the service state wasn't passed
            AssertServiceStateStopped(svc);
            AssertStateCount(svc, Service.STATE.Inited, 1);
            AssertStateCount(svc, Service.STATE.Stopped, 1);
            //now try to stop
            svc.Stop();
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }