summaryrefslogtreecommitdiff
path: root/qpid/dotnet/Qpid.Integration.Tests/interop/TestClient.cs
blob: 0def89c6dafe7cc3f07950233cb632d29320c4cc (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
/*
 *
 * 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.Collections;
using System.Text;
using System.Threading;
using Apache.Qpid.Messaging;
using Apache.Qpid.Client.Qms;
using Apache.Qpid.Client;
using log4net;
using Apache.Qpid.Integration.Tests.interop.TestCases;

namespace Apache.Qpid.Integration.Tests.interop
{
    /// <summary>
    /// Implements a test client as described in the interop testing spec
    /// (http://cwiki.apache.org/confluence/display/qpid/Interop+Testing+Specification). A test client is an agent that
    /// reacts to control message sequences send by the test coordinator.
    ///
    /// <p/><table><caption>Messages Handled by TestClient</caption>
    /// <tr><th> Message               <th> Action
    /// <tr><td> Invite(compulsory)    <td> Reply with Enlist.
    /// <tr><td> Invite(test case)     <td> Reply with Enlist if test case available.
    /// <tr><td> AssignRole(test case) <td> Reply with Accept Role if matches an enlisted test. Keep test parameters.
    /// <tr><td> Start                 <td> Send test messages defined by test parameters. Send report on messages sent.
    /// <tr><td> Status Request        <td> Send report on messages received.
    /// </table>
    ///
    /// <p><table id="crc"><caption>CRC Card</caption>
    /// <tr><th> Responsibilities <th> Collaborations
    /// <tr><td> Handle all incoming control messages. <td> {@link InteropClientTestCase}
    /// <tr><td> Configure and look up test cases by name. <td> {@link InteropClientTestCase}
    /// </table>
    /// </summary>
    public class TestClient
    {
        private static ILog log = LogManager.GetLogger(typeof(TestClient));
      
        /// <summary> Defines the default broker for the tests, localhost, default port. </summary>
        public static string DEFAULT_BROKER_URL = "amqp://guest:guest@clientid/?brokerlist='tcp://localhost:5672'";
      
        /// <summary> Defines the default virtual host to use for the tests, none. </summary>
        public static string DEFAULT_VIRTUAL_HOST = "";
      
        /// <summary> Defines the default identifying name of this test client. </summary>
        public static string DEFAULT_CLIENT_NAME = "dotnet";

        /// <summary> Holds the URL of the broker to run the tests on. </summary>
        public static string brokerUrl;

        /// <summary> Holds the virtual host to run the tests on. If <tt>null</tt>, then the default virtual host is used. </summary>
        public static string virtualHost;

        /// <summary> The clients identifying name to print in test results and to distinguish from other clients. </summary>
        private string clientName;

        /// <summary> Holds all the test cases. </summary>
        private IDictionary testCases = new Hashtable();

        InteropClientTestCase currentTestCase;

        private MessagePublisherBuilder publisherBuilder;

        private IChannel channel;

        /// <summary> Monitor to wait for termination events on. </summary>
        private static object terminationMonitor = new Object();

        /// <summary>
        /// Creates a new interop test client, listenting to the specified broker and virtual host, with the specified
        /// client identifying name.
        /// </summary>
        ///
        /// <param name="brokerUrl"> The url of the broker to connect to. </param>
        /// <param name="virtualHost"> The virtual host to conect to. </param>
        /// <param name="clientName">  The client name to use. </param>
        public TestClient(string brokerUrl, string virtualHost, string clientName)
        {
            log.Info("public TestClient(string brokerUrl = " + brokerUrl + ", string virtualHost = " + virtualHost
                     + ", string clientName = " + clientName + "): called");

            // Retain the connection parameters.
            TestClient.brokerUrl = brokerUrl;
            TestClient.virtualHost = virtualHost;
            this.clientName = clientName;
        }

    
        /// <summary>
        /// The entry point for the interop test coordinator. This client accepts the following command line arguments:
        /// </summary>
        /// 
        /// <p/><table>
        /// <tr><td> -b         <td> The broker URL.       <td> Optional.
        /// <tr><td> -h         <td> The virtual host.     <td> Optional.
        /// <tr><td> -n         <td> The test client name. <td> Optional.
        /// <tr><td> name=value <td> Trailing argument define name/value pairs. Added to system properties. <td> Optional.
        /// </table>
        ///
        /// <param name="args"> The command line arguments. </param>
        public static void Main(string[] args)
        {
            // Extract the command line options (Not exactly Posix but it will do for now...).
            string brokerUrl = DEFAULT_BROKER_URL;
            string virtualHost = DEFAULT_VIRTUAL_HOST;
            string clientName = DEFAULT_CLIENT_NAME;

            foreach (string nextArg in args)
            {
                if (nextArg.StartsWith("-b"))
                {
                    brokerUrl = nextArg.Substring(2);
                }
                else if (nextArg.StartsWith("-h"))
                {
                    virtualHost = nextArg.Substring(2);
                }
                else if (nextArg.StartsWith("-n"))
                {
                    clientName = nextArg.Substring(2);
                }
            }
            
            NDC.Push(clientName);

            // Create a test client and start it running.
            TestClient client = new TestClient(brokerUrl, virtualHost, clientName);

            try
            {
                client.Start();
            }
            catch (Exception e)
            {
                log.Error("The test client was unable to start.", e);
                System.Environment.Exit(1);
            }

            // Wait for a signal on the termination monitor before quitting.
            lock (terminationMonitor)
            {
                Monitor.Wait(terminationMonitor);
            }

            NDC.Pop();
        }

        /// <summary>
        /// Starts the interop test client running. This causes it to start listening for incoming test invites.
        /// </summary>
        private void Start()
        {
            log.Info("private void Start(): called");

            // Use a class path scanner to find all the interop test case implementations.
            ArrayList testCaseClasses = new ArrayList();

            // ClasspathScanner.getMatches(InteropClientTestCase.class, "^TestCase.*", true);
            // Hard code the test classes till the classpath scanner is fixed.
            testCaseClasses.Add(typeof(TestCase1DummyRun));
            testCaseClasses.Add(typeof(TestCase2BasicP2P));
            testCaseClasses.Add(typeof(TestCase3BasicPubSub));
            testCaseClasses.Add(typeof(TestCase4P2PMessageSize));
            testCaseClasses.Add(typeof(TestCase5PubSubMessageSize));

            // Create all the test case implementations and index them by the test names.
            foreach (Type testClass in testCaseClasses)
            {
                InteropClientTestCase testCase = (InteropClientTestCase)Activator.CreateInstance(testClass);
                testCases.Add(testCase.GetName(), testCase);

                log.Info("Found test case: " + testClass);
            }

            // Open a connection to communicate with the coordinator on.
            log.Info("brokerUrl = " + brokerUrl);
            IConnection connection = CreateConnection(brokerUrl, virtualHost);

            channel = connection.CreateChannel(false, AcknowledgeMode.AutoAcknowledge);

            // Set this up to listen for control messages.
            string responseQueueName = channel.GenerateUniqueName();
            channel.DeclareQueue(responseQueueName, false, true, true);

            channel.Bind(responseQueueName, ExchangeNameDefaults.TOPIC, "iop.control." + clientName);
            channel.Bind(responseQueueName, ExchangeNameDefaults.TOPIC, "iop.control");

            IMessageConsumer consumer = channel.CreateConsumerBuilder(responseQueueName)
                .Create();
            consumer.OnMessage += new MessageReceivedDelegate(OnMessage);

            // Create a publisher to send replies with.
            publisherBuilder = channel.CreatePublisherBuilder()
                .WithExchangeName(ExchangeNameDefaults.DIRECT);
                

            // Start listening for incoming control messages.
            connection.Start();
            Console.WriteLine("Test client " + clientName + " ready to receive test control messages...");
        }

        /// <summary>
        /// Establishes an AMQ connection. This is a simple convenience method for code that does not anticipate handling connection failures. 
        /// All exceptions that indicate that the connection has failed, are allowed to fall through.
        /// </summary>
        ///
        /// <param name="brokerUrl">   The broker url to connect to, <tt>null</tt> to use the default from the properties. </param>
        /// <param name="virtualHost"> The virtual host to connectio to, <tt>null</tt> to use the default. </param>
        ///
        /// <returns> A JMS conneciton. </returns>
        public static IConnection CreateConnection(string brokerUrl, string virtualHost)
        {
            log.Info("public static Connection createConnection(string brokerUrl = " + brokerUrl + ", string virtualHost = " 
                     + virtualHost + "): called");

            // Create a connection to the broker.
            IConnectionInfo connectionInfo = QpidConnectionInfo.FromUrl(brokerUrl);
            connectionInfo.VirtualHost = virtualHost;
            IConnection connection = new AMQConnection(connectionInfo);

            return connection;
        }
        
        /// <summary>
        /// Handles all incoming control messages.
        /// </summary>
        ///
        /// <param name="message"> The incoming message. </param>
        public void OnMessage(IMessage message)
        {
            log.Info("public void OnMessage(IMessage message = " + message + "): called");

            try
            {
                string controlType = message.Headers.GetString("CONTROL_TYPE");
                string testName = message.Headers.GetString("TEST_NAME");

                // Check if the message is a test invite.
                if ("INVITE" == controlType)
                {
                    string testCaseName = message.Headers.GetString("TEST_NAME");

                    // Flag used to indicate that an enlist should be sent. Only enlist to compulsory invites or invites
                    // for which test cases exist.
                    bool enlist = false;

                    if (testCaseName != null)
                    {
                        log.Info("Got an invite to test: " + testCaseName);

                        // Check if the requested test case is available.
                        InteropClientTestCase testCase = (InteropClientTestCase)testCases[testCaseName];
    
                        if (testCase != null)
                        {
                            // Make the requested test case the current test case.
                            currentTestCase = testCase;
                            enlist = true;
                        }
                    }
                    else
                    {
                        log.Info("Got a compulsory invite.");

                        enlist = true;
                    }

                    log.Info("enlist = " + enlist);

                    if (enlist)
                    {
                        // Reply with the client name in an Enlist message.
                        IMessage enlistMessage = channel.CreateMessage();
                        enlistMessage.Headers.SetString("CONTROL_TYPE", "ENLIST");
                        enlistMessage.Headers.SetString("CLIENT_NAME", clientName);
                        enlistMessage.Headers.SetString("CLIENT_PRIVATE_CONTROL_KEY", "iop.control." + clientName);
                        enlistMessage.CorrelationId = message.CorrelationId;

                        Send(enlistMessage, message.ReplyToRoutingKey);
                    }                    
                }
                else if ("ASSIGN_ROLE" == controlType)
                {
                    // Assign the role to the current test case.
                    string roleName = message.Headers.GetString("ROLE");
    
                    log.Info("Got a role assignment to role: " + roleName);
    
                    Roles role;

                    if (roleName == "SENDER")
                    {
                        role = Roles.SENDER;
                    }
                    else
                    {
                        role = Roles.RECEIVER;
                    }
    
                    currentTestCase.AssignRole(role, message);

                    // Reply by accepting the role in an Accept Role message.
                    IMessage acceptRoleMessage = channel.CreateMessage();
                    acceptRoleMessage.Headers.SetString("CONTROL_TYPE", "ACCEPT_ROLE");
                    acceptRoleMessage.CorrelationId = message.CorrelationId;

                    Send(acceptRoleMessage, message.ReplyToRoutingKey);
                }
                else if ("START" == controlType || "STATUS_REQUEST" == controlType)
                {
                    if ("START" == controlType)
                    {
                        log.Info("Got a start notification.");

                        // Start the current test case.
                        currentTestCase.Start();
                    }
                    else
                    {
                        log.Info("Got a status request.");
                    }

                    // Generate the report from the test case and reply with it as a Report message.
                    IMessage reportMessage = currentTestCase.GetReport(channel);
                    reportMessage.Headers.SetString("CONTROL_TYPE", "REPORT");
                    reportMessage.CorrelationId = message.CorrelationId;

                    Send(reportMessage, message.ReplyToRoutingKey);
                }
                else if ("TERMINATE" == controlType)
                {
                    Console.WriteLine("Received termination instruction from coordinator.");

                    // Is a cleaner shutdown needed?
                    System.Environment.Exit(1);
                }
                else
                {
                    // Log a warning about this but otherwise ignore it.
                    log.Warn("Got an unknown control message, controlType = " + controlType + ", message = " + message);
                }
            }
            catch (QpidException e)
            {
                // Log a warning about this, but otherwise ignore it.
                log.Warn("A QpidException occurred whilst handling a message.");
                log.Info("Got QpidException whilst handling message: " + message, e);
            }
        }

        /// <summary>
        /// Send the specified message using the specified routing key on the direct exchange.
        /// </summary>
        /// 
        /// <param name="message">    The message to send.</param>
        /// <param name="routingKey"> The routing key to send the message with.</param>
        public void Send(IMessage message, string routingKey)
        {
            IMessagePublisher publisher = publisherBuilder.WithRoutingKey(routingKey).Create();
            publisher.Send(message);
        }
    }
}