summaryrefslogtreecommitdiff
path: root/qpid/java/qpid-perftests-systests/src/test/java/org/apache/qpid/systest/disttest/clientonly/ConsumerParticipantTest.java
blob: a3c043086518897f90771520baa36f989d70f552 (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
/*
 * 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 javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.qpid.disttest.client.Client;
import org.apache.qpid.disttest.client.ConsumerParticipant;
import org.apache.qpid.disttest.client.ParticipantExecutor;
import org.apache.qpid.disttest.message.CreateConsumerCommand;
import org.apache.qpid.disttest.message.ParticipantResult;
import org.apache.qpid.systest.disttest.DistributedTestSystemTestBase;
import org.apache.qpid.systest.disttest.clientonly.ProducerParticipantTest.TestClientJmsDelegate;

public class ConsumerParticipantTest  extends DistributedTestSystemTestBase
{
    private MessageProducer _producer;
    private Session _session;
    private TestClientJmsDelegate _delegate;
    private Client _client;
    private ControllerQueue _controllerQueue;

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

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

        _delegate = new TestClientJmsDelegate(getContext());
        _client = new Client(_delegate);
    }


    @Override
    protected void tearDown() throws Exception
    {
        _controllerQueue.close();
        super.tearDown();
    }

    public void testConsumeNumberOfMessagesSynchronously() throws Exception
    {
        runTest(Session.AUTO_ACKNOWLEDGE, 10, 0, true);
    }

    public void testConsumeNumberOfMessagesAsynchronously() throws Exception
    {
        runTest(Session.AUTO_ACKNOWLEDGE, 10, 0, false);
    }

    public void testSelectors() throws Exception
    {
        final CreateConsumerCommand command = new CreateConsumerCommand();
        command.setNumberOfMessages(10);
        command.setSessionName("testSession");
        command.setDestinationName(getTestQueueName());
        command.setSelector("id=1");
        Session session = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        _delegate.addConnection("name-does-not-matter", _connection);
        _delegate.addSession(command.getSessionName(), session);

        ConsumerParticipant consumerParticipant =  new ConsumerParticipant(_delegate, command);
        _delegate.createConsumer(command);

        for (int i = 0; i < 20; i++)
        {
            Message message = _session.createMessage();
            if (i % 2 == 0)
            {
                message.setIntProperty("id", 0);
            }
            else
            {
                message.setIntProperty("id", 1);
            }
            _producer.send(message);
        }

        new ParticipantExecutor(consumerParticipant).start(_client);

        ParticipantResult results = _controllerQueue.getNext();
        assertNotNull("No results message recieved", results);
        assertEquals("Unexpected number of messages received", 10, results.getNumberOfMessagesProcessed());

        Session testQueueConsumerSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageConsumer testQueueConsumer = testQueueConsumerSession.createConsumer(getTestQueue());
        for (int i = 0; i < 10; i++)
        {
            Message message = testQueueConsumer.receive(2000);
            assertNotNull("Message is not received: " + message, message);
            assertEquals("Unexpected id value", 0, message.getIntProperty("id"));
        }
        Message message = testQueueConsumer.receive(2000);
        assertNull("Unexpected message remaining on test queue: " + message, message);

        _connection.stop();
    }

    protected void runTest(int acknowledgeMode, int numberOfMessages, int batchSize, boolean synchronous) throws Exception
    {
        final CreateConsumerCommand command = new CreateConsumerCommand();
        command.setNumberOfMessages(numberOfMessages);
        command.setBatchSize(batchSize);
        command.setSessionName("testSession");
        command.setDestinationName(getTestQueueName());
        command.setSynchronous(synchronous);

        Session session = _connection.createSession(Session.SESSION_TRANSACTED == acknowledgeMode, acknowledgeMode);

        _delegate.addConnection("name-does-not-matter", _connection);
        _delegate.addSession(command.getSessionName(), session);

        ConsumerParticipant consumerParticipant =  new ConsumerParticipant(_delegate, command);
        _delegate.createConsumer(command);

        for (int i = 0; i < numberOfMessages; i++)
        {
            _producer.send(_session.createMessage());
        }

        new ParticipantExecutor(consumerParticipant).start(_client);

        ParticipantResult results = _controllerQueue.getNext();
        assertNotNull("No results message recieved", results);

        Session testQueueConsumerSession = _connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        final MessageConsumer testQueueConsumer = testQueueConsumerSession.createConsumer(getTestQueue());
        Message message = testQueueConsumer.receive(2000);
        assertNull("Unexpected message remaining on test queue: " + message, message);

        _connection.stop();
    }
}