コード例 #1
0
        public virtual void DifferentTypesTest()
        {
            int count = 100;

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

            using (ISubscribableChannel <int> c = new mq.SubscribableChannel <int>(queueName))
                using (ISubscribableChannel <string> w = new mq.SubscribableChannel <string>(queueName))
                {
                    IChannelReader <string> t = w.Subscribe();
                    IChannelReader <int>    x = c.Subscribe();
                    Task.Factory.StartNew(() =>
                    {
                        for (int i = 0; i < count; i++)
                        {
                            if (i == 2)
                            {
                                w.Write(testString);
                            }
                            c.Write(i);
                        }

                        c.Close();
                    });

                    string val = t.Read();
                    foreach (int item in x.Enumerate())
                    {
                        actualInt.Add(item);
                    }
                    w.Write(testString + "1");
                    val = t.Read();

                    Assert.AreEqual(val, testString + "1");
                    Assert.IsFalse(t.Drained);
                    Assert.IsTrue(x.Drained);
                }
        }
コード例 #2
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);
                    }
        }