summaryrefslogtreecommitdiff
path: root/qpid/java/perftests/src/test/java/org/apache/qpid/systest/disttest/clientonly/DistributedClientTest.java
blob: 5b5a60ac43b11d29ae3cc66732b9cdbd93865c6d (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
/*
 * 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.
 *
 */
package org.apache.qpid.systest.disttest.clientonly;

import static org.apache.qpid.disttest.client.ClientState.READY;
import static org.apache.qpid.disttest.client.ClientState.RUNNING_TEST;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.qpid.client.AMQSession;
import org.apache.qpid.disttest.client.Client;
import org.apache.qpid.disttest.client.ClientState;
import org.apache.qpid.disttest.jms.ClientJmsDelegate;
import org.apache.qpid.disttest.jms.JmsMessageAdaptor;
import org.apache.qpid.disttest.message.Command;
import org.apache.qpid.disttest.message.CommandType;
import org.apache.qpid.disttest.message.CreateConnectionCommand;
import org.apache.qpid.disttest.message.CreateConsumerCommand;
import org.apache.qpid.disttest.message.CreateProducerCommand;
import org.apache.qpid.disttest.message.CreateSessionCommand;
import org.apache.qpid.disttest.message.ParticipantResult;
import org.apache.qpid.disttest.message.RegisterClientCommand;
import org.apache.qpid.disttest.message.Response;
import org.apache.qpid.disttest.message.StartTestCommand;
import org.apache.qpid.disttest.message.TearDownTestCommand;
import org.apache.qpid.systest.disttest.DistributedTestSystemTestBase;

public class DistributedClientTest extends DistributedTestSystemTestBase
{
    private static final String TEST_CONSUMER = "newTestConsumer";
    private static final String TEST_DESTINATION = "newDestination";
    private static final String TEST_PRODUCER_NAME = "newTestProducer";
    private static final String TEST_SESSION_NAME = "newTestSession";
    private static final String TEST_CONNECTION_NAME = "newTestConnection";

    private Session _session = null;
    private MessageProducer _clientQueueProducer;
    private Client _client;
    private ControllerQueue _controllerQueue;
    protected ClientJmsDelegate _clientJmsDelegate;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

        _session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        _controllerQueue = new ControllerQueue(_connection, _context);

        _clientJmsDelegate = new ClientJmsDelegate(_context);
        _client = new Client(_clientJmsDelegate);
        _client.start();

        final RegisterClientCommand registrationCommand = _controllerQueue.getNext();
        createClientQueueProducer(registrationCommand);

        createTestConnection(TEST_CONNECTION_NAME);
        createTestSession(TEST_CONNECTION_NAME, TEST_SESSION_NAME);

        assertEquals("Expected no test producers at start of test", 0, _clientJmsDelegate.getNoOfTestProducers());
        assertEquals("Expected no test consumers at start of test", 0, _clientJmsDelegate.getNoOfTestConsumers());
    }

    @Override
    protected void tearDown() throws Exception
    {
        try
        {
            _controllerQueue.close();
            if (_session != null)
            {
                _session.close();
            }
        }
        finally
        {
            super.tearDown();
        }
    }

    public void testClientCanCreateTestProducer() throws Exception
    {
        assertEquals("Should initially have zero producers", 0, _clientJmsDelegate.getNoOfTestProducers());

        createTestProducer(TEST_SESSION_NAME, TEST_PRODUCER_NAME, TEST_DESTINATION);

        assertEquals("Should now have one test producer", 1, _clientJmsDelegate.getNoOfTestProducers());
    }

    public void testClientCanCreateTestConsumer() throws Exception
    {
        assertEquals("Should initially have no test consumers", 0, _clientJmsDelegate.getNoOfTestConsumers());

        createTestConsumer(TEST_SESSION_NAME, TEST_CONSUMER, TEST_DESTINATION);

        assertEquals("Should now have one test consumer", 1, _clientJmsDelegate.getNoOfTestConsumers());
    }

    public void testClientFailsToCreateSessionUsingInvalidConnection() throws Exception
    {
        int initialNoOfTestSessions = _clientJmsDelegate.getNoOfTestSessions();

        createTestSession("nonExistentConnection", TEST_SESSION_NAME, false /* shouldSucceed */);

        assertEquals("Number of test sessions should not have changed", initialNoOfTestSessions, _clientJmsDelegate.getNoOfTestSessions());
    }

    public void testClientFailsToCreateProducerUsingInvalidSession() throws Exception
    {
        int initialNoOfTestProducers = _clientJmsDelegate.getNoOfTestProducers();

        createTestProducer("invalidSessionName", TEST_PRODUCER_NAME, TEST_DESTINATION, false /* shouldSucceed */);

        assertEquals("Number of test producers should not have changed", initialNoOfTestProducers, _clientJmsDelegate.getNoOfTestProducers());
    }

    public void testClientFailsToCreateConsumerUsingInvalidSession() throws Exception
    {
        int initialNoOfTestConsumers = _clientJmsDelegate.getNoOfTestConsumers();

        createTestConsumer("invalidSessionName", TEST_CONSUMER, TEST_DESTINATION, false /* shouldSucceed */);

        assertEquals("Number of test consumers should not have changed", initialNoOfTestConsumers, _clientJmsDelegate.getNoOfTestConsumers());
    }

    public void testClientCanStartPerformingTests() throws Exception
    {
        createTestProducer(TEST_SESSION_NAME, TEST_PRODUCER_NAME, TEST_DESTINATION);

        sendCommandToClient(new StartTestCommand());

        validateStartTestResponseAndParticipantResults(CommandType.PRODUCER_PARTICIPANT_RESULT);

        assertState(_client, RUNNING_TEST);
    }

    public void testParticipantsSendResults() throws Exception
    {
        createTestProducer(TEST_SESSION_NAME, TEST_PRODUCER_NAME, TEST_DESTINATION);

        sendCommandToClient(new StartTestCommand());

        validateStartTestResponseAndParticipantResults(CommandType.PRODUCER_PARTICIPANT_RESULT);
    }

    /**
     * Need to validate both of these responses together because their order is non-deterministic
     * @param expectedParticipantResultCommandType TODO
     */
    private void validateStartTestResponseAndParticipantResults(CommandType expectedParticipantResultCommandType) throws JMSException
    {
        Map<CommandType, Command> responses = new HashMap<CommandType, Command>();
        _controllerQueue.addNextResponse(responses);
        _controllerQueue.addNextResponse(responses);

        ParticipantResult results = (ParticipantResult) responses.get(expectedParticipantResultCommandType);
        validateResponse(null, results, true);

        Response startTestResponse = (Response) responses.get(CommandType.RESPONSE);
        validateResponse(CommandType.START_TEST, startTestResponse, true);
    }

    public void testClientCannotStartPerformingTestsInNonReadyState() throws Exception
    {
        assertState(_client, READY);
        sendCommandAndValidateResponse(new StartTestCommand(), true);
        assertState(_client, RUNNING_TEST);

        // Send another start test command
        sendCommandAndValidateResponse(new StartTestCommand(), false /*should reject duplicate start command*/);
        assertState(_client, RUNNING_TEST);
    }

    public void testNonRunningClientIsUnaffectedByStopTestCommand() throws Exception
    {
        assertState(_client, READY);

        sendCommandAndValidateResponse(new TearDownTestCommand(), false);

        assertState(_client, READY);
    }

    private void sendCommandToClient(final Command command) throws Exception
    {
        final Message message = JmsMessageAdaptor.commandToMessage(_session, command);
        _clientQueueProducer.send(message);
        ((AMQSession<?, ?>)_session).sync();
    }

    private void sendCommandAndValidateResponse(final Command command, boolean shouldSucceed) throws Exception
    {
        sendCommandToClient(command);
        Response response = _controllerQueue.getNext();
        validateResponse(command.getType(), response, shouldSucceed);
    }

    private void sendCommandAndValidateResponse(final Command command) throws Exception
    {
        sendCommandAndValidateResponse(command, true);
    }

    private void createTestConnection(String connectionName) throws Exception
    {
        int initialNumberOfConnections = _clientJmsDelegate.getNoOfTestConnections();

        final CreateConnectionCommand createConnectionCommand = new CreateConnectionCommand();
        createConnectionCommand.setConnectionName(connectionName);
        createConnectionCommand.setConnectionFactoryName("connectionfactory");

        sendCommandAndValidateResponse(createConnectionCommand);

        int expectedNumberOfConnections = initialNumberOfConnections + 1;

        assertEquals("unexpected number of test connections", expectedNumberOfConnections, _clientJmsDelegate.getNoOfTestConnections());
    }

    private void createTestSession(String connectionName, String sessionName, boolean shouldSucceed) throws Exception
    {
        int initialNumberOfSessions = _clientJmsDelegate.getNoOfTestSessions();

        final CreateSessionCommand createSessionCommand = new CreateSessionCommand();
        createSessionCommand.setConnectionName(connectionName);
        createSessionCommand.setSessionName(sessionName);
        createSessionCommand.setAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

        sendCommandAndValidateResponse(createSessionCommand, shouldSucceed);

        int expectedNumberOfSessions = initialNumberOfSessions + (shouldSucceed ? 1 : 0);

        assertEquals("unexpected number of test sessions", expectedNumberOfSessions, _clientJmsDelegate.getNoOfTestSessions());
    }

    private void createTestSession(String connectionName, String sessionName) throws Exception
    {
        createTestSession(connectionName, sessionName, true);
    }

    private void createTestProducer(String sessionName, String producerName, String destinationName, boolean shouldSucceed) throws Exception
    {
        final CreateProducerCommand createProducerCommand = new CreateProducerCommand();
        createProducerCommand.setParticipantName(producerName);
        createProducerCommand.setSessionName(sessionName);
        createProducerCommand.setDestinationName(destinationName);
        createProducerCommand.setNumberOfMessages(100);

        sendCommandAndValidateResponse(createProducerCommand, shouldSucceed);
    }

    private void createTestProducer(String sessionName, String producerName, String destinationName) throws Exception
    {
        createTestProducer(sessionName, producerName, destinationName, true);
    }

    private void createTestConsumer(String sessionName, String consumerName, String destinationName, boolean shouldSucceed) throws Exception
    {
        final CreateConsumerCommand createConsumerCommand = new CreateConsumerCommand();
        createConsumerCommand.setSessionName(sessionName);
        createConsumerCommand.setDestinationName(destinationName);
        createConsumerCommand.setParticipantName(consumerName);
        createConsumerCommand.setNumberOfMessages(1);

        sendCommandAndValidateResponse(createConsumerCommand, shouldSucceed);
    }

    private void createTestConsumer(String sessionName, String consumerName, String destinationName) throws Exception
    {
        createTestConsumer(sessionName, consumerName, destinationName, true);
    }

    private void validateResponse(CommandType originatingCommandType, Response response, boolean shouldSucceed) throws JMSException
    {
        assertEquals("Response is a reply to the wrong command: " + response,
                originatingCommandType,
                response.getInReplyToCommandType());

        boolean shouldHaveError = !shouldSucceed;
        assertEquals("Response message " + response + " should have indicated hasError=" + shouldHaveError,
                shouldHaveError,
                response.hasError());
    }

    private void createClientQueueProducer(final RegisterClientCommand registration) throws JMSException
    {
        final Destination clientCommandQueue = createDestinationFromRegistration(registration);
        _clientQueueProducer = _session.createProducer(clientCommandQueue);
    }

    private Queue createDestinationFromRegistration(final RegisterClientCommand registrationCommand) throws JMSException
    {
        String clientQueueName = registrationCommand.getClientQueueName();
        assertNotNull("Null client queue in register message", clientQueueName);
        return _session.createQueue(clientQueueName);
    }

    private static void assertState(Client client, ClientState expectedState)
    {
        ClientState clientState = client.getState();
        assertEquals("Client should be in state: " + expectedState + " but is in state " + clientState, expectedState, clientState);
    }
}