summaryrefslogtreecommitdiff
path: root/qpid/dotnet/Qpid.Integration.Tests/testcases/CommitRollbackTest.cs
blob: dbb3f70aec9aed286bda4f64b4ba87e26610128b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
using System;
using System.Threading;
using log4net;
using NUnit.Framework;
using Apache.Qpid.Messaging;
using Apache.Qpid.Client.Qms;
using Apache.Qpid.Client;

namespace Apache.Qpid.Integration.Tests.testcases
{
    /// <summary>
    /// CommitRollbackTest
    ///
    /// <p><table id="crc"><caption>CRC Card</caption>
    /// <tr><th> Responsibilities <th> Collaborations
    /// <tr><td> Check that an uncommitted send cannot be received.
    /// <tr><td> Check that a committed send can be received.
    /// <tr><td> Check that a rolled back send cannot be received.
    /// <tr><td> Check that an uncommitted receive can be re-received.
    /// <tr><td> Check that a committed receive cannot be re-received.
    /// <tr><td> Check that a rolled back receive can be re-received.
    /// </table>
    /// </summary>
    [TestFixture, Category("Integration")]
    public class CommitRollbackTest : BaseMessagingTestFixture
    {
        /// <summary>Used for debugging purposes.</summary>
        private static ILog log = LogManager.GetLogger(typeof(CommitRollbackTest));

        /// <summary>Defines the name of the test topic to use with the tests.</summary>
        public const string TEST_ROUTING_KEY = "commitrollbacktestkey";
        
        /// <summary>Used to count test messages received so far.</summary>
        private int messageReceivedCount;

        /// <summary>Used to hold the expected number of messages to receive.</summary>
        private int expectedMessageCount;

        /// <summary>Monitor used to signal succesfull receipt of all test messages.</summary>
        AutoResetEvent finishedEvent;
        
        /// <summary>Flag used to indicate that all messages really were received, and that the test did not just time out. </summary>
        private bool allReceived;
        
        [SetUp]
        public override void Init()
        {
            base.Init();

            // Create one producer and one consumer, p2p, tx, consumer with queue bound to producers routing key.
            SetUpEndPoint(0, true, false, TEST_ROUTING_KEY + testId, AcknowledgeMode.AutoAcknowledge, true, ExchangeNameDefaults.DIRECT, 
                          true, false, null);
            SetUpEndPoint(1, true, true, TEST_ROUTING_KEY + testId, AcknowledgeMode.AutoAcknowledge, true, ExchangeNameDefaults.DIRECT, 
                          true, false, null);
            
            // Clear counts
            messageReceivedCount = 0;
            expectedMessageCount = 0;
            finishedEvent = new AutoResetEvent(false);
            allReceived = false;
        }

        [TearDown]
        public override void Shutdown()
        {
            try
            {
                // Clean up after the test.
                CloseEndPoint(0);
                CloseEndPoint(1);
            } 
            finally 
            {
                base.Shutdown();
            }
        }

        /// <summary> Check that an uncommitted send cannot be received. </summary>
        [Test]
        public void TestUncommittedSendNotReceived() 
        {
            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("A"));
          
            // Try to receive messages.
            ConsumeNMessagesOnly(0, "A", testConsumer[1]);
            testChannel[1].Commit();
        }
        
        /// <summary> Check that a committed send can be received. </summary>
        [Test]
        public void TestCommittedSendReceived() 
        {
            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("B"));
            testChannel[0].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "B", testConsumer[1]);
            testChannel[1].Commit();
        }

        /// <summary> Check that a rolled back send cannot be received. </summary>
        [Test]
        public void TestRolledBackSendNotReceived()
        {
            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("B"));
            testChannel[0].Rollback();

            // Try to receive messages.
            ConsumeNMessagesOnly(0, "B", testConsumer[1]);
            testChannel[1].Commit();
        }

        /// <summary> Check that an uncommitted receive can be re-received. </summary>
        [Test]
        public void TestUncommittedReceiveCanBeRereceived() 
        {
            // Create a third end-point as an alternative delivery route for the message.
            SetUpEndPoint(2, false, true, TEST_ROUTING_KEY + testId, AcknowledgeMode.AutoAcknowledge, true, ExchangeNameDefaults.DIRECT, 
                          true, false, null);

            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("C"));
            testChannel[0].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "C", testConsumer[1]);

            // Close end-point 1 without committing the message, then re-open to consume again.
            CloseEndPoint(1);

            // Check that the message was released from the rolled back end-point an can be received on the alternative one instead.
            ConsumeNMessagesOnly(1, "C", testConsumer[2]);

            CloseEndPoint(2);
        }

        /// <summary> Check that a committed receive cannot be re-received. </summary>
        [Test]
        public void TestCommittedReceiveNotRereceived() 
        {
            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("D"));
            testChannel[0].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "D", testConsumer[1]);
            testChannel[1].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(0, "D", testConsumer[1]);
        }

        /// <summary> Check that a rolled back receive can be re-received. </summary>
        [Test]
        public void TestRolledBackReceiveCanBeRereceived() 
        {
            // Send messages.
            testProducer[0].Send(testChannel[0].CreateTextMessage("E"));
            testChannel[0].Commit();
            
            // Try to receive messages.
            ConsumeNMessagesOnly(1, "E", testConsumer[1]);

            testChannel[1].Rollback();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "E", testConsumer[1]);
            
        }
        
        [Test]
        public void TestReceiveAndSendRollback()
        {
        	// Send messages
        	testProducer[0].Send(testChannel[0].CreateTextMessage("F"));
            testChannel[0].Commit();
            
            // Try to receive messages.
            ConsumeNMessagesOnly(1, "F", testConsumer[1]);
			testProducer[1].Send(testChannel[1].CreateTextMessage("G"));
            testChannel[1].Rollback();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "F", testConsumer[1]);
            
        }
        
        [Test]
        public void TestReceivePrePublished()
        {
        	// Send messages
        	for (int i = 0; i < 10; ++i)
            {
	        	testProducer[0].Send(testChannel[0].CreateTextMessage("G"+i));
	        	testChannel[0].Commit();
        	}
        	
        	for (int i = 0; i < 10; ++i)
            {
        		ConsumeNMessages(1, "G"+i, testConsumer[1]);
        	}
        	testChannel[1].Commit();
        }
        
        [Test]
        public void TestReceivePrePublishedOnMessageHandler()
        {
        	testConsumer[1].OnMessage += new MessageReceivedDelegate(OnMessage);
        	// Send messages
        	for (int i = 0; i < 10; ++i)
            {
	        	testProducer[0].Send(testChannel[0].CreateTextMessage("G"+i));
	        	testChannel[0].Commit();
        	}
        	expectedMessageCount = 10;
        		
            finishedEvent.WaitOne(new TimeSpan(0, 0, 0, 30), false);

            // Check that all messages really were received.
            Assert.IsTrue(allReceived, "All messages were not received, only got: " + messageReceivedCount + " but wanted " + expectedMessageCount);
        
        	testChannel[1].Commit();
        }
        
        /// <summary> Atomically increments the message count on every message, and signals once all messages in the test are received. </summary>
        public void OnMessage(IMessage m)
        {
            int newCount = Interlocked.Increment(ref messageReceivedCount);

            if (newCount >= expectedMessageCount)
            {
                allReceived = true;
                finishedEvent.Set();
            }
        } 
        
    }
}