Esempio n. 1
0
        public static void HandleSupportsEventInheritance()
        {
            var queue      = new ManualEventQueue();
            var subscriber = new AncestorEventHandler();

            queue.Subscribers.AddAll(subscriber);

            // subscriber does not handle events not in it's inheritance tree
            Assert.True(queue.Enqueue(new TestEvent()));
            Assert.True(queue.HandleNext());
            Assert.Null(subscriber.LastEventHandled);

            // subscriber accepts event type it explicitly stated it would handle
            var namedEvent = new NamedEvent("name");

            Assert.True(queue.Enqueue(namedEvent));
            Assert.True(queue.HandleNext());
            Assert.AreSame(namedEvent, subscriber.LastEventHandled);

            // subscriber accepts event that inherit what it declared it would handle
            var derivedEvent = new DescendantEvent();

            Assert.True(queue.Enqueue(derivedEvent));
            Assert.True(queue.HandleNext());
            Assert.AreSame(derivedEvent, subscriber.LastEventHandled);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        public static void EnqueueReturnValue()
        {
            var queue = new ManualEventQueue();
            var evnt  = new TestEvent();

            Assert.True(queue.Enqueue(evnt));

            // adding the same event a second time
            Assert.False(queue.Enqueue(evnt));

            // handling it and adding it again is fine however
            Assert.True(queue.HandleNext());
            Assert.False(queue.HandleNext()); // verify that we only added it once
            Assert.True(queue.Enqueue(evnt));

            // adding suspension
            queue.EventAdding.Suspend();
            Assert.False(queue.Enqueue(new TestEvent()));
            queue.EventAdding.Resume();
            Assert.True(queue.Enqueue(new TestEvent()));

            // handle all events
            while (queue.HandleNext())
            {
                ;
            }

            // add and handle shutting down event
            Assert.True(queue.Enqueue(new ShuttingDownEvent()));
            Assert.False(queue.Enqueue(new ShuttingDownEvent())); // another shutting down event is already enqueued
            Assert.True(queue.HandleNext());
            Assert.False(queue.Enqueue(new TestEvent()));         // no more events of any kind can be added
        }
Esempio n. 4
0
        public static void Callback()
        {
            var  queue   = new ManualEventQueue();
            var  evnt    = new TestEvent();
            bool handled = false;

            queue.Enqueue(
                evnt,
                onHandled: e =>
            {
                Assert.True(ReferenceEquals(e, evnt));
                handled = true;
            });

            // handled original event
            Assert.False(handled);
            Assert.True(queue.HandleNext());
            Assert.False(handled);

            // handle second event, implicitly added
            Assert.True(queue.HandleNext());
            Assert.True(handled);

            // no other events were added
            Assert.False(queue.HandleNext());
        }
Esempio n. 5
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());
        }
Esempio n. 6
0
        public static void StatelessHandler()
        {
            var queue = new ManualEventQueue();

            // create subscriber
            TestEvent lastEventHandled = null;
            var       subscriber       = DelegateEventHandler.On <TestEvent>(evnt => lastEventHandled = evnt);

            queue.Subscribers.Add(subscriber);

            // enqueue and handle event
            queue.Enqueue(new TestEvent()
            {
                Value = 3
            });
            Assert.Null(lastEventHandled);
            Assert.True(queue.HandleNext());

            // test subscriber
            Assert.NotNull(lastEventHandled);
            Assert.AreEqual(3, lastEventHandled.Value);

            // queue is empty again (no unhandled exceptions or anything else)
            Assert.False(queue.HandleNext());
        }
Esempio n. 7
0
        public static void NullCallback()
        {
            var queue = new ManualEventQueue();

            Assert.True(queue.Enqueue(new TestEvent(), onHandled: null)); // no exception, successful call
            Assert.True(queue.HandleNext());
            Assert.False(queue.HandleNext());                             // no secondary event added
        }
Esempio n. 8
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);
        }
Esempio n. 9
0
        public static void EventsCanNotBeEnqueuedTwice()
        {
            var queue = new ManualEventQueue();
            var evnt  = new TestEvent();

            // the same instance twice won't work
            queue.Enqueue(evnt);
            queue.Enqueue(evnt);
            Assert.True(queue.HandleNext());
            Assert.False(queue.HandleNext());

            // .. but two separate instances of the same event are fine
            queue.Enqueue(new TestEvent());
            queue.Enqueue(new TestEvent());
            Assert.True(queue.HandleNext());
            Assert.True(queue.HandleNext());
        }
Esempio n. 10
0
        public static void EnqueueUpdatesMetadata()
        {
            var queue = new ManualEventQueue();
            var evnt  = new NamedEvent("test");

            Assert.Null(evnt.EventEnqueuePos);
            Assert.True(queue.Enqueue(evnt));
            Assert.True(evnt.EventEnqueuePos.Contains(nameof(EnqueueUpdatesMetadata)));
        }
