summaryrefslogtreecommitdiff
path: root/qpid/dotnet/Qpid.Integration.Tests/interactive/FailoverTest.cs
blob: 142ac40b27a01606db8b49887f7804e3f1dbcb65 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 *
 * 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.Runtime.InteropServices;
using System.Threading;
using log4net;
using NUnit.Framework;
using Apache.Qpid.Client.Qms;
using Apache.Qpid.Client;
using Apache.Qpid.Messaging;

namespace Apache.Qpid.Integration.Tests.interactive
{
    [TestFixture, Category("Interactive")]
    public class FailoverTest : IConnectionListener
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(FailoverTest));

        /// <summary>Specifies the number of times to run the test cycle.</summary>
        const int NUM_MESSAGES = 10;

        /// <summary>Determines how many messages to send within each commit.</summary>
        const int COMMIT_BATCH_SIZE = 1;

        /// <summary>Specifies the duration of the pause to place between each message sent in the test.</summary>
        //const int SLEEP_MILLIS = 1;

        /// <summary>Specified the maximum time in milliseconds to wait for the test to complete.</summary>
        const int TIMEOUT = 10000;

        /// <summary>Defines the number of test messages to send, before prompting the user to fail a broker.</summary>
        const int FAIL_POINT = 5;

        /// <summary>Specified the ack mode to use for the test.</summary>
        AcknowledgeMode _acknowledgeMode = AcknowledgeMode.AutoAcknowledge;

        /// <summary>Determines whether this test runs transactionally or not. </summary>
        bool transacted = false;

        /// <summary>Holds the connection to run the test over.</summary>
        AMQConnection _connection;

        /// <summary>Holds the channel for the test message publisher. </summary>
        IChannel publishingChannel;

        /// <summary>Holds the test message publisher. </summary>
        IMessagePublisher publisher;

        /// <summary>Used to keep count of the number of messages sent. </summary>
        int messagesSent;

        /// <summary>Used to keep count of the number of messages received. </summary>
        int messagesReceived;

        /// <summary>Used to wait for test completion on. </summary>
        private static object testComplete = new Object();

        /// <summary>Used to wait for failover completion on. </summary>
	private static object failoverComplete = new Object();
	
        bool failedOver=false;

        /// <summary>Used to record the extra message count (1) if the message sent right after failover actually made it to the new broker.</summary>
        int _extraMessage = 0;
	
        /// <summary>
        /// Creates the test connection with a fail-over set up, and a producer/consumer pair on that connection.
        /// </summary>
        /// [SetUp]
        public void Init(IConnectionInfo connectionInfo)
        {
	    //log4net.Config.BasicConfigurator.Configure();
            // Reset all counts.
            messagesSent = 0;
            messagesReceived = 0;
            failedOver=false;
            _extraMessage = 0;

	    PromptAndWait("Ensure both brokers are running, then press Enter");	    
	    
            // Create a connection for the test.
            _connection = new AMQConnection(connectionInfo);
            _connection.ConnectionListener = this;

            // Create a consumer to receive the test messages.
            IChannel receivingChannel = _connection.CreateChannel(false, _acknowledgeMode);

            string queueName = receivingChannel.GenerateUniqueName();
            receivingChannel.DeclareQueue(queueName, false, true, true);
            receivingChannel.Bind(queueName, "amq.direct", queueName);

            IMessageConsumer consumer = receivingChannel.CreateConsumerBuilder(queueName)
                .WithPrefetchLow(30)
                .WithPrefetchHigh(60).Create();

            consumer.OnMessage = new MessageReceivedDelegate(OnMessage);
            _connection.Start();

            // Create a publisher to send the test messages.
            publishingChannel = _connection.CreateChannel(transacted, AcknowledgeMode.NoAcknowledge);
            publisher = publishingChannel.CreatePublisherBuilder()
                .WithRoutingKey(queueName)
                .Create();

            _log.Debug("connection = " + _connection);
            _log.Debug("connectionInfo = " + connectionInfo);
            _log.Debug("connection.AsUrl = " + _connection.toURL());
            _log.Debug("AcknowledgeMode is " + _acknowledgeMode);
        }

        /// <summary>
        /// Clean up the test connection.
        /// </summary>
        [TearDown]
        public virtual void Shutdown()
        {
 	    if (!failedOver)
	    {
                 Assert.Fail("The failover callback never occured.");
            }

            Console.WriteLine("Test done shutting down");
	    Thread.Sleep(2000);
            _connection.Close();
        }

        /// <summary>
        /// Runs a failover test, building up the connection information from its component parts. In particular the brokers
        /// to fail between are seperately added into the connection info.
        /// </summary>
        /*[Test]
        public void TestWithBasicInfo()
        {
            _log.Debug("public void TestWithBasicInfo(): called");

            // Manually create the connection parameters.
            QpidConnectionInfo connectionInfo = new QpidConnectionInfo();
            connectionInfo.AddBrokerInfo(new AmqBrokerInfo("amqp", "localhost", 5672, false));
            connectionInfo.AddBrokerInfo(new AmqBrokerInfo("amqp", "localhost", 5673, false));

            Init(connectionInfo);
            DoFailoverTest();
        }*/

        /// <summary>
        /// Runs a failover test, with the failover configuration specified in the Qpid connection URL format.
        /// </summary>
        [Test]
        public void TestWithUrl()
        {
            _log.Debug("public void runTestWithUrl(): called");

            // Parse the connection parameters from a URL.
            String clientId = "failover" + DateTime.Now.Ticks;
            string defaultUrl = "amqp://guest:guest@" + clientId + "/test" +
                "?brokerlist='tcp://localhost:9672;tcp://localhost:9673'&failover='roundrobin'";            
            IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(defaultUrl);
            
            Init(connectionInfo);
            DoFailoverTest(0);
        }

        /// <summary>
        /// Send the test messages, prompting at the fail point for the user to cause a broker failure. The test checks that all messages sent
        /// are received within the test time limit.
        /// </summary>
        ///
        /// <param name="connectionInfo">The connection parameters, specifying the brokers to fail between.</param>
        void DoFailoverTest(int delay)
        {
            _log.Debug("void DoFailoverTest(IConnectionInfo connectionInfo): called");

            // Wait for all of the test messages to be received, checking that this occurs within the test time limit.
	    bool withinTimeout = false;

            for (int i = 1; i <= NUM_MESSAGES; ++i)
            {
		SendMessage();

		// Prompt the user to cause a failure if at the fail point.
		if (i == FAIL_POINT)
		{
		    for( int min = delay ; min > 0 ; min--)
		    {
		       Console.WriteLine("Waiting for "+min+" minutes to test connection time bug.");
		       Thread.Sleep(60*1000);
		    }

		    PromptAndWait("Cause a broker failure now, then press return.");
		    Console.WriteLine("NOTE: ensure that the delay between killing the broker and continuing here is less than 20 second");
		    
		    Console.WriteLine("Sending a message to ensure send right after works");

		    SendMessage();

		    Console.WriteLine("Waiting for fail-over to complete before continuing...");


		    lock(failoverComplete)
		    {
			if (!failedOver)
			{
			    withinTimeout = Monitor.Wait(failoverComplete, TIMEOUT);
			}
			else
			{
			    withinTimeout=true;
			}
		    }

		    if (!withinTimeout)
		    {
			PromptAndWait("Failover has not yet occured. Press enter to give up waiting.");
		    }
		}
	    }

            lock(testComplete)
            {
                withinTimeout = Monitor.Wait(testComplete, TIMEOUT);
            }            

            if (!withinTimeout)
            {
                Assert.Fail("Test timed out, before all messages received.");
            }

            _log.Debug("void DoFailoverTest(IConnectionInfo connectionInfo): exiting");
        }

	[Test]
        public void Test5MinuteWait()
	{
	    String clientId = "failover" + DateTime.Now.Ticks;

	    QpidConnectionInfo connectionInfo = new QpidConnectionInfo();
	    connectionInfo.Username = "guest";
	    connectionInfo.Password = "guest";
	    connectionInfo.ClientName = clientId;
	    connectionInfo.VirtualHost = "/test";
	    connectionInfo.AddBrokerInfo(new AmqBrokerInfo("amqp", "localhost", 9672, false));
	    connectionInfo.AddBrokerInfo(new AmqBrokerInfo("amqp", "localhost", 9673, false));
	    
	    Init(connectionInfo);
	    DoFailoverTest(5);
	}

	void SendMessage()
	{
	    ITextMessage msg = publishingChannel.CreateTextMessage("message=" + messagesSent);

	    publisher.Send(msg);
	    messagesSent++;

	    if (transacted)
	    {
		publishingChannel.Commit();
	    }
	    
	    Console.WriteLine("messagesSent = " + messagesSent);
	}
	
        /// <summary>
        /// Receives all of the test messages.
        /// </summary>
        ///
        /// <param name="message">The newly arrived test message.</param>
        public void OnMessage(IMessage message)
        {
            try
            {
                if (_acknowledgeMode == AcknowledgeMode.ClientAcknowledge)
                {
                    message.Acknowledge();
                }

                messagesReceived++;

                _log.Debug("messagesReceived = " + messagesReceived);

                // Check if all of the messages in the test have been received, in which case notify the message producer that the test has 
                // succesfully completed.
                if (messagesReceived == NUM_MESSAGES + _extraMessage)
                {
                    lock (testComplete)
                    {
			failedOver = true;
                        Monitor.Pulse(testComplete);
                    }
                }
            }
            catch (QpidException e)
            {
                _log.Fatal("Exception received. About to stop.", e);
                Stop();
            }
        }

        /// <summary>Prompts the user on stdout and waits for a reply on stdin, using the specified prompt message.</summary>
        ///
        /// <param name="message">The message to prompt the user with.</param>
        private void PromptAndWait(string message)
        {
            Console.WriteLine("\n" + message);
            Console.ReadLine();
        }

        // <summary>Closes the test connection.</summary>
        private void Stop()
        {
            _log.Debug("Stopping...");
            try
            {
                _connection.Close();
            }
            catch (QpidException e)
            {
                _log.Debug("Failed to shutdown: ", e);
            }
        }

        /// <summary>
        /// Called when bytes have been transmitted to the server
        /// </summary>
        ///
        /// <param>count the number of bytes sent in total since the connection was opened</param>     
        public void BytesSent(long count) {}

        /// <summary>
        /// Called when some bytes have been received on a connection
        /// </summary>
        ///
        /// <param>count the number of bytes received in total since the connection was opened</param>         
        public void BytesReceived(long count) {}

        /// <summary>
        /// Called after the infrastructure has detected that failover is required but before attempting failover.
        /// </summary>
        ///
        /// <param>redirect true if the broker requested redirect. false if failover is occurring due to a connection error.</param>
        ///
        /// <return>true to continue failing over, false to veto failover and raise a connection exception</return>         
        public bool PreFailover(bool redirect) 
        {
            _log.Debug("public bool PreFailover(bool redirect): called");
            return true; 
        }

        /// <summary>
        /// Called after connection has been made to another broker after failover has been started but before
        /// any resubscription has been done.
        /// </summary>
        ///
        /// <return> true to continue with resubscription, false to prevent automatic resubscription. This is useful in
        /// cases where the application wants to handle resubscription. Note that in the latter case all sessions, producers
        /// and consumers are invalidated.
        /// </return>
        public bool PreResubscribe() 
        {
            _log.Debug("public bool PreResubscribe(): called");
            return true; 
        }

        /// <summary>
        /// Called once failover has completed successfully. This is called irrespective of whether the client has
        /// vetoed automatic resubscription.
        /// </summary>
        public void FailoverComplete() 
        {
	    failedOver = true;
            _log.Debug("public void FailoverComplete(): called");
	    Console.WriteLine("public void FailoverComplete(): called");
	    lock (failoverComplete) 
	    {
	      Monitor.Pulse(failoverComplete);
	    }
        }
    }
}