summaryrefslogtreecommitdiff
path: root/dotnet/Qpid.Integration.Tests/testcases/CommitRollbackTest.cs
blob: aab279285ef4893e6ab8a5c889eb1571118dc516 (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
/*
 *
 * 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";

        [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, false, true, TEST_ROUTING_KEY + testId, AcknowledgeMode.AutoAcknowledge, true, ExchangeNameDefaults.DIRECT, 
                          true, false, null);
        }

        [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("A"));
            testChannel[0].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "A", 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("A"));
            testChannel[0].Rollback();

            // Try to receive messages.
            ConsumeNMessagesOnly(0, "A", 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("A"));
            testChannel[0].Commit();

            // Try to receive messages.
            ConsumeNMessagesOnly(1, "A", 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, "A", 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("A"));
            testChannel[0].Commit();

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

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

        /// <summary> Check that a rolled back receive can be re-received. </summary>
        [Test]
        public void TestRolledBackReceiveCanBeRereceived() 
        {
            // 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("A"));
            testChannel[0].Commit();
            
            // Try to receive messages.
            ConsumeNMessagesOnly(1, "A", testConsumer[1]);

            testChannel[1].Rollback();

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

            CloseEndPoint(2);
        }
    }
}