Exemple #1
0
        public virtual void Sync_Timeout()
        {
            int expected = 3;

            using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok * 20);
                    c.Write(expected);
                    Thread.Sleep(Timeblok * 20);
                    c.Write(expected);
                });

                Assert.AreEqual(expected, c.Read(Timeblok * 40));
                try
                {
                    Assert.AreEqual(expected, c.Read(Timeblok / 2));
                    Assert.Fail("Expected exception.");
                }
                catch (Exception e)
                {
                    Assert.IsInstanceOfType(e, typeof(TimeoutException));
                }
            }
        }
Exemple #2
0
        public virtual void ShovelTest()
        {
            int count = 100;

            int[]         expected = Enumerable.Range(0, count).ToArray();
            List <string> actual   = new List <string>();

            using (IChannel <int> c = new mq.Channel <int>("test-ShovelTest1" + Salt))
                using (IChannel <string> w = new mq.Channel <string>("test-ShovelTest2" + Salt))
                {
                    Task.Factory.StartNew(() =>
                    {
                        for (int i = 0; i < count; i++)
                        {
                            c.Write(i);
                        }

                        c.Close();
                    });

                    AbstractShovelThread <int, string> th = c.ShovelTo <int, string>(w, p => p.ToString(), TimeSpan.FromSeconds(10), true);
                    th.StoppedEvent += e => Assert.IsInstanceOfType(e, typeof(ChannelDrainedException));

                    foreach (string item in w.Enumerate())
                    {
                        actual.Add(item);
                    }

                    Assert.AreEqual(count, actual.Count);
                    CollectionAssert.AreEquivalent(expected.Select(p => p.ToString()).ToArray(), actual);
                }
        }
Exemple #3
0
        public virtual void ActiveEnumeratorTest()
        {
            int count = 100;

            int[]         expected = Enumerable.Range(0, count).ToArray();
            List <string> actual   = new List <string>();

            using (IChannel <int> c = new mq.Channel <int>("test-ActiveEnumeratorTest1" + Salt))
                using (IChannel <string> w = new mq.Channel <string>("test-ActiveEnumeratorTest2" + Salt))
                {
                    w.ActiveEnumerate(p => actual.Add(p)).StoppedEvent += e => {
                        Assert.IsInstanceOfType(e, typeof(ChannelDrainedException));
                        c.Write(0);
                    };

                    Task.Factory.StartNew(() =>
                    {
                        for (int i = 0; i < count; i++)
                        {
                            w.Write(i.ToString());
                        }

                        w.Close();
                    });

                    c.Read();

                    Assert.AreEqual(count, actual.Count);
                    CollectionAssert.AreEquivalent(expected.Select(p => p.ToString()).ToArray(), actual);
                }
        }
Exemple #4
0
        public virtual void SyncBarrier()
        {
            int expected = 3;

            using (IChannel <int> c0 = new mq.Channel <int>("test-SyncBarrier1" + Salt))
                using (IChannel <int> c1 = new mq.Channel <int>("test-SyncBarrier2" + Salt))
                    using (IChannel <int> c2 = new mq.Channel <int>("test-SyncBarrier3" + Salt))
                    {
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(Timeblok);
                            c0.Write(expected);
                        });
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(2 * Timeblok);
                            c1.Write(expected);
                        });
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(3 * Timeblok);
                            c2.Write(expected);
                        });

                        IEnumerable <IChannelReader <int> > res = c0.BarrierWith(c1, c2);
                        CollectionAssert.AreEquivalent(new IChannelReader <int>[] { c0, c1, c2 }, res.ToArray());
                        foreach (IChannelReader <int> item in res)
                        {
                            Assert.AreEqual(expected, item.Read());
                        }
                    }
        }
Exemple #5
0
        public virtual void SyncSelect()
        {
            int expected = 3;

            using (IChannel <int> c0 = new mq.Channel <int>("test-SyncSelect1" + Salt))
                using (IChannel <int> c1 = new mq.Channel <int>("test-SyncSelect2" + Salt))
                    using (IChannel <int> c2 = new mq.Channel <int>("test-SyncSelect3" + Salt))
                    {
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(Timeblok);
                            c0.Write(expected);
                        });
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(2 * Timeblok);
                            c1.Write(expected);
                        });
                        Task.Factory.StartNew(() =>
                        {
                            Thread.Sleep(3 * Timeblok);
                            c2.Write(expected);
                        });

                        IChannelReader <int> res = c0.SelectWith(c1, c2);
                        Assert.AreEqual(c0, res);
                        Assert.AreEqual(expected, res.Read());
                    }
        }
