1 // 2 // Test.Mono.Messaging.RabbitMQ 3 // 4 // Authors: 5 // Michael Barker (mike@middlesoft.co.uk) 6 // 7 // (C) 2008 Michael Barker 8 // 9 10 // 11 // Permission is hereby granted, free of charge, to any person obtaining 12 // a copy of this software and associated documentation files (the 13 // "Software"), to deal in the Software without restriction, including 14 // without limitation the rights to use, copy, modify, merge, publish, 15 // distribute, sublicense, and/or sell copies of the Software, and to 16 // permit persons to whom the Software is furnished to do so, subject to 17 // the following conditions: 18 // 19 // The above copyright notice and this permission notice shall be 20 // included in all copies or substantial portions of the Software. 21 // 22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 // 30 31 using System; 32 using System.Messaging; 33 using System.Reflection; 34 using System.Threading; 35 using System.Text.RegularExpressions; 36 37 using NUnit.Framework; 38 39 namespace MonoTests.System.Messaging 40 { 41 [TestFixture] 42 public class AsyncReceiveTest { 43 44 private Message m; 45 private string failureMessage = null; 46 private string state = null; 47 HandleMessage(object source, ReceiveCompletedEventArgs args)48 private void HandleMessage (object source, ReceiveCompletedEventArgs args) { 49 try { 50 MessageQueue q = (MessageQueue) source; 51 m = q.EndReceive (args.AsyncResult); 52 state = (string) args.AsyncResult.AsyncState; 53 } catch (Exception e) { 54 failureMessage = e.Message; 55 } 56 } 57 58 [Test] BeginReceive()59 public void BeginReceive() 60 { 61 MessageQueue q = MQUtil.GetQueue (); 62 Message s = new Message (new BinaryMessageFormatter ()); 63 string body = "foo-" + DateTime.Now.ToString (); 64 s.Body = body; 65 q.Send (s); 66 67 q.ReceiveCompleted += new ReceiveCompletedEventHandler (HandleMessage); 68 IAsyncResult result = q.BeginReceive (); 69 result.AsyncWaitHandle.WaitOne (); 70 Message rMsg = q.EndReceive (result); 71 Assert.IsNotNull (rMsg, "No message received"); 72 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal"); 73 } 74 75 [Test] BeginReceiveWithTimeout()76 public void BeginReceiveWithTimeout() 77 { 78 MessageQueue q = MQUtil.GetQueue (); 79 Message s = new Message (new BinaryMessageFormatter ()); 80 string body = "foo-" + DateTime.Now.ToString (); 81 s.Body = body; 82 q.Send (s); 83 84 IAsyncResult result = q.BeginReceive (new TimeSpan (0, 0, 2)); 85 result.AsyncWaitHandle.WaitOne (); 86 Message rMsg = q.EndReceive (result); 87 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal"); 88 } 89 90 [Test] BeginReceiveWithStateAndTimeout()91 public void BeginReceiveWithStateAndTimeout() 92 { 93 MessageQueue q = MQUtil.GetQueue (); 94 Message s = new Message (new BinaryMessageFormatter ()); 95 string body = "foo-" + DateTime.Now.ToString (); 96 s.Body = body; 97 q.Send (s); 98 99 IAsyncResult result = q.BeginReceive (new TimeSpan (0, 0, 2), "foo"); 100 result.AsyncWaitHandle.WaitOne (); 101 Message rMsg = q.EndReceive (result); 102 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal"); 103 Assert.AreEqual ("foo", result.AsyncState, "State not passed properly"); 104 } 105 106 private bool success = false; 107 TestCallback(IAsyncResult result)108 public void TestCallback (IAsyncResult result) 109 { 110 success = true; 111 } 112 113 [Test] BeginReceiveWithStateAndTimeoutAndCallback()114 public void BeginReceiveWithStateAndTimeoutAndCallback() 115 { 116 MessageQueue q = MQUtil.GetQueue (); 117 Message s = new Message (new BinaryMessageFormatter ()); 118 string body = "foo-" + DateTime.Now.ToString (); 119 s.Body = body; 120 q.Send (s); 121 AsyncCallback ac = new AsyncCallback (TestCallback); 122 IAsyncResult result = q.BeginReceive (new TimeSpan (0, 0, 2), "foo", ac); 123 result.AsyncWaitHandle.WaitOne (); 124 Message rMsg = q.EndReceive (result); 125 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal"); 126 Assert.AreEqual ("foo", result.AsyncState, "State not passed properly"); 127 Assert.IsTrue (success, "Callback not run"); 128 } 129 130 [Test] 131 [ExpectedException (typeof (MessageQueueException))] BeginReceiveWithException()132 public void BeginReceiveWithException() 133 { 134 MessageQueue q = MQUtil.GetQueue (); 135 IAsyncResult result = q.BeginReceive (new TimeSpan (0, 0, 2)); 136 result.AsyncWaitHandle.WaitOne (); 137 q.EndReceive (result); 138 } 139 } 140 } 141