2009-07-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / Mono.Messaging.RabbitMQ / Test / Mono.Messaging.RabbitMQ / AsyncPeekTest.cs
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.Mono.Messaging.RabbitMQ
40 {
41         [TestFixture]
42         public class AsyncPeekTest {
43
44                 bool eventCalled = false;
45                 
46                 private void HandleMessage (object source, PeekCompletedEventArgs args) {
47                         eventCalled = true;
48                 }
49
50                 [Test]
51                 public void BeginPeek()
52                 {
53                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-1");
54                         Message s = new Message (new BinaryMessageFormatter ());
55                         string body = "foo-" + DateTime.Now.ToString ();
56                         s.Body = body;
57                         q.Send (s);
58                         
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");
65                         
66                         Assert.IsNotNull (q.Receive (), "Message not peeked");
67                 }
68                 
69                 [Test]
70                 public void BeginPeekWithTimeout()
71                 {
72                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-2");
73                         Message s = new Message (new BinaryMessageFormatter ());
74                         string body = "foo-" + DateTime.Now.ToString ();
75                         s.Body = body;
76                         q.Send (s);
77                         
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");
82                         
83                         Assert.IsNotNull (q.Receive (), "Message not peeked");
84                 }
85                 
86                 [Test]
87                 public void BeginPeekWithStateAndTimeout()
88                 {
89                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-3");
90                         Message s = new Message (new BinaryMessageFormatter ());
91                         string body = "foo-" + DateTime.Now.ToString ();
92                         s.Body = body;
93                         q.Send (s);
94                         
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");
100                         
101                         Assert.IsNotNull (q.Receive (), "Message not peeked");
102                 }
103                 
104                 private bool success = false;
105                 
106                 public void TestCallback (IAsyncResult result)
107                 {
108                         success = true;
109                 }
110                 
111                 [Test]
112                 public void BeginPeekWithStateAndTimeoutAndCallback()
113                 {
114                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-4");
115                         Message s = new Message (new BinaryMessageFormatter ());
116                         string body = "foo-" + DateTime.Now.ToString ();
117                         s.Body = body;
118                         q.Send (s);
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");
126                         
127                         Assert.IsNotNull (q.Receive (), "Message not peeked");
128                 }
129                 
130                 [Test]
131                 [ExpectedException (typeof (MessageQueueException))]
132                 public void BeginPeekWithException()
133                 {
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);
138                 }               
139         }
140 }