Пример #1
0
        public async Task CatchAnExceptionViaMiddlware()
        {
            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            bool caughtException       = false;

            m.OnReceive(async(context, next) =>
            {
                try
                {
                    await next();
                    Assert.Fail("Should not get here");
                }
                catch (Exception ex)
                {
                    Assert.IsTrue(ex.Message == "test");
                    caughtException = true;
                }
            });

            m.OnReceive(async(context, next) =>
            {
                throw new Exception("test");
            });

            await m.ReceiveActivity(null);

            Assert.IsTrue(caughtException);
        }
Пример #2
0
        public async Task RunCodeBeforeAndAfter()
        {
            bool didRun1       = false;
            bool codeafter2run = false;
            bool didRun2       = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();

            m.OnReceive(async(context, next) =>
            {
                Assert.IsFalse(didRun1, "Looks like the 1st middleware has already run");
                didRun1 = true;
                await next();
                Assert.IsTrue(didRun1, "The 2nd middleware should have run now.");
                codeafter2run = true;
            });

            m.OnReceive(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run");
                Assert.IsFalse(codeafter2run, "The code that runs after middleware 2 is complete has already run.");
                didRun2 = true;
                await next();
            });

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
            Assert.IsTrue(codeafter2run);
        }
Пример #3
0
        public async Task MixedMiddlewareInOrderAnonymousLast()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();

            m.Use(
                new CallMeMiddlware(() =>
            {
                Assert.IsFalse(didRun2, "Second Middleware was called");
                didRun1 = true;
            }));

            m.OnReceive(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1st middleware has not been run");
                didRun2 = true;
                await next();
            });

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
Пример #4
0
        public async Task BubbleUncaughtException()
        {
            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.OnReceive(async(context, next) =>
            {
                throw new InvalidOperationException("test");
            });

            await m.ReceiveActivity(null);

            Assert.Fail("Should never have gotten here");
        }
Пример #5
0
        public async Task TwoAnonymousMiddleware()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.OnReceive(async(context, next) =>
            {
                didRun1 = true;
                await next();
            });
            m.OnReceive(async(context, next) =>
            {
                didRun2 = true;
                await next();
            });

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }
Пример #6
0
        public async Task TwoAnonymousMiddlewareInOrder()
        {
            bool didRun1 = false;
            bool didRun2 = false;

            Middleware.MiddlewareSet m = new Middleware.MiddlewareSet();
            m.OnReceive(async(context, next) =>
            {
                Assert.IsFalse(didRun2, "Looks like the 2nd one has already run");
                didRun1 = true;
                await next();
            });
            m.OnReceive(async(context, next) =>
            {
                Assert.IsTrue(didRun1, "Looks like the 1nd one has not yet run");
                didRun2 = true;
                await next();
            });

            await m.ReceiveActivity(null);

            Assert.IsTrue(didRun1);
            Assert.IsTrue(didRun2);
        }