Ejemplo n.º 1
0
        public void WithEventEmitter_WhenExceptionRaised_ExceptionIsCaughtAndLogged()
        {
            const string exceptionMessage = "Listener1 exception";
            bool         handled1         = false;
            bool         handled2         = false;

            // test logger, logs to memory and set the MessageSeen property if
            // the message passed to the constructor is logged;
            var logger = new TestLogger(messageToTest: exceptionMessage);
            var em     = new TestEventEmitter(logger);

            List <int> callOrder = new List <int>();

            void Listener1(TestEventEmitterArgs args)
            {
                handled1 = true;
                callOrder.Add(1);
                throw new Exception(exceptionMessage);
            }

            void Listener2(TestEventEmitterArgs args)
            {
                handled2 = true;
                callOrder.Add(2);
            }

            em.On(1, Listener1);
            em.On(1, Listener2);
            em.DoDummyEmit(1, string.Empty);
            handled1.Should().BeTrue();
            handled2.Should().BeTrue();
            logger.MessageSeen.Should().BeTrue();
            callOrder.Count.Should().Be(2);
            callOrder[0].Should().Be(1);
            callOrder[1].Should().Be(2);

            handled1 = handled2 = false;
            logger.Reset();
            callOrder = new List <int>();

            em.Once(1, Listener1);
            em.Once(1, Listener2);
            em.DoDummyEmit(1, string.Empty);
            handled1.Should().BeTrue();
            handled2.Should().BeTrue();
            callOrder.Count.Should().Be(4);

            // On is handled
            callOrder[0].Should().Be(1);
            callOrder[1].Should().Be(2);

            // then Once
            callOrder[2].Should().Be(1);
            callOrder[3].Should().Be(2);
            logger.MessageSeen.Should().BeTrue();
        }
Ejemplo n.º 2
0
        public void WithEventEmitter_WhenOnce_ListenerRegistersForOneEvent()
        {
            var    em      = new TestEventEmitter(DefaultLogger.LoggerInstance);
            bool   t       = false;
            bool   tt      = false;
            string message = string.Empty;
            int    counter = 0;

            void Reset()
            {
                t  = false;
                tt = false;
            }

            void Handler1(TestEventEmitterArgs args)
            {
                counter++;
                message = args.Message;
                t       = true;
            }

            void Handler2(TestEventEmitterArgs args)
            {
                counter++;
                message = args.Message;
                tt      = true;
            }

            // no event/state argument, catch all
            em.Once(Handler1);
            em.Once(Handler1);
            em.DoDummyEmit(1, "once");
            t.Should().BeTrue();
            message.Should().Be("once");
            counter.Should().Be(2);
            Reset();

            // only catch 1 events
            em.Once(1, Handler2);
            em.DoDummyEmit(2, "on");

            // no events should be handled, t & tt should be false here
            t.Should().BeFalse();
            tt.Should().BeFalse();

            // there are 2 listeners and the first is catch all
            // but the first should have already handled an event and de-registered
            // so the count remains the same
            counter.Should().Be(2);
            Reset();
            em.DoDummyEmit(1, "on");

            // t should not have been set, tt should complete for the first time
            t.Should().BeFalse();
            tt.Should().BeTrue();

            // still 2 listeners and we sent a 1 event which second should handle
            counter.Should().Be(3);
            Reset();
            em.DoDummyEmit(1, "on");

            // t & tt should both be false
            t.Should().BeFalse();
            tt.Should().BeFalse();

            // handlers should be de-registered so the count should remain at 2
            counter.Should().Be(3);
        }