Example #1
0
        public async Task TestToStaticThreadPool()
        {
            await AsTask.ToStaticThreadPool();

            Console.WriteLine(AsTask.WhereAmI());
            Assert.True(AsTask.IsStaticThreadPool(), "This is not the StaticThreadPool!");
        }
Example #2
0
 public async ValueTask TestSwitch2()
 {
     for (var i = 0; i < 100; i++)
     {
         await AsTask.ToStaticThreadPool();
     }
 }
Example #3
0
        private static async Task <long> FindPrimeNumberAsync(int n)
        {
            // Here we switch to a normal thread pool to do the heavy work...
            await AsTask.ToStaticThreadPool();

            var  count = 0;
            long a     = 2;

            while (count < n)
            {
                long b     = 2;
                var  prime = 1;
                while (b * b <= a)
                {
                    if (a % b == 0)
                    {
                        prime = 0;
                        break;
                    }

                    b++;
                }

                if (prime > 0)
                {
                    count++;
                }

                a++;
            }

            return(--a);
        }
Example #4
0
        public async ValueTask TestStp2()
        {
            await AsTask.ToStaticThreadPool();

            await Task.Delay(10);

            await Task.Yield();
        }
Example #5
0
        public async ValueTask TestSwitch4()
        {
            for (var i = 0; i < 100; i++)
            {
                await AsTask.ToBackgroundContext();

                await AsTask.ToStaticThreadPool();

                await AsTask.ToDynamicThreadPool();
            }
        }
Example #6
0
        public async Task TestDelayAwaiterStaticThreadPool()
        {
            await AsTask.ToStaticThreadPool();

            var stopwatch = Stopwatch.StartNew();
            await Task.Delay(100);

            stopwatch.Stop();
            Assert.True(AsTask.IsStaticThreadPool(), $"This is not the StaticThreadPool: {AsTask.WhereAmI()}");
            Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms");
            Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150);
        }
Example #7
0
        public async Task TestExceptions1()
        {
            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");

                case 1:
                    await AsTask.ToBackgroundContext();

                    throw new ApplicationException("ToBackgroundContext");

                case 2:
                    await AsTask.ToContext(_contextTests2Id);

                    throw new ApplicationException("ToContext");

                case 3:
                    await AsTask.ToStaticThreadPool();

                    throw new ApplicationException("ToStaticThreadPool");

                case 4:
                    await AsTask.ToDynamicThreadPool();

                    throw new ApplicationException("ToDynamicThreadPool");
                }
            }
        }
Example #8
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 #9
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());
            }
        }
Example #10
0
        public async ValueTask TestStp1()
        {
            await AsTask.ToStaticThreadPool(() => Thread.Sleep(10));

            await Task.Yield();
        }