예제 #1
0
 public void TestInvalidMultiAccessOperation()
 {
     Assert.Throws <InvalidOperationException>(() =>
     {
         try
         {
             var c1 = ChannelManager.CreateChannel <int>();
             MultiChannelAccess.ReadOrWriteAnyAsync(MultisetRequest.Read(c1), MultisetRequest.Write(1, c1)).WaitForTask().Wait();
         }
         catch (AggregateException aex)
         {
             if (aex.InnerExceptions.Count == 1)
             {
                 throw aex.InnerExceptions.First();
             }
             throw;
         }
     });
 }
예제 #2
0
        public void TestMultiAccessOperation()
        {
            var c1 = ChannelManager.CreateChannel <int>();
            var c2 = ChannelManager.CreateChannel <int>();

            // Copy c2 + 1 => c1
            Func <Task> p1 = async() => {
                var val = await c2.ReadAsync();

                while (true)
                {
                    var res = await MultiChannelAccess.ReadOrWriteAnyAsync(MultisetRequest.Read(c2), MultisetRequest.Write(val, c1));

                    if (res.IsRead)
                    {
                        val = res.Value + 1;
                    }
                }
            };

            // Copy c1 => c2
            Func <Task> p2 = async() => {
                var val = 1;
                for (var i = 0; i < 10; i++)
                {
                    await c2.WriteAsync(val);

                    val = await c1.ReadAsync();
                }

                c1.Retire();
                c2.Retire();

                if (val != 10)
                {
                    throw new InvalidProgramException("Bad counter!");
                }
            };

            // Wait for shutdown
            try
            {
                Task.WhenAll(p1(), p2()).WaitForTask().Wait();
            }
            catch (Exception ex)
            {
                // Filter out all ChannelRetired exceptions
                if (ex is AggregateException)
                {
                    var rex = (from n in (ex as AggregateException).InnerExceptions
                               where !(n is RetiredException)
                               select n);

                    if (rex.Count() == 1)
                    {
                        throw rex.First();
                    }
                    else if (rex.Count() != 0)
                    {
                        throw new AggregateException(rex);
                    }
                }
                else
                {
                    throw;
                }
            }
        }