コード例 #1
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
        public void MessageOfUnknownTypeShouldPauseProcessingTillCorrespondingHandlerIsRegisteredTest()
        {
            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                var type1Received = new AutoResetEvent(false);
                var type2Received = new AutoResetEvent(false);

                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    type1Received.Set();
                    acknowledge(true);
                }, "type1");

                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(type1Received.WaitOne(500), Is.True, "Message of subscribed type was not delivered");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2"
                }, 0);
                //Give time for type2 message to be  pushed back by mq
                //Thread.Sleep(500);
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(type1Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
                Assert.That(type2Received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { type2Received.Set();
                                                                                  acknowledge(true); }, "type2");
                Assert.That(type1Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered");
                Assert.That(type2Received.WaitOne(500), Is.True, "Processing was not resumed after handler for unknown message type was registered");
            }
        }
コード例 #2
0
        public void SendTest()
        {
            using (var transport = new InMemoryTransport())
            {
                var delivered1 = new ManualResetEvent(false);
                var delivered2 = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
                {
                    delivered1.Set();
                    Console.WriteLine("subscription1: message:" + message.Type);
                }, typeof(byte[]).Name);

                processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
                {
                    delivered2.Set();
                    Console.WriteLine("subscription2: message:" + message.Type);
                }, typeof(byte[]).Name);


                processingGroup.Send(TEST_TOPIC, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name
                }, 0);


                Assert.That(delivered1.WaitOne(1000), Is.True, "message was not delivered to all subscribers");
                Assert.That(delivered2.WaitOne(1000), Is.True, "message was not delivered to all subscribers");
                Thread.Sleep(1000);
            }
        }
コード例 #3
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
 public void AttemptToSubscribeSameDestinationWithoutMessageTypeTwiceFailureTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, null);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, null);
     }
 }
コード例 #4
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
        public void PerformanceTest(int messageSize)
        {
            var messageBytes = new byte[messageSize];

            new Random().NextBytes(messageBytes);

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                Stopwatch        sw = Stopwatch.StartNew();
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = messageBytes, Type = typeof(byte[]).Name
                }, 0);
                int sendCounter;
                for (sendCounter = 0; sw.ElapsedMilliseconds < 4000; sendCounter++)
                {
                    processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                        Bytes = messageBytes, Type = typeof(byte[]).Name
                    }, 0);
                }
                int receiveCounter = 0;

                var ev = new ManualResetEvent(false);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => receiveCounter++, typeof(byte[]).Name);
                ev.WaitOne(2000);
                Console.WriteLine("Send: {0} per second. {1:0.00} Mbit/s", sendCounter / 4, 1.0 * sendCounter * messageSize / 4 / 1024 / 1024 * 8);
                Console.WriteLine("Receive: {0} per second. {1:0.00}  Mbit/s", receiveCounter / 2, 1.0 * receiveCounter * messageSize / 2 / 1024 / 1024 * 8);
            }
        }
コード例 #5
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
        public void UnknownMessageTypeHandlerWaitingDoesNotPreventTransportDisposeTest()
        {
            var    received         = new ManualResetEvent(false);
            Thread connectionThread = null;

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    connectionThread = Thread.CurrentThread;
                    received.Set();
                }, "type1");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
                }, 0);
                Assert.That(received.WaitOne(100), Is.True, "Message was not delivered");
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type2"
                }, 0);
            }

            Thread.Sleep(200);
            Assert.That(connectionThread.ThreadState, Is.EqualTo(ThreadState.Stopped), "Processing thread is still active in spite of transport dispose");
        }
コード例 #6
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
        public void NackTest()
        {
            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                var delivered = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
                    Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = typeof(byte[]).Name
                }, 0);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
                {
                    Console.WriteLine("message:" + message.Type);
                    delivered.Set();
                    acknowledge(false);
                }, typeof(byte[]).Name);
                Assert.That(delivered.WaitOne(300), Is.True, "Message was not delivered");
            }

            using (var transport = new Transport(HOST, "guest", "guest"))
            {
                var delivered = new ManualResetEvent(false);
                IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
                processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => delivered.Set(), typeof(byte[]).Name);
                Assert.That(delivered.WaitOne(1000), Is.True, "Message was not returned to queue");
            }
        }
