Пример #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 MessagingSessionFailureCallbackTest()
        {
            var    resolver = MockTransportResolver();
            Action createdSessionOnFailure = () => { Console.WriteLine("!!"); };
            var    transport = new Mock <ITransport>();

            transport
            .Setup(t => t.CreateSession(It.IsAny <Action>(), It.IsAny <Destination>()))
            .Callback <Action, Destination>((invocation, dst) => createdSessionOnFailure = invocation);
            var factory = new Mock <ITransportFactory>();

            factory.Setup(f => f.Create(It.IsAny <TransportInfo>(), It.IsAny <Action>())).Returns(transport.Object);
            factory.Setup(f => f.Name).Returns("Mock");
            var transportManager = new TransportManager(_logFactory, resolver, factory.Object);
            int i = 0;

            transportManager.GetMessagingSession(
                new Endpoint {
                TransportId = TransportConstants.TRANSPORT_ID3
            },
                "test",
                () => { Interlocked.Increment(ref i); });

            createdSessionOnFailure();
            createdSessionOnFailure();

            Assert.That(i, Is.Not.EqualTo(0), "Session failure callback was not called");
            Assert.That(i, Is.EqualTo(1), "Session  failure callback was called twice");
        }
        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");
            }
        }
Пример #5
0
        public void MessagingSessionFailureCallbackTest()
        {
            var    resolver  = MockTransportResolver();
            var    factory   = MockRepository.GenerateMock <ITransportFactory>();
            var    transport = MockRepository.GenerateMock <ITransport>();
            Action creaedSessionOnFailure = () => { Console.WriteLine("!!"); };

            transport.Expect(t => t.CreateSession(null)).IgnoreArguments().WhenCalled(invocation => creaedSessionOnFailure = (Action)invocation.Arguments[0]);
            factory.Expect(f => f.Create(null, null, null)).IgnoreArguments().Return(transport);
            factory.Expect(f => f.Name).Return("Mock");
            var transportManager = new TransportManager(_logFactory, resolver, factory);
            int i = 0;

            transportManager.GetMessagingSession(TransportConstants.TRANSPORT_ID3, "test", () => { Interlocked.Increment(ref i); });
            creaedSessionOnFailure();
            creaedSessionOnFailure();

            Assert.That(i, Is.Not.EqualTo(0), "Session failure callback was not called");
            Assert.That(i, Is.EqualTo(1), "Session  failure callback was called twice");
        }
        public void ConcurrentTransportResolutionTest()
        {
            var resolver         = MockTransportResolver();
            var transportManager = new TransportManager(_logFactory, resolver, new InMemoryTransportFactory());
            var start            = new ManualResetEvent(false);
            int errorCount       = 0;
            int attemptCount     = 0;

            foreach (var i in Enumerable.Range(1, 10))
            {
                var thread = new Thread(() =>
                {
                    start.WaitOne();
                    try
                    {
                        transportManager.GetMessagingSession(
                            new Endpoint {
                            TransportId = TransportConstants.TRANSPORT_ID1
                        },
                            "test");
                        Interlocked.Increment(ref attemptCount);
                    }
                    catch (Exception)
                    {
                        Interlocked.Increment(ref errorCount);
                    }
                });
                thread.Start();
            }

            start.Set();
            while (attemptCount < 10)
            {
                Thread.Sleep(50);
            }

            Assert.That(errorCount, Is.EqualTo(0));
        }