Ejemplo n.º 1
0
        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);
        });
Ejemplo n.º 2
0
        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);
            }
        });
Ejemplo n.º 3
0
        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);
            }
        });
Ejemplo n.º 4
0
 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);
     }
 });
Ejemplo n.º 5
0
 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);
     }
 });
Ejemplo n.º 6
0
 public void TestDoException() => AsyncContext.Run(async() =>
 {
     using (var loop = new MessageLoop <string>(() => ""))
         await loop.DoAsync(state => DivZero());
 });
Ejemplo n.º 7
0
        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);
        });
Ejemplo n.º 8
0
 public void TestDoException() => AsyncContext.Run(async () =>
 {
     using (var loop = new MessageLoop<string>(() => ""))
         await loop.DoAsync(state => DivZero());
 });