public async ValueTask TestSwitch3() { for (var i = 0; i < 100; i++) { await AsTask.ToDynamicThreadPool(); } }
public async Task TestToDynamicThreadPool1() { await AsTask.ToDynamicThreadPool(); Console.WriteLine(AsTask.WhereAmI()); Assert.True(AsTask.IsDynamicThreadPool(), "This is not the DynamicThreadPool!"); }
public async ValueTask TestDtp2() { await AsTask.ToDynamicThreadPool(); await Task.Delay(10); await Task.Yield(); }
public async ValueTask TestSwitch4() { for (var i = 0; i < 100; i++) { await AsTask.ToBackgroundContext(); await AsTask.ToStaticThreadPool(); await AsTask.ToDynamicThreadPool(); } }
public async Task TestDelayAwaiterDynamicThreadPool() { await AsTask.ToDynamicThreadPool(); var stopwatch = Stopwatch.StartNew(); await Task.Delay(100); stopwatch.Stop(); Assert.True(AsTask.IsDynamicThreadPool(), $"This is not the DynamicThreadPool: {AsTask.WhereAmI()}"); Console.WriteLine($"{nameof(stopwatch)} {stopwatch.ElapsedMilliseconds}ms == {AsTask.GetCurrentContextType()} awaiter 100ms"); Assert.True(stopwatch.ElapsedMilliseconds >= 80 && stopwatch.ElapsedMilliseconds <= 150); }
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"); } } }
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 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()); } }
public async ValueTask TestDtp1() { await AsTask.ToDynamicThreadPool(() => Thread.Sleep(10)); await Task.Yield(); }