summaryrefslogtreecommitdiff
path: root/java/perftests/src/test/java/org/apache/qpid/disttest/controller/TestRunnerTest.java
blob: 983da299b97aa0e4696b88214330497f7974d2f0 (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
/*
 * 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.disttest.controller;

import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import junit.framework.TestCase;

import org.apache.qpid.disttest.DistributedTestException;
import org.apache.qpid.disttest.controller.config.QueueConfig;
import org.apache.qpid.disttest.controller.config.TestInstance;
import org.apache.qpid.disttest.jms.ControllerJmsDelegate;
import org.apache.qpid.disttest.message.Command;
import org.apache.qpid.disttest.message.CreateConnectionCommand;
import org.apache.qpid.disttest.message.NoOpCommand;
import org.apache.qpid.disttest.message.ParticipantResult;
import org.apache.qpid.disttest.message.Response;
import org.apache.qpid.disttest.message.StartTestCommand;
import org.apache.qpid.disttest.message.TearDownTestCommand;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

public class TestRunnerTest extends TestCase
{
    private static final String TEST_NAME = "TEST_NAME";
    private static final String PARTICIPANT_NAME = "TEST_PARTICIPANT_NAME";
    private static final int ITERATION_NUMBER = 1;

    private static final String CLIENT1_REGISTERED_NAME = "client-uid1";
    private static final String CLIENT1_CONFIGURED_NAME = "client1";

    private static final long COMMAND_RESPONSE_TIMEOUT = 1000;
    private static final long TEST_RESULT_TIMEOUT = 2000;
    private static final long DELAY = 100;

    private TestRunner _testRunner;
    private TestInstance _testInstance;
    private ControllerJmsDelegate _respondingJmsDelegate;
    private ParticipatingClients _participatingClients;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        _respondingJmsDelegate = mock(ControllerJmsDelegate.class);

        _participatingClients = mock(ParticipatingClients.class);
        when(_participatingClients.getRegisteredNameFromConfiguredName(CLIENT1_CONFIGURED_NAME)).thenReturn(CLIENT1_REGISTERED_NAME);
        when(_participatingClients.getConfiguredNameFromRegisteredName(CLIENT1_REGISTERED_NAME)).thenReturn(CLIENT1_CONFIGURED_NAME);
        when(_participatingClients.getRegisteredNames()).thenReturn(Collections.singleton(CLIENT1_REGISTERED_NAME));

        doAnswer(new Answer<Void>()
        {
            @Override
            public Void answer(InvocationOnMock invocation) throws Throwable
            {
                final String clientName = (String)invocation.getArguments()[0];
                final Command command = (Command)invocation.getArguments()[1];
                _testRunner.processCommandResponse(new Response(clientName, command.getType()));
                return null;
            }
        }).when(_respondingJmsDelegate).sendCommandToClient(anyString(), isA(Command.class));
    }

    public void testSendConnectionCommandToClient()
    {
        _testInstance = createTestInstanceWithConnection();

        _testRunner = new TestRunner(_participatingClients, _testInstance , _respondingJmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);
        _testRunner.sendTestSetupCommands();

        verify(_respondingJmsDelegate).sendCommandToClient(eq(CLIENT1_REGISTERED_NAME), isA(CreateConnectionCommand.class));
    }

    public void testSendCommandToAllParticipatingClients()
    {
        _testRunner = new TestRunner(_participatingClients, mock(TestInstance.class), _respondingJmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);

        StartTestCommand startTestCommand = new StartTestCommand();
        _testRunner.sendCommandToParticipatingClients(startTestCommand);

        verify(_respondingJmsDelegate).sendCommandToClient(CLIENT1_REGISTERED_NAME, startTestCommand);
    }

    public void testWaitsForCommandResponses()
    {
        _testInstance = createTestInstanceWithConnection();
        _testRunner = new TestRunner(_participatingClients, _testInstance , _respondingJmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);

        _testRunner.sendTestSetupCommands();

        _testRunner.awaitCommandResponses();
    }

    public void testClientFailsToSendCommandResponseWithinTimeout()
    {
        ControllerJmsDelegate jmsDelegate = mock(ControllerJmsDelegate.class);

        _testInstance = createTestInstanceWithConnection();
        _testRunner = new TestRunner(_participatingClients, _testInstance , jmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);

        _testRunner.sendTestSetupCommands();
        // we don't call sendCommandResponseLater so controller should time out

        try
        {
            _testRunner.awaitCommandResponses();
            fail("Exception not thrown");
        }
        catch (DistributedTestException e)
        {
            // PASS
        }
    }

    public void testCreateAndDeleteQueues()
    {
        _testInstance =  mock(TestInstance.class);
        List<QueueConfig> queues = mock(List.class);
        when(_testInstance.getQueues()).thenReturn(queues);

        _testRunner = new TestRunner(_participatingClients, _testInstance, _respondingJmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);

        _testRunner.createQueues();
        verify(_respondingJmsDelegate).createQueues(queues);

        _testRunner.deleteQueues();
        verify(_respondingJmsDelegate).deleteQueues(queues);
    }

    public void testRun()
    {
        _testInstance = createTestInstanceWithOneParticipant();
        _testRunner = new TestRunner(_participatingClients, _testInstance , _respondingJmsDelegate, COMMAND_RESPONSE_TIMEOUT, TEST_RESULT_TIMEOUT);

        ParticipantResult incomingParticipantResult = new ParticipantResult(PARTICIPANT_NAME);
        incomingParticipantResult.setRegisteredClientName(CLIENT1_REGISTERED_NAME);
        sendTestResultsLater(_testRunner, incomingParticipantResult);

        TestResult results = _testRunner.run();

        verify(_respondingJmsDelegate).addCommandListener(isA(TestRunner.TestCommandResponseListener.class));
        verify(_respondingJmsDelegate).addCommandListener(isA(TestRunner.ParticipantResultListener.class));

        verify(_respondingJmsDelegate).createQueues(isA(List.class));

        verify(_respondingJmsDelegate).sendCommandToClient(eq(CLIENT1_REGISTERED_NAME), isA(StartTestCommand.class));
        verify(_respondingJmsDelegate).sendCommandToClient(eq(CLIENT1_REGISTERED_NAME), isA(NoOpCommand.class));
        verify(_respondingJmsDelegate).sendCommandToClient(eq(CLIENT1_REGISTERED_NAME), isA(TearDownTestCommand.class));

        verify(_respondingJmsDelegate).deleteQueues(isA(List.class));

        verify(_respondingJmsDelegate).removeCommandListener(isA(TestRunner.ParticipantResultListener.class));
        verify(_respondingJmsDelegate).removeCommandListener(isA(TestRunner.TestCommandResponseListener.class));

        List<ParticipantResult> participantResults = results.getParticipantResults();
        assertEquals(1, participantResults.size());
        ParticipantResult resultingParticipantResult = participantResults.get(0);

        assertResultHasCorrectTestDetails(resultingParticipantResult);
    }

    private void assertResultHasCorrectTestDetails(ParticipantResult resultingParticipantResult)
    {
        assertEquals("Test runner should have set configured name when it received participant results",
                CLIENT1_CONFIGURED_NAME, resultingParticipantResult.getConfiguredClientName());
        assertEquals("Test runner should have set test name when it received participant results",
                TEST_NAME, resultingParticipantResult.getTestName());
        assertEquals("Test runner should have set test iteration number when it received participant results",
                ITERATION_NUMBER, resultingParticipantResult.getIterationNumber());
    }


    private TestInstance createTestInstanceWithOneParticipant()
    {
        TestInstance testInstance = mock(TestInstance.class);

        List<CommandForClient> commands = Arrays.asList(
                new CommandForClient(CLIENT1_CONFIGURED_NAME, new NoOpCommand()));

        when(testInstance.createCommands()).thenReturn(commands);

        when(testInstance.getTotalNumberOfParticipants()).thenReturn(1);

        when(testInstance.getName()).thenReturn(TEST_NAME);

        List<QueueConfig> queues = mock(List.class);
        when(testInstance.getQueues()).thenReturn(queues);

        when(testInstance.getIterationNumber()).thenReturn(ITERATION_NUMBER);

        return testInstance;
    }

    private TestInstance createTestInstanceWithConnection()
    {
        TestInstance testInstance = mock(TestInstance.class);

        List<CommandForClient> commands = Arrays.asList(
                new CommandForClient(CLIENT1_CONFIGURED_NAME, new CreateConnectionCommand("conn1", "factory")));

        when(testInstance.createCommands()).thenReturn(commands);

        return testInstance;
    }

    private void sendTestResultsLater(final TestRunner runner, final ParticipantResult result)
    {
        doLater(new TimerTask()
        {
            @Override
            public void run()
            {
                runner.processParticipantResult(result);
            }
        }, DELAY);
    }

    private void doLater(TimerTask task, long delayInMillis)
    {
        Timer timer = new Timer();
        timer.schedule(task, delayInMillis);
    }

}