Exemplo n.º 1
0
        public virtual void CloseTest()
        {
            int count = 100;

            int[]      expected  = Enumerable.Range(0, count).ToArray();
            List <int> actualInt = new List <int>();
            Dictionary <string, string> testString = new Dictionary <string, string>()
            {
                { "val", "test" }
            };
            string queueName = "test-CloseTest" + Salt;

            using (ISubscribableChannel <Dictionary <string, string> > w = new mq.SubscribableChannel <Dictionary <string, string> >(queueName))
                using (IChannel <Dictionary <string, string> > c = new mq.Channel <Dictionary <string, string> >(queueName))
                {
                    IChannelReader <Dictionary <string, string> > t = w.Subscribe();

                    c.Close();

                    w.Write(testString);

                    Dictionary <string, string> result = t.Read();
                    Assert.IsTrue(result.ContainsKey("val"));
                    Assert.AreEqual(testString["val"], result["val"]);
                }
        }
Exemplo n.º 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);
                }
        }
Exemplo n.º 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);
                }
        }
Exemplo n.º 4
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);
                }
        }
Exemplo n.º 5
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);
                    }
        }