public void TestProperties()
        {
            using( var send = new TmMqSender( "TestProperties" ) )
            {
                var msg = new TmMqMessage();
                msg.Text = "msg1";
                msg.Properties.String = "string";
                msg.Properties.Int = 1234;
                msg.Properties.Complex = new Complex{ Prop1 = 1, PropD = 123.456, PropS = "abcdefg" };
                send.Send( msg );
            }

            using( var recv = new TmMqReceiver( "TestProperties" ) )
            {
                var count = recv.CountPending();
                Assert.AreEqual( 1, count, "Should be one pending item" );

                ITmMqMessage recieved = recv.Receive().FirstOrDefault();

                Assert.IsNotNull( recieved );
                Assert.AreEqual( "msg1", recieved.Text );
                Assert.AreEqual( "string", recieved.Properties.String, "Invalid recieved.Properties.String" );
                Assert.AreEqual( 1234, recieved.Properties.Int, "Invalid recieved.Properties.Int" );
                Assert.IsInstanceOfType( recieved.Properties.Complex, typeof(Complex) );
                Assert.AreEqual( 1, recieved.Properties.Complex.Prop1, "Invalid recieved.Properties.Complex.Prop1" );
                Assert.AreEqual( 123.456D, recieved.Properties.Complex.PropD, "Invalid recieved.Properties.Complex.PropD" );
                Assert.AreEqual( "abcdefg", recieved.Properties.Complex.PropS, "Invalid recieved.Properties.Complex.PropS" );
            }
        }
        public void TestSendBeforeReceiveStarted()
        {
            using( var send = new TmMqSender( "TestSendBeforeReceiveStarted" ) )
            {
                var msg = new TmMqMessage();
                msg.Text = "msg1";
                send.Send( msg );
            }

            using( var recv = new TmMqReceiver( "TestSendBeforeReceiveStarted" ) )
            {
                var count = recv.CountPending();
                Assert.AreEqual( 1, count, "Should be one pending item" );

                ITmMqMessage recieved = recv.Receive().FirstOrDefault();

                Assert.IsNotNull( recieved );
                Assert.AreEqual( "msg1", recieved.Text );
            }
        }
        public void TestStartReceive()
        {
            using( var recv = new TmMqReceiver( "TestStartReceive" ) )
            {
                ITmMqMessage recieved = null;

                using( var evt = new ManualResetEvent( false ) )
                {
                    using( var send = new TmMqSender( "TestStartReceive" ) )
                    {
                        recv.StartReceiving( 1, m =>
                                                {
                                                    recieved = m;
                                                    evt.Set();
                                                } );

                        var msg = new TmMqMessage();
                        msg.Text = "msg1";
                        send.Send( msg );
                    }

                    evt.WaitOne( 500 );
                }

                Assert.IsNotNull( recieved );
                Assert.AreEqual( "msg1", recieved.Text );
            }
        }
        public void TestRedeliver()
        {
            using( var send = new TmMqSender( "TestRedeliver" ) )
            {
                var msg = new TmMqMessage();
                msg.Text = "msg1";
                send.Send( msg );
            }

            using( var recv = new TmMqReceiver( "TestRedeliver" ) )
            {
                var count = recv.CountPending();
                Assert.AreEqual( 1, count, "Should be one pending item" );

                ITmMqMessage recieved = null;

                int retry = 0;

                using( var evt = new ManualResetEvent( false ) )
                {
                    recv.StartReceiving( 1, msg =>
                                                {
                                                    if( retry++ == 0 )
                                                    {
                                                        throw new Exception( "fail " + retry );
                                                    }

                                                    recieved = msg;
                                                    evt.Set();
                                                } );
                    evt.WaitOne( 5000 );
                }

                Assert.IsNotNull( recieved );
                Assert.AreEqual( "msg1", recieved.Text );
                Assert.AreEqual( 1, recieved.Errors.Count, "An error should have been logged" );
            }
        }
        public void TestErrorQueue()
        {
            using( var send = new TmMqSender( "TestErrorQueue" ) )
            {
                var msg = new TmMqMessage();
                msg.Text = "msg1 - fail";
                send.Send( msg );
            }

            using( var recv = new TmMqReceiver( "TestErrorQueue" ) )
            {
                int fail = 0;

                using( var evt = new ManualResetEvent( false ) )
                {
                    recv.StartReceiving( 1, msg =>
                                                {
                                                    if( fail++ == 2 )
                                                    {
                                                        evt.Set();
                                                    }

                                                    throw new Exception( "fail " );
                                                } );

                    evt.WaitOne( 5000 );
                }
            }

            using( var recv = new TmMqReceiver( "error" ) )
            {
                ITmMqMessage errorMsg = null;

                using( var evt = new ManualResetEvent( false ) )
                {
                    recv.StartReceiving( 1, msg =>
                    {
                        errorMsg = msg;
                        evt.Set();
                    } );

                    evt.WaitOne( 5000 );
                }

                Assert.IsNotNull( errorMsg );
                Assert.AreEqual( "msg1 - fail", errorMsg.Text );
                Assert.AreEqual( "TestErrorQueue", errorMsg.OriginalQueue );
            }
        }