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
285
286
287
288
289
290
291
292
293
|
/*
*
* 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 <sstream>
#include "unit_test.h"
#include "test_tools.h"
#include "qpid/broker/QueueFlowLimit.h"
#include "qpid/client/QueueOptions.h"
#include "qpid/sys/Time.h"
#include "qpid/framing/reply_exceptions.h"
#include "BrokerFixture.h"
using namespace qpid::broker;
using namespace qpid::client;
using namespace qpid::framing;
namespace qpid {
namespace tests {
QPID_AUTO_TEST_SUITE(QueuePolicyTestSuite)
QPID_AUTO_TEST_CASE(testRingPolicyCount)
{
QueueOptions args;
args.setSizePolicy(RING, 0, 5);
SessionFixture f;
std::string q("my-ring-queue");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
for (int i = 0; i < 10; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
client::Message msg;
for (int i = 5; i < 10; i++) {
BOOST_CHECK(f.subs.get(msg, q, qpid::sys::TIME_SEC));
BOOST_CHECK_EQUAL((boost::format("%1%_%2%") % "Message" % (i+1)).str(), msg.getData());
}
BOOST_CHECK(!f.subs.get(msg, q));
for (int i = 10; i < 20; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
for (int i = 15; i < 20; i++) {
BOOST_CHECK(f.subs.get(msg, q, qpid::sys::TIME_SEC));
BOOST_CHECK_EQUAL((boost::format("%1%_%2%") % "Message" % (i+1)).str(), msg.getData());
}
BOOST_CHECK(!f.subs.get(msg, q));
}
QPID_AUTO_TEST_CASE(testRingPolicySize)
{
std::string hundredBytes = std::string(100, 'h');
std::string fourHundredBytes = std::string (400, 'f');
std::string thousandBytes = std::string(1000, 't');
// Ring queue, 500 bytes maxSize
QueueOptions args;
args.setSizePolicy(RING, 500, 0);
SessionFixture f;
std::string q("my-ring-queue");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
// A. Send messages 0 .. 5, each 100 bytes
client::Message m(hundredBytes, q);
for (int i = 0; i < 6; i++) {
std::stringstream id;
id << i;
m.getMessageProperties().setCorrelationId(id.str());
f.session.messageTransfer(arg::content=m);
}
// should find 1 .. 5 on the queue, 0 is displaced by 5
client::Message msg;
for (int i = 1; i < 6; i++) {
std::stringstream id;
id << i;
BOOST_CHECK(f.subs.get(msg, q, qpid::sys::TIME_SEC));
BOOST_CHECK_EQUAL(msg.getMessageProperties().getCorrelationId(), id.str());
}
BOOST_CHECK(!f.subs.get(msg, q));
// B. Now make sure that one 400 byte message displaces four 100 byte messages
// Send messages 0 .. 5, each 100 bytes
for (int i = 0; i < 6; i++) {
client::Message m(hundredBytes, q);
std::stringstream id;
id << i;
m.getMessageProperties().setCorrelationId(id.str());
f.session.messageTransfer(arg::content=m);
}
// Now send one 400 byte message
client::Message m2(fourHundredBytes, q);
m2.getMessageProperties().setCorrelationId("6");
f.session.messageTransfer(arg::content=m2);
// expect to see 5, 6 on the queue
for (int i = 5; i < 7; i++) {
std::stringstream id;
id << i;
BOOST_CHECK(f.subs.get(msg, q, qpid::sys::TIME_SEC));
BOOST_CHECK_EQUAL(msg.getMessageProperties().getCorrelationId(), id.str());
}
BOOST_CHECK(!f.subs.get(msg, q));
// C. Try sending a 1000-byte message, should fail - exceeds maxSize of queue
client::Message m3(thousandBytes, q);
m3.getMessageProperties().setCorrelationId("6");
try {
ScopedSuppressLogging sl;
f.session.messageTransfer(arg::content=m3);
BOOST_FAIL("Ooops - successfully added a 1000 byte message to a 512 byte ring queue ...");
}
catch (...) {
}
}
QPID_AUTO_TEST_CASE(testStrictRingPolicy)
{
QueueOptions args;
args.setSizePolicy(RING_STRICT, 0, 5);
args.setString("qpid.flow_stop_count", "0");
SessionFixture f;
std::string q("my-ring-queue");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
LocalQueue incoming;
SubscriptionSettings settings(FlowControl::unlimited());
settings.autoAck = 0; // no auto ack.
Subscription sub = f.subs.subscribe(incoming, q, settings);
for (int i = 0; i < 5; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
for (int i = 0; i < 5; i++) {
BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
}
try {
ScopedSuppressLogging sl; // Suppress messages for expected errors.
f.session.messageTransfer(arg::content=client::Message("Message_6", q));
BOOST_FAIL("expecting ResourceLimitExceededException.");
} catch (const ResourceLimitExceededException&) {}
}
QPID_AUTO_TEST_CASE(testPolicyWithDtx)
{
QueueOptions args;
args.setSizePolicy(REJECT, 0, 5);
SessionFixture f;
std::string q("my-policy-queue");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
LocalQueue incoming;
SubscriptionSettings settings(FlowControl::unlimited());
settings.autoAck = 0; // no auto ack.
Subscription sub = f.subs.subscribe(incoming, q, settings);
f.session.dtxSelect();
Xid tx1(1, "test-dtx-mgr", "tx1");
f.session.dtxStart(arg::xid=tx1);
for (int i = 0; i < 5; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
f.session.dtxEnd(arg::xid=tx1);
f.session.dtxCommit(arg::xid=tx1, arg::onePhase=true);
Xid tx2(1, "test-dtx-mgr", "tx2");
f.session.dtxStart(arg::xid=tx2);
for (int i = 0; i < 5; i++) {
BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
}
SequenceSet accepting=sub.getUnaccepted();
f.session.messageAccept(accepting);
f.session.dtxEnd(arg::xid=tx2);
f.session.dtxPrepare(arg::xid=tx2);
f.session.dtxRollback(arg::xid=tx2);
f.session.messageRelease(accepting);
Xid tx3(1, "test-dtx-mgr", "tx3");
f.session.dtxStart(arg::xid=tx3);
for (int i = 0; i < 5; i++) {
incoming.pop();
}
accepting=sub.getUnaccepted();
f.session.messageAccept(accepting);
f.session.dtxEnd(arg::xid=tx3);
f.session.dtxPrepare(arg::xid=tx3);
Session other = f.connection.newSession();
try {
ScopedSuppressLogging sl; // Suppress messages for expected errors.
other.messageTransfer(arg::content=client::Message("Message_6", q));
BOOST_FAIL("expecting ResourceLimitExceededException.");
} catch (const ResourceLimitExceededException&) {}
f.session.dtxCommit(arg::xid=tx3);
//now retry and this time should succeed
other = f.connection.newSession();
other.messageTransfer(arg::content=client::Message("Message_6", q));
}
QPID_AUTO_TEST_CASE(testFlowToDiskWithNoStore)
{
//Ensure that with no store loaded, we don't flow to disk but
//fallback to rejecting messages
QueueOptions args;
args.setSizePolicy(FLOW_TO_DISK, 0, 5);
// Disable flow control, or else we'll never hit the max limit
args.setInt(QueueFlowLimit::flowStopCountKey, 0);
SessionFixture f;
std::string q("my-queue");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
LocalQueue incoming;
SubscriptionSettings settings(FlowControl::unlimited());
settings.autoAck = 0; // no auto ack.
Subscription sub = f.subs.subscribe(incoming, q, settings);
for (int i = 0; i < 5; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
for (int i = 0; i < 5; i++) {
BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%") % "Message" % (i+1)).str());
}
try {
ScopedSuppressLogging sl; // Suppress messages for expected errors.
f.session.messageTransfer(arg::content=client::Message("Message_6", q));
BOOST_FAIL("expecting ResourceLimitExceededException.");
} catch (const ResourceLimitExceededException&) {}
}
QPID_AUTO_TEST_CASE(testPolicyFailureOnCommit)
{
QueueOptions args;
args.setSizePolicy(REJECT, 0, 5);
SessionFixture f;
std::string q("q");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
f.session.txSelect();
for (int i = 0; i < 10; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
ScopedSuppressLogging sl; // Suppress messages for expected errors.
BOOST_CHECK_THROW(f.session.txCommit(), InternalErrorException);
}
QPID_AUTO_TEST_CASE(testCapacityConversion)
{
FieldTable args;
args.setString("qpid.max_count", "5");
args.setString("qpid.flow_stop_count", "0");
SessionFixture f;
std::string q("q");
f.session.queueDeclare(arg::queue=q, arg::exclusive=true, arg::autoDelete=true, arg::arguments=args);
for (int i = 0; i < 5; i++) {
f.session.messageTransfer(arg::content=client::Message((boost::format("%1%_%2%") % "Message" % (i+1)).str(), q));
}
try {
ScopedSuppressLogging sl; // Suppress messages for expected errors.
f.session.messageTransfer(arg::content=client::Message("Message_6", q));
BOOST_FAIL("expecting ResourceLimitExceededException.");
} catch (const ResourceLimitExceededException&) {}
}
QPID_AUTO_TEST_SUITE_END()
}} // namespace qpid::tests
|