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); }
public async Task TestDelayAwaiterMainContext() { await AsTask.ToMainContext(); var stopwatch = Stopwatch.StartNew(); await Task.Delay(100); stopwatch.Stop(); Assert.True(AsTask.IsMainContext(), $"This is not the MainContext: {AsTask.WhereAmI()}"); Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms"); Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150); }
public async Task TestAwaitPost() { await AsTask.ToMainContext(); var stopwatch = Stopwatch.StartNew(); await AsTask.ToBackgroundContext(() => Thread.Sleep(1000)); stopwatch.Stop(); Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 1000ms"); Assert.True(stopwatch.ElapsedMilliseconds >= 1000); }