public void TestCancelReceive() { var cancelSource = new CancellationTokenSource(); var cancelToken = cancelSource.Token; var task = Task.Factory.StartNew( () => { using( var recv = new TmMqReceiver( "TestCancelReceiveQueue" ) ) { var count = recv.CountPending(); Assert.AreEqual( 0, count, "Should be no pending items" ); string messageResult = null; recv.Receive( m => messageResult = "MessageID: " + m.MessageId, cancelToken ); Assert.IsNull( messageResult, "Message was received, but nothing was posted?" ); } } ); cancelSource.Cancel(); if( !task.Wait( 10000 ) ) { //NOTE: No way to abort a task without AppDomain Assert.Fail( "Receive task didn't cancel on demand (waited 10 seconds). The receive task is still running." ); } }
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 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" ); } }