Esempio n. 1
0
        public void SameThreadSubscriptionTest()
        {
            var transportManager = new TransportManager(
                _logFactory,
                new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "transport-1", new TransportInfo("transport-1", "login1", "pwd1", "None", "InMemory") }
            }));
            var processingGroupManager = new ProcessingGroupManager(_logFactory, transportManager);

            var session = transportManager.GetMessagingSession("transport-1", "pg"); var usedThreads = new List <int>();
            var subscription = processingGroupManager.Subscribe(new Endpoint {
                Destination = "queue", TransportId = "transport-1"
            },
                                                                (message, action) =>
            {
                lock (usedThreads)
                {
                    usedThreads.Add(Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine(Thread.CurrentThread.Name + Thread.CurrentThread.ManagedThreadId);
                }
                Thread.Sleep(50);
            }, null, "pg", 0);

            using (subscription)
            {
                Enumerable.Range(1, 20).ToList().ForEach(i => session.Send("queue", new BinaryMessage(), 0));
                Thread.Sleep(2000);
            }
            Assert.That(usedThreads.Count(), Is.EqualTo(20), "not all messages were processed");
            Assert.That(usedThreads.Distinct().Count(), Is.EqualTo(1), "more then one thread was used for message processing");
        }
        public void MultiThreadThreadSubscriptionTest()
        {
            var transportManager = new TransportManager(
                _logFactory,
                new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "transport-1", new TransportInfo("transport-1", "login1", "pwd1", "None", "InMemory") }
            }));
            var processingGroupManager = new ProcessingGroupManager(_logFactory, transportManager, new Dictionary <string, ProcessingGroupInfo>()
            {
                {
                    "pg", new ProcessingGroupInfo()
                    {
                        ConcurrencyLevel = 3
                    }
                }
            });
            var endpoint = new Endpoint {
                Destination = "queue", TransportId = "transport-1"
            };
            var processingGroup = transportManager.GetMessagingSession(endpoint, "pg");
            var usedThreads     = new List <int>();
            var subscription    = processingGroupManager.Subscribe(
                endpoint,
                (message, action) =>
            {
                lock (usedThreads)
                {
                    usedThreads.Add(Thread.CurrentThread.ManagedThreadId);
                }
                Thread.Sleep(50);
            },
                null,
                "pg",
                0);

            using (subscription)
            {
                Enumerable.Range(1, 20)
                .ToList()
                .ForEach(i => processingGroup.Send(
                             "queue",
                             new BinaryMessage {
                    Bytes = Encoding.UTF8.GetBytes((i % 3).ToString())
                },
                             0));
                Thread.Sleep(1200);
            }
            Assert.That(usedThreads.Count, Is.EqualTo(20), "not all messages were processed");
            Assert.That(usedThreads.Distinct().Count(), Is.EqualTo(3), "wrong number of threads was used for message processing");
        }
        public void QueuedTaskSchedulerIsHiddenTest()
        {
            var transportManager = new TransportManager(
                _logFactory,
                new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "transport-1", new TransportInfo("transport-1", "login1", "pwd1", "None", "InMemory") }
            }));
            var processingGroupManager = new ProcessingGroupManager(_logFactory, transportManager, new Dictionary <string, ProcessingGroupInfo>()
            {
                {
                    "pg", new ProcessingGroupInfo()
                    {
                        ConcurrencyLevel = 1, QueueCapacity = 1000
                    }
                }
            });

            var e        = new ManualResetEvent(false);
            var endpoint = new Endpoint {
                Destination = "queue", TransportId = "transport-1"
            };
            var processingGroup = transportManager.GetMessagingSession(endpoint, "pg");
            var childTaskFinishedBeforeHandler = false;
            var subscription = processingGroupManager.Subscribe(
                endpoint,
                (message, action) =>
            {
                if (Task.Factory.StartNew(() => { }).Wait(500))
                {
                    childTaskFinishedBeforeHandler = true;
                }
                e.Set();
            },
                null,
                "pg",
                0);

            using (subscription)
            {
                processingGroup.Send("queue", new BinaryMessage {
                    Bytes = Encoding.UTF8.GetBytes((100).ToString())
                }, 0);
                e.WaitOne(1000);
                Assert.That(childTaskFinishedBeforeHandler, "Child task used scheduler from QueuedTaskScheduler");
            }
        }
Esempio n. 4
0
        public void MultiThreadPrioritizedThreadSubscriptionTest()
        {
            var transportManager = new TransportManager(
                _logFactory,
                new TransportResolver(new Dictionary <string, TransportInfo>
            {
                { "transport-1", new TransportInfo("transport-1", "login1", "pwd1", "None", "InMemory") }
            }));
            var processingGroupManager = new ProcessingGroupManager(_logFactory, transportManager, new Dictionary <string, ProcessingGroupInfo>()
            {
                {
                    "pg", new ProcessingGroupInfo {
                        ConcurrencyLevel = 3
                    }
                }
            });

            var usedThreads       = new List <int>();
            var processedMessages = new List <int>();
            Action <BinaryMessage, AcknowledgeDelegate> callback = (message, action) =>
            {
                lock (usedThreads)
                {
                    usedThreads.Add(Thread.CurrentThread.ManagedThreadId);
                    processedMessages.Add(int.Parse(Encoding.UTF8.GetString(message.Bytes)));
                    Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ":" + Encoding.UTF8.GetString(message.Bytes));
                }
                Thread.Sleep(50);
            };

            var subscription0 = processingGroupManager.Subscribe(new Endpoint {
                Destination = "queue0", TransportId = "transport-1"
            }, callback, null, "pg", 0);
            var subscription1 = processingGroupManager.Subscribe(new Endpoint {
                Destination = "queue1", TransportId = "transport-1"
            }, callback, null, "pg", 1);
            var subscription2 = processingGroupManager.Subscribe(new Endpoint {
                Destination = "queue2", TransportId = "transport-1"
            }, callback, null, "pg", 2);

            using (subscription0)
                using (subscription1)
                    using (subscription2)
                    {
                        Enumerable.Range(1, 20).ToList()
                        .ForEach(i => processingGroupManager.Send(new Endpoint {
                            Destination = "queue" + i % 3, TransportId = "transport-1"
                        }, new BinaryMessage {
                            Bytes = Encoding.UTF8.GetBytes((i % 3).ToString())
                        }, 0, "pg"));

                        Thread.Sleep(1200);
                        ResolvedTransport transport = transportManager.ResolveTransport("transport-1");
                        Assert.That(transport.Sessions.Select(s => s.Name).OrderBy(s => s), Is.EqualTo(new[] { "pg priority0", "pg priority1", "pg priority2" }), "Wrong sessions were created. Expectation: one session per [processingGroup,priority] pair ");
                    }
            Assert.That(usedThreads.Count(), Is.EqualTo(20), "not all messages were processed");
            Assert.That(usedThreads.Distinct().Count(), Is.EqualTo(3), "wrong number of threads was used for message processing");

            double averageOrder0 = processedMessages.Select((i, index) => new { message = i, index }).Where(arg => arg.message == 0).Average(arg => arg.index);
            double averageOrder1 = processedMessages.Select((i, index) => new { message = i, index }).Where(arg => arg.message == 1).Average(arg => arg.index);
            double averageOrder2 = processedMessages.Select((i, index) => new { message = i, index }).Where(arg => arg.message == 2).Average(arg => arg.index);

            Assert.That(averageOrder0, Is.LessThan(averageOrder1), "priority was not respected");
            Assert.That(averageOrder1, Is.LessThan(averageOrder2), "priority was not respected");
        }