summaryrefslogtreecommitdiff
path: root/java/systests/src/main/java/org/apache/qpid/systest/management/jmx/ConnectionManagementTest.java
blob: 28d7bf4aed6df3368c0569aff3a03b4f5976f879 (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
/*
 * 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.management.jmx;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.management.JMException;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.TabularData;

import org.apache.commons.lang.StringUtils;
import org.apache.qpid.common.QpidProperties;
import org.apache.qpid.management.common.mbeans.ManagedConnection;
import org.apache.qpid.management.common.mbeans.ManagedQueue;
import org.apache.qpid.test.utils.JMXTestUtils;
import org.apache.qpid.test.utils.QpidBrokerTestCase;

public class ConnectionManagementTest extends QpidBrokerTestCase
{
    private static final String VIRTUAL_HOST_NAME = "test";

    private JMXTestUtils _jmxUtils;
    private Connection _connection;

    public void setUp() throws Exception
    {
        _jmxUtils = new JMXTestUtils(this);
        _jmxUtils.setUp(); // modifies broker config therefore must be done before super.setUp()
        super.setUp();
        _jmxUtils.open();
    }

    public void tearDown() throws Exception
    {
        try
        {
            if (_jmxUtils != null)
            {
                _jmxUtils.close();
            }
        }
        finally
        {
            super.tearDown();
        }
    }

    public void testNumberOfManagedConnectionsMatchesNumberOfClientConnections() throws Exception
    {
        assertEquals("Expected no managed connections", 0, getManagedConnections().size());

        _connection = getConnection();
        assertEquals("Expected one managed connection", 1, getManagedConnections().size());

        _connection.close();
        assertEquals("Expected no managed connections after client connection closed", 0, getManagedConnections().size());
    }

    public void testGetAttributes() throws Exception
    {
        _connection = getConnection();
        final ManagedConnection mBean = getConnectionMBean();

        checkAuthorisedId(mBean);
        checkClientVersion(mBean);
        checkClientId(mBean);
    }

    public void testNonTransactedSession() throws Exception
    {
        _connection = getConnection();

        boolean transactional = false;
        boolean flowBlocked = false;

        _connection.createSession(transactional, Session.AUTO_ACKNOWLEDGE);

        final ManagedConnection mBean = getConnectionMBean();
        final CompositeDataSupport row = getTheOneChannelRow(mBean);
        assertChannelRowData(row, 0, transactional, flowBlocked);
    }

    public void testTransactedSessionWithUnackMessages() throws Exception
    {
        _connection = getConnection();
        _connection.start();

        boolean transactional = true;
        int numberOfMessages = 2;
        final Session session = _connection.createSession(transactional, Session.SESSION_TRANSACTED);
        final Destination destination = session.createQueue(getTestQueueName());
        final MessageConsumer consumer = session.createConsumer(destination);

        sendMessage(session, destination, numberOfMessages);
        receiveMessagesWithoutCommit(consumer, numberOfMessages);

        final ManagedConnection mBean = getConnectionMBean();
        final CompositeDataSupport row = getTheOneChannelRow(mBean);
        boolean flowBlocked = false;
        assertChannelRowData(row, numberOfMessages, transactional, flowBlocked);

        // check that commit advances the lastIoTime
        final Date initialLastIOTime = mBean.getLastIoTime();
        session.commit();
        assertTrue("commit should have caused last IO time to advance", mBean.getLastIoTime().after(initialLastIOTime));

        // check that channels() now returns one session with no unacknowledged messages
        final CompositeDataSupport rowAfterCommit = getTheOneChannelRow(mBean);
        final Number unackCountAfterCommit = (Number) rowAfterCommit.get(ManagedConnection.UNACKED_COUNT);
        assertEquals("Unexpected number of unacknowledged messages", 0, unackCountAfterCommit);
    }


    public void testProducerFlowBlocked() throws Exception
    {
        _connection = getConnection();
        _connection.start();

        String queueName = getTestQueueName();
        Session session = _connection.createSession(true, Session.SESSION_TRANSACTED);
        Queue queue = session.createQueue(queueName);
        createQueueOnBroker(session, queue);

        ManagedQueue managedQueue = _jmxUtils.getManagedQueue(queueName);
        managedQueue.setFlowResumeCapacity(DEFAULT_MESSAGE_SIZE * 2l);
        managedQueue.setCapacity(DEFAULT_MESSAGE_SIZE * 3l);

        final ManagedConnection managedConnection = getConnectionMBean();

        // Check that producer flow is not block before test
        final CompositeDataSupport rowBeforeSend = getTheOneChannelRow(managedConnection);
        assertFlowBlocked(rowBeforeSend, false);


        // Check that producer flow does not become block too soon
        sendMessage(session, queue, 3);
        final CompositeDataSupport rowBeforeFull = getTheOneChannelRow(managedConnection);
        assertFlowBlocked(rowBeforeFull, false);

        // Fourth message will over-fill the queue (but as we are not sending more messages, client thread wont't block)
        sendMessage(session, queue, 1);
        final CompositeDataSupport rowAfterFull = getTheOneChannelRow(managedConnection);
        assertFlowBlocked(rowAfterFull, true);

        // Consume two to bring the queue down to the resume capacity
        MessageConsumer consumer = session.createConsumer(queue);
        assertNotNull("Could not receive first message", consumer.receive(1000));
        assertNotNull("Could not receive second message", consumer.receive(1000));
        session.commit();

        // Check that producer flow is no longer blocked
        final CompositeDataSupport rowAfterReceive = getTheOneChannelRow(managedConnection);
        assertFlowBlocked(rowAfterReceive, false);
    }

    private void createQueueOnBroker(Session session, Destination destination) throws JMSException
    {
        session.createConsumer(destination).close(); // Create a consumer only to cause queue creation
    }

    private void assertChannelRowData(final CompositeData row, int unacknowledgedMessages, boolean isTransactional, boolean flowBlocked)
    {
        assertNotNull(row);
        assertEquals("Unexpected transactional flag", isTransactional, row.get(ManagedConnection.TRANSACTIONAL));
        assertEquals("Unexpected unacknowledged message count", unacknowledgedMessages, row.get(ManagedConnection.UNACKED_COUNT));
        assertEquals("Unexpected flow blocked", flowBlocked, row.get(ManagedConnection.FLOW_BLOCKED));
    }

    private void assertFlowBlocked(final CompositeData row, boolean flowBlocked)
    {
        assertNotNull(row);
        assertEquals("Unexpected flow blocked", flowBlocked, row.get(ManagedConnection.FLOW_BLOCKED));
    }

    private void checkAuthorisedId(ManagedConnection mBean) throws Exception
    {
        assertEquals("Unexpected authorized id", GUEST_USERNAME, mBean.getAuthorizedId());
    }

    private void checkClientVersion(ManagedConnection mBean) throws Exception
    {
        String expectedVersion = QpidProperties.getReleaseVersion();
        assertTrue(StringUtils.isNotBlank(expectedVersion));

        assertEquals("Unexpected version", expectedVersion, mBean.getVersion());
    }

    private void checkClientId(ManagedConnection mBean) throws Exception
    {
        String expectedClientId = _connection.getClientID();
        assertTrue(StringUtils.isNotBlank(expectedClientId));

        assertEquals("Unexpected ClientId", expectedClientId, mBean.getClientId());
    }

    private ManagedConnection getConnectionMBean()
    {
        List<ManagedConnection> connections = getManagedConnections();
        assertNotNull("Connection MBean is not found", connections);
        assertEquals("Unexpected number of connection mbeans", 1, connections.size());
        final ManagedConnection mBean = connections.get(0);
        assertNotNull("Connection MBean is null", mBean);
        return mBean;
    }

    private List<ManagedConnection> getManagedConnections()
    {
        return _jmxUtils.getManagedConnections(VIRTUAL_HOST_NAME);
    }

    private CompositeDataSupport getTheOneChannelRow(final ManagedConnection mBean) throws Exception
    {
        TabularData channelsData = getChannelsDataWithRetry(mBean);

        assertEquals("Unexpected number of rows in channel table", 1, channelsData.size());

        @SuppressWarnings("unchecked")
        final Iterator<CompositeDataSupport> rowItr = (Iterator<CompositeDataSupport>) channelsData.values().iterator();
        final CompositeDataSupport row = rowItr.next();
        return row;
    }

    private void receiveMessagesWithoutCommit(final MessageConsumer consumer, int numberOfMessages) throws Exception
    {
        for (int i = 0; i < numberOfMessages; i++)
        {
            final Message m = consumer.receive(1000l);
            assertNotNull("Message " + i + " is not received", m);
        }
    }

    private TabularData getChannelsDataWithRetry(final ManagedConnection mBean)
            throws IOException, JMException
    {
        TabularData channelsData = mBean.channels();
        int retries = 0;
        while(channelsData.size() == 0 && retries < 5)
        {
            sleep();
            channelsData = mBean.channels();
            retries++;
        }
        return channelsData;
    }

    private void sleep()
    {
        try
        {
            Thread.sleep(50);
        }
        catch (InterruptedException ie)
        {
            Thread.currentThread().interrupt();
        }
    }}