Exemple #6
0
        public virtual void Enumerate()
        {
            int count = 100;

            int[]      expected = Enumerable.Range(0, count).ToArray();
            List <int> actual   = new List <int>();

            using (IChannel <int> c = new mq.Channel <int>("test-Enumerate1" + Salt))
                using (IChannel <int> w = new mq.Channel <int>("test-Enumerate2" + Salt))
                {
                    Task.Factory.StartNew(() =>
                    {
                        for (int i = 0; i < count; i++)
                        {
                            c.Write(i);
                        }

                        c.Close();
                    });
                    Task.Factory.StartNew(() =>
                    {
                        foreach (int item in c.Enumerate())
                        {
                            actual.Add(item);
                        }

                        w.Write(count);
                        w.Close();
                    });

                    Assert.AreEqual(count, w.Read());
                    w.WaitReadable.WaitOne();
                    CollectionAssert.AreEquivalent(expected, actual);
                }
        }
Exemple #7
0
        public virtual void Sync_Transactional_2()
        {
            int expected = 3;

            using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok);
                    c.Write(expected);
                });

                IAbortableOperation <int> res = c.Consume(Timeblok * 20);
                Assert.AreEqual(expected, res.Value);
                res.Abort();
                res = c.Consume(Timeblok * 20);
                Assert.AreEqual(expected, res.Value);
                res.Abort();

                res = c.Consume(Timeblok * 20);
                Assert.AreEqual(expected, res.Value);
                res.Commit();

                try
                {
                    c.Consume(Timeblok / 2);
                    Assert.Fail("Expected exception.");
                }
                catch (Exception e)
                {
                    Assert.IsInstanceOfType(e, typeof(TimeoutException));
                }
            }
        }
Exemple #8
0
        public virtual void Acc()
        {
            int a = 1, b = 2;
            int expected = a + b;

            using (IChannel <int> c = new mq.Channel <int>("test-Acc" + Salt))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok);
                    c.Write(a);
                });
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok);
                    c.Write(b);
                });

                Assert.AreEqual(expected, c.Read() + c.Read());
            }
        }
Exemple #9
0
        public virtual void SyncWrite()
        {
            int expected = 3;

            using (IChannel <int> c = new mq.Channel <int>("test-SyncWrite1" + Salt))
                using (IChannel <int> w = new mq.Channel <int>("test-SyncWrite2" + Salt))
                {
                    Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(Timeblok);
                        c.Read();
                        c.Read();
                        c.Write(expected);
                        w.Write(0);
                    });
                    c.Write(0);
                    c.Write(1);
                    w.Read();
                    Assert.AreEqual(expected, c.Read());
                }
        }
Exemple #10
0
        public virtual void Sync()
        {
            int expected = 3;

            using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok);
                    c.Write(expected);
                });

                Assert.AreEqual(expected, c.Read());
            }
        }
Exemple #11
0
        public virtual void SyncDiff()
        {
            int expected = 3;

            INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt);
            {
                Task.Factory.StartNew(() =>
                {
                    INamedChannel <int> w = new mq.Channel <int>(c.Name);
                    Thread.Sleep(Timeblok);
                    w.Write(expected);
                });

                Assert.AreEqual(expected, c.Read());
            }
        }
Exemple #12
0
        public virtual void OutputAdapter()
        {
            int expected = 3;

            using (IChannel <int> c = new mq.Channel <int>("test-OutputAdapter" + Salt))
                using (IChannelReader <string> ca = new FuncChannelOutputAdapter <int, string>(c, p => p.ToString()))
                {
                    Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(Timeblok);
                        c.Write(expected);
                    });

                    Assert.AreEqual(expected.ToString(), ca.Read());
                }
        }
