summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/ExchangeTest.cpp
blob: 0946d3115d3147d936dfb71f446d45095f57ce74 (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
/*
 *
 * 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 "qpid/Exception.h"
#include "qpid/broker/Exchange.h"
#include "qpid/broker/Queue.h"
#include "qpid/broker/DeliverableMessage.h"
#include "qpid/broker/DirectExchange.h"
#include "qpid/broker/ExchangeRegistry.h"
#include "qpid/broker/FanOutExchange.h"
#include "qpid/broker/HeadersExchange.h"
#include "qpid/broker/TopicExchange.h"
#include "qpid/framing/reply_exceptions.h"
#include "unit_test.h"
#include <iostream>
#include "MessageUtils.h"

using boost::intrusive_ptr;
using namespace qpid::broker;
using namespace qpid::framing;
using namespace qpid::sys;
using namespace qpid;

QPID_AUTO_TEST_SUITE(ExchangeTestSuite)

QPID_AUTO_TEST_CASE(testMe) 
{
    Queue::shared_ptr queue(new Queue("queue", true));
    Queue::shared_ptr queue2(new Queue("queue2", true));

    TopicExchange topic("topic");
    topic.bind(queue, "abc", 0);
    topic.bind(queue2, "abc", 0);

    DirectExchange direct("direct");
    direct.bind(queue, "abc", 0);
    direct.bind(queue2, "abc", 0);

    queue.reset();
    queue2.reset();

    intrusive_ptr<Message> msgPtr(MessageUtils::createMessage("exchange", "key", "id"));
    DeliverableMessage msg(msgPtr);
    topic.route(msg, "abc", 0);
    direct.route(msg, "abc", 0);

}

QPID_AUTO_TEST_CASE(testIsBound)
{
    Queue::shared_ptr a(new Queue("a", true));
    Queue::shared_ptr b(new Queue("b", true));
    Queue::shared_ptr c(new Queue("c", true));
    Queue::shared_ptr d(new Queue("d", true));
        
    string k1("abc");
    string k2("def");
    string k3("xyz");

    FanOutExchange fanout("fanout");
    BOOST_CHECK(fanout.bind(a, "", 0));
    BOOST_CHECK(fanout.bind(b, "", 0));
    BOOST_CHECK(fanout.bind(c, "", 0));

    BOOST_CHECK(fanout.isBound(a, 0, 0));
    BOOST_CHECK(fanout.isBound(b, 0, 0));
    BOOST_CHECK(fanout.isBound(c, 0, 0));
    BOOST_CHECK(!fanout.isBound(d, 0, 0));

    DirectExchange direct("direct");
    BOOST_CHECK(direct.bind(a, k1, 0));
    BOOST_CHECK(direct.bind(a, k3, 0));
    BOOST_CHECK(direct.bind(b, k2, 0));
    BOOST_CHECK(direct.bind(c, k1, 0));

    BOOST_CHECK(direct.isBound(a, 0, 0));
    BOOST_CHECK(direct.isBound(a, &k1, 0));
    BOOST_CHECK(direct.isBound(a, &k3, 0));
    BOOST_CHECK(!direct.isBound(a, &k2, 0));
    BOOST_CHECK(direct.isBound(b, 0, 0));
    BOOST_CHECK(direct.isBound(b, &k2, 0));
    BOOST_CHECK(direct.isBound(c, &k1, 0));
    BOOST_CHECK(!direct.isBound(d, 0, 0));
    BOOST_CHECK(!direct.isBound(d, &k1, 0));
    BOOST_CHECK(!direct.isBound(d, &k2, 0));
    BOOST_CHECK(!direct.isBound(d, &k3, 0));

    TopicExchange topic("topic");
    BOOST_CHECK(topic.bind(a, k1, 0));
    BOOST_CHECK(topic.bind(a, k3, 0));
    BOOST_CHECK(topic.bind(b, k2, 0));
    BOOST_CHECK(topic.bind(c, k1, 0));

    BOOST_CHECK(topic.isBound(a, 0, 0));
    BOOST_CHECK(topic.isBound(a, &k1, 0));
    BOOST_CHECK(topic.isBound(a, &k3, 0));
    BOOST_CHECK(!topic.isBound(a, &k2, 0));
    BOOST_CHECK(topic.isBound(b, 0, 0));
    BOOST_CHECK(topic.isBound(b, &k2, 0));
    BOOST_CHECK(topic.isBound(c, &k1, 0));
    BOOST_CHECK(!topic.isBound(d, 0, 0));
    BOOST_CHECK(!topic.isBound(d, &k1, 0));
    BOOST_CHECK(!topic.isBound(d, &k2, 0));
    BOOST_CHECK(!topic.isBound(d, &k3, 0));

    HeadersExchange headers("headers");
    FieldTable args1;
    args1.setString("x-match", "all");
    args1.setString("a", "A");
    args1.setInt("b", 1);
    FieldTable args2;
    args2.setString("x-match", "any");
    args2.setString("a", "A");
    args2.setInt("b", 1);
    FieldTable args3;
    args3.setString("x-match", "any");
    args3.setString("c", "C");
    args3.setInt("b", 6);

    headers.bind(a, "", &args1);
    headers.bind(a, "", &args3);
    headers.bind(b, "", &args2);
    headers.bind(c, "", &args1);
        
    BOOST_CHECK(headers.isBound(a, 0, 0));
    BOOST_CHECK(headers.isBound(a, 0, &args1));
    BOOST_CHECK(headers.isBound(a, 0, &args3));
    BOOST_CHECK(!headers.isBound(a, 0, &args2));
    BOOST_CHECK(headers.isBound(b, 0, 0));
    BOOST_CHECK(headers.isBound(b, 0, &args2));
    BOOST_CHECK(headers.isBound(c, 0, &args1));
    BOOST_CHECK(!headers.isBound(d, 0, 0));
    BOOST_CHECK(!headers.isBound(d, 0, &args1));
    BOOST_CHECK(!headers.isBound(d, 0, &args2));
    BOOST_CHECK(!headers.isBound(d, 0, &args3));
}

QPID_AUTO_TEST_CASE(testDeleteGetAndRedeclare) 
{
    ExchangeRegistry exchanges;
    exchanges.declare("my-exchange", "direct", false, FieldTable());
    exchanges.destroy("my-exchange");
    try {
        exchanges.get("my-exchange");
    } catch (const NotFoundException&) {}
    std::pair<Exchange::shared_ptr, bool> response = exchanges.declare("my-exchange", "direct", false, FieldTable());
    BOOST_CHECK_EQUAL(string("direct"), response.first->getType());  
}

intrusive_ptr<Message> cmessage(std::string exchange, std::string routingKey) {
    intrusive_ptr<Message> msg(new Message());
    AMQFrame method(in_place<MessageTransferBody>(ProtocolVersion(), exchange, 0, 0));
    AMQFrame header(in_place<AMQHeaderBody>());
    msg->getFrames().append(method);
    msg->getFrames().append(header);
    msg->getFrames().getHeaders()->get<DeliveryProperties>(true)->setRoutingKey(routingKey);
    return msg;
}

QPID_AUTO_TEST_CASE(testSequenceOptions) 
{
    FieldTable args;
    args.setInt("qpid.msg_sequence",1);
    char* buff = new char[10000];
    framing::Buffer buffer(buff,10000);
    {
        DirectExchange direct("direct1", false, args);

        intrusive_ptr<Message> msg1 = cmessage("e", "A");
        intrusive_ptr<Message> msg2 = cmessage("e", "B");
        intrusive_ptr<Message> msg3 = cmessage("e", "C");

        DeliverableMessage dmsg1(msg1);
        DeliverableMessage dmsg2(msg2);
        DeliverableMessage dmsg3(msg3);

        direct.route(dmsg1, "abc", 0);
        direct.route(dmsg2, "abc", 0);
        direct.route(dmsg3, "abc", 0);

        BOOST_CHECK_EQUAL(1, msg1->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));
        BOOST_CHECK_EQUAL(2, msg2->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));
        BOOST_CHECK_EQUAL(3, msg3->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));

        FanOutExchange fanout("fanout1", false, args);
        HeadersExchange header("headers1", false, args);
        TopicExchange topic ("topic1", false, args);

        // check other exchanges, that they preroute
        intrusive_ptr<Message> msg4 = cmessage("e", "A");
        intrusive_ptr<Message> msg5 = cmessage("e", "B");
        intrusive_ptr<Message> msg6 = cmessage("e", "C");

        DeliverableMessage dmsg4(msg4);
        DeliverableMessage dmsg5(msg5);
        DeliverableMessage dmsg6(msg6);

        fanout.route(dmsg4, "abc", 0);
        BOOST_CHECK_EQUAL(1, msg4->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));

        FieldTable headers;
        header.route(dmsg5, "abc", &headers);
        BOOST_CHECK_EQUAL(1, msg5->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));

        topic.route(dmsg6, "abc", 0);
        BOOST_CHECK_EQUAL(1, msg6->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));
        direct.encode(buffer);
    }
    {
        
        ExchangeRegistry exchanges;
        buffer.reset();
        DirectExchange::shared_ptr exch_dec = Exchange::decode(exchanges, buffer);
        
        intrusive_ptr<Message> msg1 = cmessage("e", "A");
        DeliverableMessage dmsg1(msg1);
        exch_dec->route(dmsg1, "abc", 0);

        BOOST_CHECK_EQUAL(4, msg1->getApplicationHeaders()->getAsInt64("qpid.msg_sequence"));
    
    }
    delete [] buff;
}

QPID_AUTO_TEST_CASE(testIVEOption) 
{
    FieldTable args;
    args.setInt("qpid.ive",1);
    DirectExchange direct("direct1", false, args);
    FanOutExchange fanout("fanout1", false, args);
    HeadersExchange header("headers1", false, args);
    TopicExchange topic ("topic1", false, args);
    
    intrusive_ptr<Message> msg1 = cmessage("direct1", "abc");
    msg1->getProperties<MessageProperties>()->getApplicationHeaders().setString("a", "abc");
    DeliverableMessage dmsg1(msg1);

    FieldTable args2;
    args2.setString("x-match", "any");
    args2.setString("a", "abc");
    
    direct.route(dmsg1, "abc", 0);
    fanout.route(dmsg1, "abc", 0);
    header.route(dmsg1, "abc", &args2);
    topic.route(dmsg1, "abc", 0);
    Queue::shared_ptr queue(new Queue("queue", true));
    Queue::shared_ptr queue1(new Queue("queue1", true));
    Queue::shared_ptr queue2(new Queue("queue2", true));
    Queue::shared_ptr queue3(new Queue("queue3", true));
    
    BOOST_CHECK(HeadersExchange::match(args2, msg1->getProperties<MessageProperties>()->getApplicationHeaders()));
    
    BOOST_CHECK(direct.bind(queue, "abc", 0));
    BOOST_CHECK(fanout.bind(queue1, "abc", 0));
    BOOST_CHECK(header.bind(queue2, "", &args2));
    BOOST_CHECK(topic.bind(queue3, "abc", 0));
    
    BOOST_CHECK_EQUAL(1u,queue->getMessageCount());
    BOOST_CHECK_EQUAL(1u,queue1->getMessageCount());
    BOOST_CHECK_EQUAL(1u,queue2->getMessageCount());
    BOOST_CHECK_EQUAL(1u,queue3->getMessageCount());
    
}


QPID_AUTO_TEST_SUITE_END()