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 
34 using NUnit.Framework;
35 
36 namespace MonoTests.System.Messaging
37 {
38 	[TestFixture]
39 	[Ignore]
40 	public class MessageEnumeratorTest {
41 
42 		string qName;
43 
44 		[SetUp]
SetUp()45 		public void SetUp()
46 		{
47 			qName = MQUtil.CreateQueueName ();
48 		}
49 
SendMessage(string s)50 		private void SendMessage (string s) {
51 			MessageQueue mq = MQUtil.GetQueue (qName);
52 			using (mq) {
53 				Message m = new Message (s, new BinaryMessageFormatter ());
54 				m.CorrelationId = Guid.NewGuid () + "\\0";
55 				mq.Send (m);
56 			}
57 		}
58 
59 		[Test]
GetMessagesInOrder()60 		public void GetMessagesInOrder ()
61 		{
62 			SendMessage ("message 1");
63 			SendMessage ("message 2");
64 			SendMessage ("message 3");
65 			SendMessage ("message 4");
66 
67 			MessageQueue mq0 = MQUtil.GetQueue (qName);
68 			MessageEnumerator me0 = mq0.GetMessageEnumerator ();
69 
70 			me0.MoveNext ();
71 			Console.WriteLine("Message0 {0}", me0.Current.Body);
72 			me0.MoveNext ();
73 			Console.WriteLine("Message0 {0}", me0.Current.Body);
74 			me0.MoveNext ();
75 			Console.WriteLine("Message0 {0}", me0.Current.Body);
76 			me0.MoveNext ();
77 			Console.WriteLine("Message0 {0}", me0.Current.Body);
78 
79 			me0.Dispose ();
80 			mq0.Dispose ();
81 
82 			MessageQueue mq1 = MQUtil.GetQueue (qName);
83 			MessageEnumerator me1 = mq1.GetMessageEnumerator ();
84 
85 			me1.MoveNext ();
86 			Console.WriteLine("Message1 {0}", me1.Current.Body);
87 			me1.MoveNext ();
88 			Console.WriteLine("Message1 {0}", me1.Current.Body);
89 			me1.MoveNext ();
90 			Console.WriteLine("Message1 {0}", me1.Current.Body);
91 			me1.MoveNext ();
92 			Console.WriteLine("Message1 {0}", me1.Current.Body);
93 
94 			Message m1 = me1.Current;
95 			m1.Formatter = new BinaryMessageFormatter ();
96 			Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
97 
98 			mq1.Purge ();
99 			MessageQueue.Delete (qName);
100 		}
101 
102 		[Test]
RemoveMessage()103 		public void RemoveMessage ()
104 		{
105 			SendMessage ("message 1");
106 			SendMessage ("message 2");
107 			SendMessage ("message 3");
108 			SendMessage ("message 4");
109 
110 			MessageQueue mq0 = MQUtil.GetQueue (qName);
111 			MessageEnumerator me0 = mq0.GetMessageEnumerator ();
112 
113 			me0.MoveNext ();
114 			me0.MoveNext ();
115 			me0.MoveNext ();
116 
117 			Message m0 = me0.RemoveCurrent ();
118 
119 			me0.MoveNext ();
120 
121 			me0.Dispose ();
122 			mq0.Dispose ();
123 
124 			MessageQueue mq1 = MQUtil.GetQueue (qName);
125 			MessageEnumerator me1 = mq1.GetMessageEnumerator ();
126 
127 			me1.MoveNext();
128 			me1.MoveNext();
129 			me1.MoveNext();
130 
131 			Message m1 = me1.Current;
132 			m1.Formatter = new BinaryMessageFormatter ();
133 			Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
134 
135 			mq1.Purge ();
136 			MessageQueue.Delete (qName);
137 		}
138 
139 		[Test]
RemoveMessageWithTimeout()140 		public void RemoveMessageWithTimeout ()
141 		{
142 			SendMessage ("message 1");
143 			SendMessage ("message 2");
144 			SendMessage ("message 3");
145 			SendMessage ("message 4");
146 
147 			MessageQueue mq0 = MQUtil.GetQueue (qName);
148 			MessageEnumerator me0 = mq0.GetMessageEnumerator ();
149 
150 			TimeSpan ts = new TimeSpan (0, 0, 2);
151 
152 			me0.MoveNext (ts);
153 			me0.MoveNext (ts);
154 			me0.MoveNext (ts);
155 
156 			Message m0 = me0.RemoveCurrent (ts);
157 
158 			me0.MoveNext (ts);
159 
160 			me0.Dispose ();
161 			mq0.Dispose ();
162 
163 			MessageQueue mq1 = MQUtil.GetQueue (qName);
164 			MessageEnumerator me1 = mq1.GetMessageEnumerator ();
165 
166 			me1.MoveNext (ts);
167 			me1.MoveNext (ts);
168 			me1.MoveNext (ts);
169 
170 			Message m1 = me1.Current;
171 			m1.Formatter = new BinaryMessageFormatter ();
172 			Assert.AreEqual ("message 4", (String) m1.Body, "body incorrect");
173 
174 			mq1.Purge ();
175 			MessageQueue.Delete (qName);
176 		}
177 
178 		//[Test]
179 		// Not supported with AMQP
RemoveMessageWithTx()180 		public void RemoveMessageWithTx ()
181 		{
182 			MessageQueue q = MQUtil.GetQueue (qName);
183 
184 			q.Formatter = new BinaryMessageFormatter ();
185 			q.Send ("foo1");
186 			q.Send ("foo2");
187 
188 			MessageEnumerator me1 = q.GetMessageEnumerator ();
189 			MessageQueueTransaction tx = new MessageQueueTransaction ();
190 			me1.MoveNext ();
191 			Message m1 = me1.Current;
192 			me1.RemoveCurrent (tx);
193 			tx.Commit ();
194 			me1.Close ();
195 
196 			MessageEnumerator me2 = q.GetMessageEnumerator ();
197 			Assert.IsTrue (me1.MoveNext ());
198 			me2.RemoveCurrent ();
199 			Assert.IsFalse (me2.MoveNext ());
200 		}
201 	}
202 }
203