예제 #1
0
        public static void ShutdownRequest_AfterShuttingDown()
        {
            // can not enqueue after shutting down event was enqueued
            var queue = new ManualEventQueue();

            Assert.True(queue.BeginShutdown());
            Assert.False(queue.Enqueue(new ShutdownRequestEvent()));

            // handling is skipped, if a shutting down event is enqueued beforehand
            queue = new ManualEventQueue();

            bool requestDetected = false;

            queue.Subscribers.Add(
                DelegateEventHandler.OnShutdownRequest(
                    e => requestDetected = true),
                weakRef: false);

            Assert.True(queue.Enqueue(new ShutdownRequestEvent())); // enqueueing succeeds
            Assert.True(queue.Enqueue(new ShuttingDownEvent()));    // shutting down event added
            Assert.False(requestDetected);
            Assert.True(queue.HandleNext());                        // the request was "handled" successfully
            Assert.False(requestDetected);                          // but the handler was not actually invoked

            // ensure there are no unexpected events in the queue, that could have shifted the position of the request event
            Assert.True(queue.HandleNext()); // shutting down event
            Assert.True(queue.HandleNext()); // shut down event
            Assert.False(queue.HandleNext());
            Assert.True(queue.IsShutDown);
        }
예제 #2
0
        public static void IsShutDown()
        {
            // initially open
            var queue = new ManualEventQueue();

            Assert.False(queue.IsShutDown);

            // removing all subscribers does not shut down the queue
            queue.Subscribers.AddAll(new TestEventHandler(), weakRef: false);
            queue.Subscribers.Clear();
            Assert.False(queue.IsShutDown);

            // adding the shutting down event is not enough
            queue.BeginShutdown();
            Assert.False(queue.IsShutDown);

            // handling the shutting down event is not enough...
            queue.Subscribers.Add(
                DelegateEventHandler.OnShuttingDown(
                    e => queue.Enqueue(new NamedEvent("shutting down event handler"))),
                weakRef: false);
            Assert.True(queue.HandleNext());
            Assert.False(queue.IsShutDown);

            // handling the NamedEvent added in the ShuttingDownEvent handler, is not enough
            Assert.True(queue.HandleNext());
            Assert.False(queue.IsShutDown);

            // ... but handling the ShutDownEvent is
            Assert.True(queue.HandleNext());
            Assert.True(queue.IsShutDown);
            Assert.False(queue.HandleNext());
        }
예제 #3
0
        private static void Constructor(out ManualEventQueue queue)
        {
            // try to pass a null event queue
            Assert.Throws <ArgumentNullException>(() => TestApp.Initialize(null));

            // try to pass an event queue that's already shut down
            var manualQueue = new ManualEventQueue();

            manualQueue.BeginShutdown();
            while (manualQueue.HandleNext())
            {
                ;
            }
            Assert.Throws <ArgumentException>(() => TestApp.Initialize(manualQueue));

            // pass a proper event queue
            manualQueue = new ManualEventQueue();
            TestApp.Initialize(manualQueue);

            Assert.NotNull(AppBase.MainEventQueue);
            Assert.AreSame(manualQueue, AppBase.MainEventQueue.BaseEventQueue);
            Assert.AreSame(manualQueue.Subscribers, AppBase.MainEventQueue.Subscribers);
            Assert.AreSame(manualQueue.EventAdding, AppBase.MainEventQueue.EventAdding);
            Assert.AreSame(manualQueue.EventHandling, AppBase.MainEventQueue.EventHandling);
            Assert.AreSame(manualQueue.RaiseUnhandledEvents, AppBase.MainEventQueue.RaiseUnhandledEvents);
            Assert.AreEqual(AppState.Shutdown, AppBase.CurrentState);

            queue = manualQueue;
        }
예제 #4
0
        public static void NoNewSubscriptionsAfterShutdown()
        {
            var queue   = new ManualEventQueue();
            var handler = new TestEventHandler();

            queue.Subscribers.AddAll(handler);

            queue.BeginShutdown();
            Assert.True(queue.HandleNext());                    // shutting down event
            Assert.True(queue.HandleNext());                    // shut down event
            Assert.True(queue.IsShutDown);
            Assert.False(queue.Subscribers.RemoveAll(handler)); // a shutdown queue has no subscribers
            Assert.False(queue.Subscribers.AddAll(new TestEventHandler(), weakRef: false));
        }
예제 #5
0
        private static void ShutdownEventTriggersShutdownState(ManualEventQueue manualQueue)
        {
            TestApp.MoveToState(AppState.Running);
            while (manualQueue.HandleNext())
            {
                ;
            }

            manualQueue.BeginShutdown();
            while (manualQueue.HandleNext())
            {
                ;
            }
            Assert.True(manualQueue.IsShutDown);
            Assert.AreEqual(AppState.Shutdown, TestApp.CurrentState);
        }
예제 #6
0
        public static void EventsCanNotBeEnqueuedAfterShuttingDown()
        {
            var queue        = new ManualEventQueue();
            var testListener = new TestEventHandler();

            queue.Subscribers.AddAll(testListener);
            queue.Subscribers.Add(
                DelegateEventHandler.OnShuttingDown(
                    e => queue.Enqueue(new NamedEvent("shutting down event handler"))),
                weakRef: false);

            // enqueue shutting down event
            queue.BeginShutdown();

            // can enqueue, after the shutting down event is enqueued, but before it is handled
            Assert.True(queue.Enqueue(new TestEvent()
            {
                Value = 1
            }));

            // handle shutting down event
            Assert.True(queue.HandleNext());
            Assert.Null(testListener.LastEventHandled);

            // try to enqueue another event, after the shutting down event is handled:
            // it should have no effect
            Assert.False(queue.Enqueue(new TestEvent()
            {
                Value = 2
            }));

            // handle first test event
            Assert.True(queue.HandleNext());
            Assert.AreEqual(1, ((TestEvent)testListener.LastEventHandled).Value);

            // handle the event added in the shutting down event handler
            Assert.True(queue.HandleNext());
            Assert.IsInstanceOf <NamedEvent>(testListener.LastEventHandled); // false for null

            // handle shut down event
            Assert.True(queue.HandleNext(out var shutDownEvent));
            Assert.IsInstanceOf <ShutDownEvent>(shutDownEvent);

            // nothing more to handle
            Assert.True(queue.IsShutDown);
        }
예제 #7
0
        public static void ShuttingDownHandler()
        {
            var queue = new ManualEventQueue();

            // create subscriber
            ShuttingDownEvent lastEventHandled = null;
            var subscriber = DelegateEventHandler.OnShuttingDown(evnt => lastEventHandled = evnt);

            queue.Subscribers.Add(subscriber);

            // enqueue and handle event
            queue.BeginShutdown();
            Assert.Null(lastEventHandled);
            Assert.True(queue.HandleNext()); // shutting down event

            // test subscriber
            Assert.NotNull(lastEventHandled);
            Assert.True(queue.HandleNext()); // shut down event
            Assert.True(queue.IsShutDown);
        }