Ejemplo 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"]);
                }
        }
Ejemplo n.º 2
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);
                }
        }
Ejemplo n.º 3
0
        public virtual void UntypedTypesTest2()
        {
            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-UntypedTypesTest2" + Salt;

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

                #region bare rabbitmq
                ConnectionFactory fc = new ConnectionFactory()
                {
                    HostName = "code.test.aliaslab.net", Port = 5672, VirtualHost = "channels-lib", UserName = "******", Password = "******"
                };
                IConnection connection = fc.CreateConnection();
                IModel      _channel   = connection.CreateModel();

                _channel.BasicQos(0, 1, true);

                _channel.ExchangeDeclare(queueName, ExchangeType.Fanout, true, true, null);
                IBasicProperties prop = _channel.CreateBasicProperties();
                prop.ContentEncoding = "utf-8";
                prop.ContentType     = "application/json";
                prop.Persistent      = true;
                prop.Headers         = new Dictionary <string, object>();

                byte[] data = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { name = "piero", age = 13 }));
                _channel.BasicPublish(queueName, "test", prop, data);

                _channel.BasicPublish("channels-subscribableChannelCollectorExchange", $"{queueName}", prop, data);

                #endregion

                w.Write(testString);

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