コード例 #7
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
 public void HandlerWaitStopsAndMessageOfUnknownTypeReturnsToQueueOnUnsubscribeTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         var         received             = new AutoResetEvent(false);
         IDisposable subscription         = processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) =>
         {
             received.Set();
             Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
         }, "type2");
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "type1"
         }, 0);
         Assert.That(received.WaitOne(500), Is.False, "Message of not subscribed type has not paused processing");
         subscription.Dispose();
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => received.Set(), "type1");
         Assert.That(received.WaitOne(500), Is.True, "Message was not returned to queue");
     }
 }
コード例 #8
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
 public void UnsubscribeTest(string messageType)
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         var ev = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = messageType
         }, 0);
         IDisposable subscription = processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => ev.Set(), messageType);
         Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered");
         subscription.Dispose();
         Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription");
     }
 }
コード例 #9
0
 public void UnsubscribeTest()
 {
     using (var transport = new InMemoryTransport())
     {
         var ev = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);
         IDisposable      subscription    = processingGroup.Subscribe(TEST_TOPIC, (message, ack) => ev.Set(), null);
         processingGroup.Send(TEST_TOPIC, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = null
         }, 0);
         Assert.That(ev.WaitOne(500), Is.True, "Message was not delivered");
         subscription.Dispose();
         Assert.That(ev.WaitOne(500), Is.False, "Message was delivered for canceled subscription");
     }
 }
コード例 #10
0
ファイル: TransportTests.cs プロジェクト: mmusin/Messaging
 public void ConnectionFailureTest()
 {
     using (var transport = new Transport(HOST, "guest", "guest"))
     {
         var onFailureCalled = new AutoResetEvent(false);
         IProcessingGroup processingGroup = transport.CreateProcessingGroup(() =>
         {
             onFailureCalled.Set();
             Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
         });
         processingGroup.Send(TEST_EXCHANGE, new BinaryMessage {
             Bytes = new byte[] { 0x0, 0x1, 0x2 }, Type = "messageType"
         }, 0);
         processingGroup.Subscribe(TEST_QUEUE, (message, acknowledge) => { }, "messageType");
         FieldInfo field      = typeof(ProcessingGroup).GetField("m_Connection", BindingFlags.NonPublic | BindingFlags.Instance);
         var       connection = field.GetValue(processingGroup) as IConnection;
         connection.Abort(1, "All your base are belong to us");
         Assert.That(onFailureCalled.WaitOne(500), Is.True, "Subsciptionwas not notefied on failure");
     }
 }
コード例 #11
0
        public void DisposeTest()
        {
            ManualResetEvent delivered = new ManualResetEvent(false);
            int deliveredMessagesCount = 0;
            var transport = new InMemoryTransport();
            IProcessingGroup processingGroup = transport.CreateProcessingGroup(null);

            processingGroup.Subscribe(TEST_TOPIC, (message, ack) =>
            {
                delivered.WaitOne();
                Interlocked.Increment(ref deliveredMessagesCount);
            }, null);
            processingGroup.Send(TEST_TOPIC, new BinaryMessage(), 0);
            Thread.Sleep(200);
            var task = Task.Factory.StartNew(transport.Dispose);

            Assert.That(task.Wait(200), Is.False, "transport was disposd before all message processing finished");
            delivered.Set();
            Assert.That(task.Wait(1000), Is.True, "transport was not disposd after all message processing finished");
        }
コード例 #12
0
 public IDisposable Subscribe(string destination, Action <BinaryMessage, Action <bool> > callback, string messageType)
 {
     ensureProcessingGroupIsCreated(destination);
     return(m_Instance.Subscribe(destination, callback, messageType));
 }