Example #1
0
        public async Task TestToAsyncContext()
        {
            await AsTask.ToContext(_contextTests1Id);

            Console.WriteLine(AsTask.WhereAmI());
            Assert.True(AsTask.IsThreadContext(_contextTests1Id), $"This is not the AsContext({_contextTests1Id})!");
        }
Example #2
0
        public void TestExceptions3()
        {
            TaskExceptionHandler.SetExceptionHandler(task => Console.WriteLine($"[UnhandledException] {task.Exception?.GetBaseException().Message}"));

            for (var i = 0; i < 5; i++)
            {
                Exceptions(i);
            }

            void Exceptions(int idx)
            {
                switch (idx)
                {
                case 0:
                    AsTask.ToMainContext(() => throw new ApplicationException("ToMainContext"));
                    break;

                case 1:
                    AsTask.ToBackgroundContext(() => throw new ApplicationException("ToBackgroundContext"));
                    break;

                case 2:
                    AsTask.ToContext(_contextTests2Id, () => throw new ApplicationException("ToContext"));
                    break;

                case 3:
                    AsTask.ToStaticThreadPool(() => throw new ApplicationException("ToStaticThreadPool"));
                    break;

                case 4:
                    AsTask.ToDynamicThreadPool(() => throw new ApplicationException("ToDynamicThreadPool"));
                    break;
                }
            }
        }
Example #3
0
        public async Task TestExceptions2()
        {
            for (var i = 0; i < 5; i++)
            {
                try
                {
                    await Exceptions(i);
                }
                catch (Exception)
                {
                    Console.WriteLine($"Catch exception[{i}]");
                }
            }

            async Task Exceptions(int idx)
            {
                switch (idx)
                {
                case 0:
                    await AsTask.ToMainContext(() => throw new ApplicationException("ToMainContext"));

                    break;

                case 1:
                    await AsTask.ToBackgroundContext(() => throw new ApplicationException("ToBackgroundContext"));

                    break;

                case 2:
                    await AsTask.ToContext(_contextTests2Id, () => throw new ApplicationException("ToContext"));

                    break;

                case 3:
                    await AsTask.ToStaticThreadPool(() => throw new ApplicationException("ToStaticThreadPool"));

                    break;

                case 4:
                    await AsTask.ToDynamicThreadPool(() => throw new ApplicationException("ToDynamicThreadPool"));

                    break;
                }
            }
        }
Example #4
0
        public async Task TestSwitchingToContexts()
        {
            await AsTask.ToBackgroundContext();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext);

            await AsTask.ToContext(_contextTests1Id);

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.ThreadContext);

            await AsTask.ToStaticThreadPool();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.StaticThreadPool);

            await AsTask.ToDynamicThreadPool();

            Assert.True(AsTask.GetCurrentContextType() == ThreadContextType.DynamicThreadPool);
        }
Example #5
0
        public async Task TestMoreSwitching()
        {
            for (var i = 0; i < 1000; i++)
            {
                await TestSwitchingToContexts();
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToMainContext();

                Assert.True(AsTask.IsMainContext());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToBackgroundContext();

                Assert.True(AsTask.IsBackgroundContext());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToContext(_contextTests1Id);

                Assert.True(AsTask.IsThreadContext(_contextTests1Id));
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToStaticThreadPool();

                Assert.True(AsTask.IsStaticThreadPool());
            }

            for (var i = 0; i < 1000; i++)
            {
                await AsTask.ToDynamicThreadPool();

                Assert.True(AsTask.IsDynamicThreadPool());
            }
        }