Esempio n. 11
0
        public static void StatefulHandler()
        {
            var queue = new ManualEventQueue();

            // create subscriber
            TestEvent lastEventHandled = null;
            var       subscriber       = DelegateEventHandler.On(
                (int currentState, TestEvent evnt) =>
            {
                lastEventHandled = evnt;
                evnt.Value      += currentState;
                return(currentState + 1);
            },
                initialState: 10);

            queue.Subscribers.Add(subscriber);

            // enqueue events
            queue.Enqueue(new TestEvent()
            {
                Value = 3
            });
            queue.Enqueue(new TestEvent()
            {
                Value = 4
            });
            Assert.Null(lastEventHandled);

            // handle first event
            Assert.True(queue.HandleNext());
            Assert.NotNull(lastEventHandled);
            Assert.AreEqual(13, lastEventHandled.Value);

            // handle second event
            Assert.True(queue.HandleNext());
            Assert.NotNull(lastEventHandled);
            Assert.AreEqual(15, lastEventHandled.Value);

            // queue is empty again (no unhandled exceptions or anything else)
            Assert.False(queue.HandleNext());
        }
Esempio n. 12
0
        public static void EventAddingSuspension()
        {
            var queue = new ManualEventQueue();

            Assert.False(queue.EventHandling.IsSuspended);

            var testListener = new TestEventHandler();

            queue.Subscribers.AddAll(testListener);
            var evnt = new TestEvent();

            queue.EventAdding.Suspend();
            Assert.False(queue.Enqueue(evnt));
            Assert.False(queue.HandleNext());
            Assert.Null(testListener.LastEventHandled);

            queue.EventAdding.Resume();
            Assert.True(queue.Enqueue(evnt));
            Assert.True(queue.HandleNext());
            Assert.AreSame(evnt, testListener.LastEventHandled);
        }
Esempio n. 13
0
        public static void HandleNextInvokesHandler()
        {
            var queue      = new ManualEventQueue();
            var evnt       = new TestEvent();
            var subscriber = new TestEventHandler();

            // queue empty
            Assert.False(queue.HandleNext());

            // enqueueing does not invoke handlers
            queue.Subscribers.AddAll(subscriber);
            Assert.True(queue.Enqueue(evnt));
            Assert.Null(subscriber.LastEventHandled);

            // invoking handlers is successful
            Assert.True(queue.HandleNext());
            Assert.AreSame(evnt, subscriber.LastEventHandled);

            // queue is empty again
            Assert.False(queue.HandleNext());
        }
Esempio n. 14
0
        public static void ExceptionHandler()
        {
            var queue = new ManualEventQueue();

            // create subscriber
            UnhandledExceptionEvent lastEventHandled = null;
            var subscriber = DelegateEventHandler.OnException(evnt => lastEventHandled = evnt);

            queue.Subscribers.Add(subscriber);

            // enqueue and handle event
            queue.Enqueue(new UnhandledExceptionEvent(new UnauthorizedAccessException()));
            Assert.Null(lastEventHandled);
            Assert.True(queue.HandleNext());

            // test subscriber
            Assert.NotNull(lastEventHandled);
            Test.OrdinalEquals(lastEventHandled.Type, typeof(UnauthorizedAccessException).ToString());

            // queue is empty again (no unhandled exceptions or anything else)
            Assert.False(queue.HandleNext());
        }
Esempio n. 15
0
        public static void EventHandlerExceptions()
        {
            var queue = new ManualEventQueue();

            queue.Subscribers.Add(
                DelegateEventHandler.On <TestEvent>(
                    e => throw new ArgumentOutOfRangeException()),
                weakRef: false);

            queue.Subscribers.Add(
                DelegateEventHandler.On <TestEvent>(
                    e => throw new MissingMemberException()),
                weakRef: false);

            bool argumentExceptionFound         = false;
            bool memberExceptionFound           = false;
            bool unhandledExceptionEventHandled = false;

            queue.Subscribers.Add(
                DelegateEventHandler.On <UnhandledExceptionEvent>(
                    e =>
            {
                unhandledExceptionEventHandled = true;

                if (e.Type.IndexOf(nameof(ArgumentOutOfRangeException), StringComparison.Ordinal) != -1)
                {
                    argumentExceptionFound = true;
                }
                else if (e.Type.IndexOf(nameof(MissingMemberException), StringComparison.Ordinal) != -1)
                {
                    memberExceptionFound = true;
                }
            }),
                weakRef: false);

            // unhandled exception events are generated by default
            queue.Enqueue(new TestEvent());
            Assert.False(argumentExceptionFound);
            Assert.False(memberExceptionFound);
            Assert.False(unhandledExceptionEventHandled);
            Assert.True(queue.HandleNext()); // test event
            Assert.True(queue.HandleNext()); // argument exception
            Assert.True(queue.HandleNext()); // missing member exception
            Assert.True(argumentExceptionFound);
            Assert.True(memberExceptionFound);
            Assert.True(unhandledExceptionEventHandled);

            // unhandled exception event suppression
            queue.Enqueue(new TestEvent());
            argumentExceptionFound         = false;
            memberExceptionFound           = false;
            unhandledExceptionEventHandled = false;
            queue.RaiseUnhandledEvents.Suspend();
            Assert.True(queue.HandleNext());
            Assert.False(queue.HandleNext());
            queue.RaiseUnhandledEvents.Resume();
            Assert.False(argumentExceptionFound);
            Assert.False(memberExceptionFound);
            Assert.False(unhandledExceptionEventHandled);

            // event addition suppression
            queue.Enqueue(new TestEvent());
            queue.EventAdding.Suspend();
            Assert.True(queue.HandleNext());
            Assert.False(queue.HandleNext());
            queue.EventAdding.Resume();
            Assert.False(argumentExceptionFound);
            Assert.False(memberExceptionFound);
            Assert.False(unhandledExceptionEventHandled);
        }