예제 #1
0
        public virtual void TestStopFailingInitAndStop()
        {
            BreakableService svc = new BreakableService(true, false, true);

            svc.RegisterServiceListener(new LoggingStateChangeListener());
            try
            {
                svc.Init(new Configuration());
                NUnit.Framework.Assert.Fail("Expected a failure, got " + svc);
            }
            catch (BreakableService.BrokenLifecycleEvent e)
            {
                Assert.Equal(Service.STATE.Inited, e.state);
            }
            //the service state is stopped
            AssertServiceStateStopped(svc);
            Assert.Equal(Service.STATE.Inited, svc.GetFailureState());
            Exception failureCause = svc.GetFailureCause();

            NUnit.Framework.Assert.IsNotNull("Null failure cause in " + svc, failureCause);
            BreakableService.BrokenLifecycleEvent cause = (BreakableService.BrokenLifecycleEvent
                                                           )failureCause;
            NUnit.Framework.Assert.IsNotNull("null state in " + cause + " raised by " + svc,
                                             cause.state);
            Assert.Equal(Service.STATE.Inited, cause.state);
        }
예제 #2
0
        public virtual void TestStopUnstarted()
        {
            BreakableService svc = new BreakableService();

            svc.Stop();
            AssertServiceStateStopped(svc);
            AssertStateCount(svc, Service.STATE.Inited, 0);
            AssertStateCount(svc, Service.STATE.Stopped, 1);
        }
예제 #3
0
        public virtual void TestStartTwice()
        {
            BreakableService svc = new BreakableService();

            svc.Init(new Configuration());
            svc.Start();
            svc.Start();
            AssertStateCount(svc, Service.STATE.Started, 1);
        }
        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);
        }
예제 #5
0
        public virtual void TestAddServiceInInit()
        {
            BreakableService child = new BreakableService();

            AssertInState(Service.STATE.Notinited, child);
            TestCompositeService.CompositeServiceAddingAChild composite = new TestCompositeService.CompositeServiceAddingAChild
                                                                              (child);
            composite.Init(new Configuration());
            AssertInState(Service.STATE.Inited, child);
        }
예제 #6
0
        public virtual void TestInitTwice()
        {
            BreakableService svc  = new BreakableService();
            Configuration    conf = new Configuration();

            conf.Set("test.init", "t");
            svc.Init(conf);
            svc.Init(new Configuration());
            AssertStateCount(svc, Service.STATE.Inited, 1);
            AssertServiceConfigurationContains(svc, "test.init");
        }
예제 #7
0
        /// <summary>
        /// Assert that the breakable service has entered a state exactly the number
        /// of time asserted.
        /// </summary>
        /// <param name="service">service -if null an assertion is raised.</param>
        /// <param name="state">state to check.</param>
        /// <param name="expected">expected count.</param>
        public static void AssertStateCount(BreakableService service, Service.STATE state
                                            , int expected)
        {
            NUnit.Framework.Assert.IsNotNull("Null service", service);
            int actual = service.GetCount(state);

            if (expected != actual)
            {
                NUnit.Framework.Assert.Fail("Expected entry count for state [" + state + "] of "
                                            + service + " to be " + expected + " but was " + actual);
            }
        }
예제 #8
0
        public virtual void TestInitNullConf()
        {
            BreakableService svc = new BreakableService(false, false, false);

            try
            {
                svc.Init(null);
                Log.Warn("Null Configurations are permitted ");
            }
            catch (ServiceStateException)
            {
            }
        }
예제 #9
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAddUninitedSiblingInStart()
        {
            CompositeService parent  = new CompositeService("parent");
            BreakableService sibling = new BreakableService();

            parent.AddService(new TestCompositeService.AddSiblingService(parent, sibling, Service.STATE
                                                                         .Started));
            parent.Init(new Configuration());
            AssertInState(Service.STATE.Notinited, sibling);
            parent.Start();
            parent.Stop();
            Assert.Equal("Incorrect number of services", 2, parent.GetServices
                             ().Count);
        }
예제 #10
0
        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();
        }
예제 #11
0
        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);
        }
예제 #13
0
        public virtual void TestStopFailedStart()
        {
            BreakableService svc = new BreakableService(false, true, false);

            svc.Init(new Configuration());
            AssertServiceStateInited(svc);
            try
            {
                svc.Start();
                NUnit.Framework.Assert.Fail("Expected a failure, got " + svc);
            }
            catch (BreakableService.BrokenLifecycleEvent)
            {
            }
            //expected
            //the service state wasn't passed
            AssertServiceStateStopped(svc);
        }
예제 #14
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAddStartedChildBeforeInit()
        {
            CompositeService parent = new CompositeService("parent");
            BreakableService child  = new BreakableService();

            child.Init(new Configuration());
            child.Start();
            TestCompositeService.AddSiblingService.AddChildToService(parent, child);
            try
            {
                parent.Init(new Configuration());
                NUnit.Framework.Assert.Fail("Expected an exception, got " + parent);
            }
            catch (ServiceStateException)
            {
            }
            //expected
            parent.Stop();
        }
예제 #15
0
        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);
        }
예제 #16
0
        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();
        }
예제 #17
0
        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 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();
        }
예제 #19
0
        /// <exception cref="System.Exception"/>
        public virtual void TestAddUninitedSiblingInInit()
        {
            CompositeService parent  = new CompositeService("parent");
            BreakableService sibling = new BreakableService();

            parent.AddService(new TestCompositeService.AddSiblingService(parent, sibling, Service.STATE
                                                                         .Inited));
            parent.Init(new Configuration());
            try
            {
                parent.Start();
                NUnit.Framework.Assert.Fail("Expected an exception, got " + parent);
            }
            catch (ServiceStateException)
            {
            }
            //expected
            parent.Stop();
            Assert.Equal("Incorrect number of services", 2, parent.GetServices
                             ().Count);
        }
예제 #20
0
        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);
        }