summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/ReplicationTest.cpp
blob: 7bd585639d6991bfb0604dd9d84e543e0b65e9df (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
 /*
 *
 * 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 "unit_test.h"
#include "test_tools.h"
#include "BrokerFixture.h"

#include "qpid/Plugin.h"
#include "qpid/broker/Broker.h"
#include "qpid/client/QueueOptions.h"
#include "qpid/framing/reply_exceptions.h"
#include "qpid/framing/SequenceNumber.h"
#include "qpid/replication/constants.h"
#include "qpid/sys/Shlib.h"

#include <string>
#include <sstream>
#include <vector>

#include <boost/assign.hpp>
#include <boost/bind.hpp>

using namespace qpid::client;
using namespace qpid::framing;
using namespace qpid::replication::constants;
using boost::assign::list_of;

QPID_AUTO_TEST_SUITE(ReplicationTestSuite)

qpid::sys::Shlib plugin("../.libs/replicating_listener.so");

qpid::broker::Broker::Options getBrokerOpts(const std::vector<std::string>& args)
{
    std::vector<const char*> argv(args.size());
    transform(args.begin(), args.end(), argv.begin(), boost::bind(&string::c_str, _1));

    qpid::broker::Broker::Options opts;
    qpid::Plugin::addOptions(opts);
    opts.parse(argv.size(), &argv[0], "", true);
    return opts;
}

QPID_AUTO_TEST_CASE(testReplicationExchange) 
{
    qpid::broker::Broker::Options brokerOpts(getBrokerOpts(list_of<string>("qpidd")
                                                           ("--replication-exchange-name=qpid.replication")));
    ProxySessionFixture f(brokerOpts);


    std::string dataQ("queue-1");
    std::string eventQ("event-queue-1");
    std::string dataQ2("queue-2");
    std::string eventQ2("event-queue-2");
    FieldTable eventQopts;
    eventQopts.setString("qpid.insert_sequence_numbers", REPLICATION_EVENT_SEQNO);

    f.session.queueDeclare(arg::queue=eventQ, arg::exclusive=true, arg::autoDelete=true, arg::arguments=eventQopts);
    f.session.exchangeBind(arg::exchange="qpid.replication", arg::queue=eventQ, arg::bindingKey=dataQ);
    
    f.session.queueDeclare(arg::queue=eventQ2, arg::exclusive=true, arg::autoDelete=true, arg::arguments=eventQopts);
    f.session.exchangeBind(arg::exchange="qpid.replication", arg::queue=eventQ2, arg::bindingKey=dataQ2);

    QueueOptions args;
    args.enableQueueEvents(false);
    f.session.queueDeclare(arg::queue=dataQ, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
    f.session.queueDeclare(arg::queue=dataQ2, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
    for (int i = 0; i < 10; i++) {
        f.session.messageTransfer(arg::content=Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), dataQ));
        f.session.messageTransfer(arg::content=Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), dataQ2));
    }
    Message msg;
    LocalQueue incoming;
    Subscription sub = f.subs.subscribe(incoming, dataQ);
    for (int i = 0; i < 10; i++) {
        BOOST_CHECK(incoming.get(msg, qpid::sys::TIME_SEC));
        BOOST_CHECK_EQUAL((boost::format("%1%_%2%") % "Message" % (i+1)).str(), msg.getData());
    }
    BOOST_CHECK(!f.subs.get(msg, dataQ));

    sub.cancel();
    sub = f.subs.subscribe(incoming, eventQ);
    //check that we received enqueue events for first queue:
    for (int i = 0; i < 10; i++) {
        BOOST_CHECK(incoming.get(msg, qpid::sys::TIME_SEC));
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsString(REPLICATION_TARGET_QUEUE), dataQ);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt(REPLICATION_EVENT_TYPE), ENQUEUE);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt64(REPLICATION_EVENT_SEQNO), (i+1));
        BOOST_CHECK_EQUAL((boost::format("%1%_%2%") % "Message" % (i+1)).str(), msg.getData());
    }
    //check that we received dequeue events for first queue:
    for (int i = 0; i < 10; i++) {
        BOOST_CHECK(incoming.get(msg, qpid::sys::TIME_SEC));
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsString(REPLICATION_TARGET_QUEUE), dataQ);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt(REPLICATION_EVENT_TYPE), DEQUEUE);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt(DEQUEUED_MESSAGE_POSITION), (i+1));
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt64(REPLICATION_EVENT_SEQNO), (i+11));
    }

    sub.cancel();
    sub = f.subs.subscribe(incoming, eventQ2);
    //check that we received enqueue events for second queue:
    for (int i = 0; i < 10; i++) {
        BOOST_CHECK(incoming.get(msg, qpid::sys::TIME_SEC));
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsString(REPLICATION_TARGET_QUEUE), dataQ2);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt(REPLICATION_EVENT_TYPE), ENQUEUE);
        BOOST_CHECK_EQUAL(msg.getHeaders().getAsInt64(REPLICATION_EVENT_SEQNO), (i+1));
        BOOST_CHECK_EQUAL((boost::format("%1%_%2%") % "Message" % (i+1)).str(), msg.getData());
    }
}


QPID_AUTO_TEST_SUITE_END()