Exemple #1
0
        public void CanInterceptNestedMessages() => AsyncContext.Run(async() =>
        {
            var normalInterceptions = 0;
            var nestedInterceptions = 0;

            const int expected = 123;
            using (var loop = new MessageLoop <int>(() => expected, interceptor: (i, ctx) =>
            {
                if (ctx.NestedMessage)
                {
                    nestedInterceptions++;
                }
                else
                {
                    normalInterceptions++;
                }

                return(ctx.Func(i));
            }))
            {
                var x = await loop.GetAsync(async n => await loop.GetAsync(async n2 => await loop.GetAsync(n3 => n3)));
                Assert.AreEqual(expected, x);
                Assert.AreEqual(1, normalInterceptions);
                Assert.AreEqual(2, nestedInterceptions);
            }
        });
Exemple #2
0
 public void CanNestMessages() => AsyncContext.Run(async() =>
 {
     const int expected = 123;
     using (var loop = new MessageLoop <int>(() => expected))
     {
         var x = await loop.GetAsync(async n => await loop.GetAsync(async n2 => await loop.GetAsync(n3 => n3)));
         Assert.AreEqual(expected, x);
     }
 });
Exemple #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);
            }
        });
Exemple #4
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);
            }
        });
Exemple #5
0
 public void CanGetAsync() => AsyncContext.Run(async() =>
 {
     using (var loop = new MessageLoop <int>(() => 1))
     {
         var n = await loop.GetAsync(x => x);
         Assert.AreEqual(1, n);
     }
 });
Exemple #6
0
 public void CanGetAsync() => AsyncContext.Run(async () =>
 {
     using (var loop = new MessageLoop<int>(() => 1))
     {
         var n = await loop.GetAsync(x => x);
         Assert.AreEqual(1, n);
     }
 });
Exemple #7
0
 async Task test()
 {
     using (var loop = new MessageLoop <string>(() => ""))
         await loop.GetAsync(state => Task.FromResult(DivZero()));
 }
Exemple #8
0
 public void TestGetAsyncException() => AsyncContext.Run(async() =>
 {
     using (var loop = new MessageLoop <string>(() => ""))
         await loop.GetAsync(state => Task.FromResult(DivZero()));
 });
Exemple #9
0
 public void TestGetAsyncException() => AsyncContext.Run(async () => 
 {
     using (var loop = new MessageLoop<string>(() => ""))
         await loop.GetAsync(state => Task.FromResult(DivZero()));
 });
Exemple #10
0
        public void CanInterceptNestedMessages() => AsyncContext.Run(async () =>
        {
            var normalInterceptions = 0;
            var nestedInterceptions = 0;

            const int expected = 123;
            using (var loop = new MessageLoop<int>(() => expected, interceptor: (i, ctx) =>
            {
                if (ctx.NestedMessage) nestedInterceptions++;
                else normalInterceptions++;

                return ctx.Func(i);
            }))
            {
                var x = await loop.GetAsync(async n => await loop.GetAsync(async n2 => await loop.GetAsync(n3 => n3)));
                Assert.AreEqual(expected, x);
                Assert.AreEqual(1, normalInterceptions);
                Assert.AreEqual(2, nestedInterceptions);

            }
        });
Exemple #11
0
 public void CanNestMessages() => AsyncContext.Run(async () =>
 {
     const int expected = 123;
     using (var loop = new MessageLoop<int>(() => expected))
     {
         var x = await loop.GetAsync(async n => await loop.GetAsync(async n2 => await loop.GetAsync(n3 => n3)));
         Assert.AreEqual(expected, x);
     }
 });