Exemple #13
0
        public virtual void Sync_Transactional()
        {
            int expected = 3;

            using (INamedChannel <int> c = new mq.Channel <int>("test-sync" + Salt))
            {
                Task.Factory.StartNew(() =>
                {
                    Thread.Sleep(Timeblok);
                    c.Write(expected);
                });

                try
                {
                    c.Consume(p => { throw new Exception(); }, Timeblok * 20);
                    Assert.Fail();
                }
                catch (OperationCanceledException e)
                { }

                try
                {
                    c.Consume(p => { throw new Exception(); }, Timeblok * 20);
                    Assert.Fail();
                }
                catch (OperationCanceledException e)
                { }

                c.Consume(p => Assert.AreEqual(expected, p), Timeblok * 20);

                try
                {
                    c.Consume(p => Assert.AreEqual(expected, p), Timeblok / 2);
                    Assert.Fail("Expected exception.");
                }
                catch (Exception e)
                {
                    Assert.IsInstanceOfType(e, typeof(TimeoutException));
                }
            }
        }
Exemple #14
0
        public virtual void ConcurretSingleQueue()
        {
            string salt = Salt;

            using (IChannel <int> c1 = new mq.Channel <int>("test-ConcurretSingleQueue" + salt))
                using (IChannel <int> c2 = new mq.Channel <int>("test-ConcurretSingleQueue" + salt))
                {
                    c2.Write(1);
                    c1.Write(2);

                    IChannelReader <int> x = c1.SelectWith(c2);

                    Assert.AreEqual(1, x.Read());

                    IChannelReader <int> y = c1.SelectWith(c2);

                    Assert.AreNotEqual(x, y);
                    Assert.AreEqual(2, y.Read());
                    Console.WriteLine();
                }
        }
Exemple #15
0
        public virtual void ConcurrentReaders()
        {
            int count = 2000;
            int r0    = 0;
            int r1    = 0;

            using (IChannel <int> c = new mq.Channel <int>("test-ConcurrentReaders1" + Salt))
                using (IChannel <int> w0 = new mq.Channel <int>("test-ConcurrentReaders2" + Salt))
                    using (IChannel <int> w1 = new mq.Channel <int>("test-ConcurrentReaders3" + Salt))
                    {
                        Task.Factory.StartNew(() =>
                        {
                            foreach (int item in c.Enumerate())
                            {
                                r0++;
                            }
                            w0.Write(r0);
                        });
                        Task.Factory.StartNew(() =>
                        {
                            foreach (int item in c.Enumerate())
                            {
                                r1++;
                            }
                            w1.Write(r1);
                        });

                        for (int i = 0; i < count; i++)
                        {
                            c.Write(i);
                        }
                        c.Close();

                        Assert.AreEqual(count, w0.Read() + w1.Read());
                        Assert.IsTrue(r0 > 0);
                        Assert.IsTrue(r1 > 0);
                        //Assert.AreEqual(0.5, r0 / (double)count, 0.2);
                        //Assert.AreEqual(0.5, r1 / (double)count, 0.2);
                    }
        }
Exemple #16
0
        public virtual void SubscribedReaders()
        {
            int count = 10;
            int r0    = 0;
            int r1    = 0;

            using (ISubscribableChannel <int> c = new mq.SubscribableChannel <int>("test-SubscribedReaders1" + Salt))
                using (IChannel <int> w0 = new mq.Channel <int>("test-SubscribedReaders2" + Salt))
                    using (IChannel <int> w1 = new mq.Channel <int>("test-SubscribedReaders3" + Salt))
                    {
                        Task.Factory.StartNew((object rd) =>
                        {
                            foreach (int item in ((IChannelReader <int>)rd).Enumerate())
                            {
                                r0++;
                            }
                            w0.Write(r0);
                        }, c.Subscribe());

                        Task.Factory.StartNew((object rd) =>
                        {
                            foreach (int item in ((IChannelReader <int>)rd).Enumerate())
                            {
                                r1++;
                            }
                            w1.Write(r1);
                        }, c.Subscribe());

                        for (int i = 0; i < count; i++)
                        {
                            c.Write(i);
                        }
                        c.Close();

                        Assert.AreEqual(2 * count, w0.Read() + w1.Read());
                        Assert.AreEqual(count, r0);
                        Assert.AreEqual(count, r1);
                    }
        }