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.Mono.Messaging.RabbitMQ
42 public class AsyncPeekTest {
44 bool eventCalled = false;
46 private void HandleMessage (object source, PeekCompletedEventArgs args) {
51 public void BeginPeek()
53 MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-1");
54 Message s = new Message (new BinaryMessageFormatter ());
55 string body = "foo-" + DateTime.Now.ToString ();
59 q.PeekCompleted += new PeekCompletedEventHandler (HandleMessage);
60 IAsyncResult result = q.BeginPeek ();
61 result.AsyncWaitHandle.WaitOne ();
62 Message rMsg = q.EndPeek (result);
63 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
64 Assert.IsTrue (eventCalled, "Handle Message not called");
66 Assert.IsNotNull (q.Receive (), "Message not peeked");
70 public void BeginPeekWithTimeout()
72 MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-2");
73 Message s = new Message (new BinaryMessageFormatter ());
74 string body = "foo-" + DateTime.Now.ToString ();
78 IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2));
79 result.AsyncWaitHandle.WaitOne ();
80 Message rMsg = q.EndPeek (result);
81 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
83 Assert.IsNotNull (q.Receive (), "Message not peeked");
87 public void BeginPeekWithStateAndTimeout()
89 MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-3");
90 Message s = new Message (new BinaryMessageFormatter ());
91 string body = "foo-" + DateTime.Now.ToString ();
95 IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2), "foo");
96 result.AsyncWaitHandle.WaitOne ();
97 Message rMsg = q.EndPeek (result);
98 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
99 Assert.AreEqual ("foo", result.AsyncState, "State not passed properly");
101 Assert.IsNotNull (q.Receive (), "Message not peeked");
104 private bool success = false;
106 public void TestCallback (IAsyncResult result)
112 public void BeginPeekWithStateAndTimeoutAndCallback()
114 MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-4");
115 Message s = new Message (new BinaryMessageFormatter ());
116 string body = "foo-" + DateTime.Now.ToString ();
119 AsyncCallback ac = new AsyncCallback (TestCallback);
120 IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2), "foo", ac);
121 result.AsyncWaitHandle.WaitOne ();
122 Message rMsg = q.EndPeek (result);
123 Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
124 Assert.AreEqual ("foo", result.AsyncState, "State not passed properly");
125 Assert.IsTrue (success, "Callback not run");
127 Assert.IsNotNull (q.Receive (), "Message not peeked");
131 [ExpectedException (typeof (MessageQueueException))]
132 public void BeginPeekWithException()
134 MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-5");
135 IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2));
136 result.AsyncWaitHandle.WaitOne ();
137 Message rMsg = q.EndPeek (result);