2 // Test.Mono.Messaging.RabbitMQ
5 // Michael Barker (mike@middlesoft.co.uk)
7 // (C) 2008 Michael Barker
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Messaging;
33 using System.Reflection;
34 using System.Threading;
35 using System.Text.RegularExpressions;
37 using NUnit.Framework;
39 namespace MonoTests.System.Messaging
42 public class AsyncReceiveTest {
45 private string failureMessage = null;
46 private string state = null;
48 private void HandleMessage (object source, ReceiveCompletedEventArgs args) {
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;
59 public void BeginReceive()
61 MessageQueue q = MQUtil.GetQueue ();
62 Message s = new Message (new BinaryMessageFormatter ());
63 string body = "foo-" + DateTime.Now.ToString ();
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");
76 public void BeginReceiveWithTimeout()
78 MessageQueue q = MQUtil.GetQueue ();
79 Message s = new Message (new BinaryMessageFormatter ());
80 string body = "foo-" + DateTime.Now.ToString ();
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");
91 public void BeginReceiveWithStateAndTimeout()
93 MessageQueue q = MQUtil.GetQueue ();
94 Message s = new Message (new BinaryMessageFormatter ());
95 string body = "foo-" + DateTime.Now.ToString ();
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");
106 private bool success = false;
108 public void TestCallback (IAsyncResult result)
114 public void BeginReceiveWithStateAndTimeoutAndCallback()
116 MessageQueue q = MQUtil.GetQueue ();
117 Message s = new Message (new BinaryMessageFormatter ());
118 string body = "foo-" + DateTime.Now.ToString ();
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");
131 [ExpectedException (typeof (MessageQueueException))]
132 public void BeginReceiveWithException()
134 MessageQueue q = MQUtil.GetQueue ();
135 IAsyncResult result = q.BeginReceive (new TimeSpan (0, 0, 2));
136 result.AsyncWaitHandle.WaitOne ();
137 q.EndReceive (result);