2008-12-22 Michael Barker <mike@middlesoft.co.uk>
[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                         q.Purge ();
66                 }
67                 
68                 [Test]
69                 public void BeginPeekWithTimeout()
70                 {
71                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-2");
72                         Message s = new Message (new BinaryMessageFormatter ());
73                         string body = "foo-" + DateTime.Now.ToString ();
74                         s.Body = body;
75                         q.Send (s);
76                         
77                         IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2));
78                         result.AsyncWaitHandle.WaitOne ();
79                         Message rMsg = q.EndPeek (result);
80                         Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
81                         
82                         q.Purge ();
83                 }
84                 
85                 [Test]
86                 public void BeginPeekWithStateAndTimeout()
87                 {
88                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-3");
89                         Message s = new Message (new BinaryMessageFormatter ());
90                         string body = "foo-" + DateTime.Now.ToString ();
91                         s.Body = body;
92                         q.Send (s);
93                         
94                         IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2), "foo");
95                         result.AsyncWaitHandle.WaitOne ();
96                         Message rMsg = q.EndPeek (result);
97                         Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
98                         Assert.AreEqual ("foo", result.AsyncState, "State not passed properly");
99                         
100                         q.Purge ();
101                 }
102                 
103                 private bool success = false;
104                 
105                 public void TestCallback (IAsyncResult result)
106                 {
107                         success = true;
108                 }
109                 
110                 [Test]
111                 public void BeginPeekWithStateAndTimeoutAndCallback()
112                 {
113                         MessageQueue q = MQUtil.GetQueue (@".\private$\async-peek-4");
114                         Message s = new Message (new BinaryMessageFormatter ());
115                         string body = "foo-" + DateTime.Now.ToString ();
116                         s.Body = body;
117                         q.Send (s);
118                         AsyncCallback ac = new AsyncCallback (TestCallback);
119                         IAsyncResult result = q.BeginPeek (new TimeSpan (0, 0, 2), "foo", ac);
120                         result.AsyncWaitHandle.WaitOne ();
121                         Message rMsg = q.EndPeek (result);
122                         Assert.AreEqual (body, rMsg.Body, "Async Send Failed, bodies not equal");
123                         Assert.AreEqual ("foo", result.AsyncState, "State not passed properly");
124                         Assert.IsTrue (success, "Callback not run");
125                         
126                         q.Purge ();                     
127                 }
128         }
129 }