summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/QueueEvents.cpp
blob: df3c937e33316401fc1b1be2db240f49344173b8 (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
/*
 *
 * 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.
 *
 */

#include "MessageUtils.h"
#include "unit_test.h"
#include "BrokerFixture.h"
#include "qpid/broker/Message.h"
#include "qpid/broker/Queue.h"
#include "qpid/broker/QueueEvents.h"
#include "qpid/client/QueueOptions.h"
#include "qpid/framing/SequenceNumber.h"
#include "qpid/sys/Dispatcher.h"
#include <boost/bind.hpp>
#include <boost/format.hpp>

QPID_AUTO_TEST_SUITE(QueueEventsSuite)

using namespace qpid::client;
using namespace qpid::broker;
using namespace qpid::sys;
using qpid::framing::SequenceNumber;

struct DummyListener
{
    typedef std::deque<QueueEvents::Event> Events;

    Events events;
    boost::shared_ptr<Poller> poller;

    void handle(QueueEvents::Event e)
    {
        if (events.empty()) {
            BOOST_FAIL("Unexpected event received");
        } else {
            BOOST_CHECK_EQUAL(events.front().type, e.type);
            BOOST_CHECK_EQUAL(events.front().msg.queue, e.msg.queue);
            BOOST_CHECK_EQUAL(events.front().msg.payload, e.msg.payload);
            BOOST_CHECK_EQUAL(events.front().msg.position, e.msg.position);
            events.pop_front();
        }
        if (events.empty() && poller) poller->shutdown();
    }

    void expect(QueueEvents::Event e)
    {
        events.push_back(e);
    }
};

QPID_AUTO_TEST_CASE(testBasicEventProcessing)
{
    boost::shared_ptr<Poller> poller(new Poller());
    sys::Dispatcher dispatcher(poller);
    Thread dispatchThread(dispatcher);
    QueueEvents events(poller);
    DummyListener listener;
    listener.poller = poller;
    events.registerListener("dummy", boost::bind(&DummyListener::handle, &listener, _1));
    //signal occurence of some events:
    Queue queue("queue1");
    SequenceNumber id;
    QueuedMessage event1(&queue, MessageUtils::createMessage(), id);
    QueuedMessage event2(&queue, MessageUtils::createMessage(), ++id);

    //define events expected by listener:
    listener.expect(QueueEvents::Event(QueueEvents::ENQUEUE, event1));
    listener.expect(QueueEvents::Event(QueueEvents::ENQUEUE, event2));
    listener.expect(QueueEvents::Event(QueueEvents::DEQUEUE, event1));

    events.enqueued(event1);
    events.enqueued(event2);
    events.dequeued(event1);

    dispatchThread.join();
    events.shutdown();
    events.unregisterListener("dummy");
}


struct EventRecorder
{
    struct EventRecord
    {
        QueueEvents::EventType type;
        std::string queue;
        std::string content;
        SequenceNumber position;
    };

    typedef std::deque<EventRecord> Events;

    Events events;

    void handle(QueueEvents::Event event)
    {
        EventRecord record;
        record.type = event.type;
        record.queue = event.msg.queue->getName();
        event.msg.payload->getFrames().getContent(record.content);
        record.position = event.msg.position;
        events.push_back(record);
    }

    void check(QueueEvents::EventType type, const std::string& queue, const std::string& content, const SequenceNumber& position)
    {
        if (events.empty()) {
            BOOST_FAIL("Missed event");
        } else {
            BOOST_CHECK_EQUAL(events.front().type, type);
            BOOST_CHECK_EQUAL(events.front().queue, queue);
            BOOST_CHECK_EQUAL(events.front().content, content);
            BOOST_CHECK_EQUAL(events.front().position, position);
            events.pop_front();
        }
    }
    void checkEnqueue(const std::string& queue, const std::string& data, const SequenceNumber& position)
    {
        check(QueueEvents::ENQUEUE, queue, data, position);
    }

    void checkDequeue(const std::string& queue, const std::string& data, const SequenceNumber& position)
    {
        check(QueueEvents::DEQUEUE, queue, data, position);
    }
};

QPID_AUTO_TEST_CASE(testSystemLevelEventProcessing)
{
    ProxySessionFixture fixture;
    //register dummy event listener to broker
    EventRecorder listener;
    fixture.broker->getQueueEvents().registerListener("recorder", boost::bind(&EventRecorder::handle, &listener, _1));

    //declare queue with event options specified
    QueueOptions options;
    options.enableQueueEvents(false);
    std::string q("queue-events-test");
    fixture.session.queueDeclare(arg::queue=q, arg::arguments=options);
    //send and consume some messages
    LocalQueue incoming;
    Subscription sub = fixture.subs.subscribe(incoming, q); 
    for (int i = 0; i < 5; i++) {
        fixture.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
    }
    for (int i = 0; i < 3; i++) {
        BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
    }
    for (int i = 5; i < 10; i++) {
        fixture.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
    }
    for (int i = 3; i < 10; i++) {
        BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
    }
    fixture.connection.close();
    fixture.broker->getQueueEvents().shutdown();

    //check listener was notified of all events, and in correct order
    SequenceNumber enqueueId(1);
    SequenceNumber dequeueId(1);
    for (int i = 0; i < 5; i++) {
        listener.checkEnqueue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), enqueueId++);
    }    
    for (int i = 0; i < 3; i++) {
        listener.checkDequeue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), dequeueId++);
    }
    for (int i = 5; i < 10; i++) {
        listener.checkEnqueue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), enqueueId++);
    }
    for (int i = 3; i < 10; i++) {
        listener.checkDequeue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), dequeueId++);
    }
}

QPID_AUTO_TEST_CASE(testSystemLevelEventProcessing_enqueuesOnly)
{
    ProxySessionFixture fixture;
    //register dummy event listener to broker
    EventRecorder listener;
    fixture.broker->getQueueEvents().registerListener("recorder", boost::bind(&EventRecorder::handle, &listener, _1));

    //declare queue with event options specified
    QueueOptions options;
    options.enableQueueEvents(true);
    std::string q("queue-events-test");
    fixture.session.queueDeclare(arg::queue=q, arg::arguments=options);
    //send and consume some messages
    LocalQueue incoming;
    Subscription sub = fixture.subs.subscribe(incoming, q); 
    for (int i = 0; i < 5; i++) {
        fixture.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
    }
    for (int i = 0; i < 3; i++) {
        BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
    }
    for (int i = 5; i < 10; i++) {
        fixture.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
    }
    for (int i = 3; i < 10; i++) {
        BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
    }
    fixture.connection.close();
    fixture.broker->getQueueEvents().shutdown();

    //check listener was notified of all events, and in correct order
    SequenceNumber enqueueId(1);
    SequenceNumber dequeueId(1);
    for (int i = 0; i < 5; i++) {
        listener.checkEnqueue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), enqueueId++);
    }    
    for (int i = 5; i < 10; i++) {
        listener.checkEnqueue(q, (boost::format("%1%_%2%") % "Message" % (i+1)).str(), enqueueId++);
    }
}

QPID_AUTO_TEST_SUITE_END()