public void CanInterceptMessages() => AsyncContext.Run(async() => { var i = 0; using (var loop = new MessageLoop <int>(() => 0, interceptor: async(s, ctx) => { i++; try { var result = await ctx.Func(s); return(result); } finally { i++; } })) { await loop.DoAsync(w => Task.FromResult(i++)); try { await loop.DoAsync(w => DivZero()); } catch { } } Assert.AreEqual(5, i); });
public void Stress() => AsyncContext.Run(async() => { const int workerCount = 10; const int iters = 100; using (var loop = new MessageLoop <Wrapper <int> >(() => new Wrapper <int>())) { var workers = System.Linq.Enumerable.Range(0, workerCount) .Select(i => { return(Task.Run(() => { for (int j = 0; j < iters; j++) { loop.DoAsync(w => w.Value++).Wait(); } })); }).ToArray(); Task.WaitAll(workers); var n = await loop.GetAsync(async x => { return(await Task.FromResult(x.Value)); }); Assert.AreEqual(workerCount * iters, n); } });
public void Stress() => AsyncContext.Run(async () => { const int workerCount = 10; const int iters = 100; using (var loop = new MessageLoop<Wrapper<int>>(() => new Wrapper<int>())) { var workers = System.Linq.Enumerable.Range(0, workerCount) .Select(i => { return Task.Run(() => { for (int j = 0; j < iters; j++) { loop.DoAsync(w => w.Value++).Wait(); } }); }).ToArray(); Task.WaitAll(workers); var n = await loop.GetAsync(async x => { return await Task.FromResult(x.Value); }); Assert.AreEqual(workerCount*iters, n); } });
public void CanDoAsync() => AsyncContext.Run(async() => { using (var loop = new MessageLoop <int>(() => 1)) { int n = 0; await loop.DoAsync(x => n = x); Assert.AreEqual(1, n); } });
public void CanDoAsync() => AsyncContext.Run(async () => { using (var loop = new MessageLoop<int>(() => 1)) { int n = 0; await loop.DoAsync(x => n = x); Assert.AreEqual(1, n); } });
public void TestDoException() => AsyncContext.Run(async() => { using (var loop = new MessageLoop <string>(() => "")) await loop.DoAsync(state => DivZero()); });
public void CanInterceptMessages() => AsyncContext.Run(async () => { var i = 0; using (var loop = new MessageLoop<int>(() => 0, interceptor: async (s, ctx) => { i++; try { var result = await ctx.Func(s); return result; } finally { i++; } })) { await loop.DoAsync(w => Task.FromResult(i++)); try { await loop.DoAsync(w => DivZero()); } catch { } } Assert.AreEqual(5, i); });
public void TestDoException() => AsyncContext.Run(async () => { using (var loop = new MessageLoop<string>(() => "")) await loop.DoAsync(state => DivZero()); });