Exemplo n.º 1
0
        public async Task TestToBackgroundContext()
        {
            await AsTask.ToBackgroundContext();

            Console.WriteLine(AsTask.WhereAmI());
            Assert.True(AsTask.IsBackgroundContext(), "This is not the BackgroundContext!");
        }
Exemplo n.º 2
0
        public async Task TestDelayAwaiterBackgroundContext()
        {
            await AsTask.ToBackgroundContext();

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

            stopwatch.Stop();
            Assert.True(AsTask.IsBackgroundContext(), $"This is not the BackgroundContext: {AsTask.WhereAmI()}");
            Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms");
            Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150);
        }
Exemplo n.º 3
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());
            }
        }