diff options
author | Gordon Sim <gsim@apache.org> | 2006-11-28 15:25:35 +0000 |
---|---|---|
committer | Gordon Sim <gsim@apache.org> | 2006-11-28 15:25:35 +0000 |
commit | 4ad5fc50f0894e219c37118252d6a618419ea212 (patch) | |
tree | b48ff062cd48b7de6b606330c5cefecf15b7691a /cpp/test | |
parent | e2665f7339231eb2d85506c86a96f0859016fa89 (diff) | |
download | qpid-python-4ad5fc50f0894e219c37118252d6a618419ea212.tar.gz |
Modifications to allow loading of message data in chunks, refragmentation of messages, plus some related refactoring and tests.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@480087 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/test')
-rw-r--r-- | cpp/test/unit/qpid/broker/InMemoryContentTest.cpp | 97 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/LazyLoadedContentTest.cpp | 122 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/MessageBuilderTest.cpp | 18 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/MessageTest.cpp | 11 |
4 files changed, 235 insertions, 13 deletions
diff --git a/cpp/test/unit/qpid/broker/InMemoryContentTest.cpp b/cpp/test/unit/qpid/broker/InMemoryContentTest.cpp new file mode 100644 index 0000000000..175ef0cf27 --- /dev/null +++ b/cpp/test/unit/qpid/broker/InMemoryContentTest.cpp @@ -0,0 +1,97 @@ +/* + * + * 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/broker/InMemoryContent.h> +#include <qpid_test_plugin.h> +#include <iostream> +#include <list> + +using std::list; +using std::string; +using boost::dynamic_pointer_cast; +using namespace qpid::broker; +using namespace qpid::framing; + +struct DummyHandler : OutputHandler{ + std::vector<AMQFrame*> frames; + + virtual void send(AMQFrame* frame){ + frames.push_back(frame); + } +}; + +class InMemoryContentTest : public CppUnit::TestCase +{ + CPPUNIT_TEST_SUITE(InMemoryContentTest); + CPPUNIT_TEST(testRefragmentation); + CPPUNIT_TEST_SUITE_END(); + +public: + void testRefragmentation() + { + {//no remainder + string out[] = {"abcde", "fghij", "klmno", "pqrst"}; + string in[] = {out[0] + out[1], out[2] + out[3]}; + refragment(2, in, 4, out); + } + {//remainder for last frame + string out[] = {"abcde", "fghij", "klmno", "pqrst", "uvw"}; + string in[] = {out[0] + out[1], out[2] + out[3] + out[4]}; + refragment(2, in, 5, out); + } + } + + + void refragment(size_t inCount, string* in, size_t outCount, string* out, u_int32_t framesize = 5) + { + InMemoryContent content; + DummyHandler handler; + u_int16_t channel = 3; + + addframes(content, inCount, in); + content.send(&handler, channel, framesize); + check(handler, channel, outCount, out); + } + + void addframes(InMemoryContent& content, size_t frameCount, string* frameData) + { + for (unsigned int i = 0; i < frameCount; i++) { + AMQContentBody::shared_ptr frame(new AMQContentBody(frameData[i])); + content.add(frame); + } + } + + void check(DummyHandler& handler, u_int16_t channel, size_t expectedChunkCount, string* expectedChunks) + { + CPPUNIT_ASSERT_EQUAL(expectedChunkCount, handler.frames.size()); + + for (unsigned int i = 0; i < expectedChunkCount; i++) { + AMQContentBody::shared_ptr chunk(dynamic_pointer_cast<AMQContentBody, AMQBody>(handler.frames[i]->getBody())); + CPPUNIT_ASSERT(chunk); + CPPUNIT_ASSERT_EQUAL(expectedChunks[i], chunk->getData()); + CPPUNIT_ASSERT_EQUAL(channel, handler.frames[i]->getChannel()); + } + } +}; + +// Make this test suite a plugin. +CPPUNIT_PLUGIN_IMPLEMENT(); +CPPUNIT_TEST_SUITE_REGISTRATION(InMemoryContentTest); + diff --git a/cpp/test/unit/qpid/broker/LazyLoadedContentTest.cpp b/cpp/test/unit/qpid/broker/LazyLoadedContentTest.cpp new file mode 100644 index 0000000000..4d267887ba --- /dev/null +++ b/cpp/test/unit/qpid/broker/LazyLoadedContentTest.cpp @@ -0,0 +1,122 @@ +/* + * + * 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/broker/LazyLoadedContent.h> +#include <qpid/broker/NullMessageStore.h> +#include <qpid_test_plugin.h> +#include <iostream> +#include <list> +#include <sstream> + +using std::list; +using std::string; +using boost::dynamic_pointer_cast; +using namespace qpid::broker; +using namespace qpid::framing; + +struct DummyHandler : OutputHandler{ + std::vector<AMQFrame*> frames; + + virtual void send(AMQFrame* frame){ + frames.push_back(frame); + } +}; + + +class LazyLoadedContentTest : public CppUnit::TestCase +{ + CPPUNIT_TEST_SUITE(LazyLoadedContentTest); + CPPUNIT_TEST(testFragmented); + CPPUNIT_TEST(testWhole); + CPPUNIT_TEST(testHalved); + CPPUNIT_TEST_SUITE_END(); + + class TestMessageStore : public NullMessageStore + { + const string content; + + public: + TestMessageStore(const string& _content) : content(_content) {} + + void loadContent(u_int64_t, string& data, u_int64_t offset, u_int32_t length) + { + if (offset + length <= content.size()) { + data = content.substr(offset, length); + } else{ + std::stringstream error; + error << "Invalid segment: offset=" << offset << ", length=" << length << ", content_length=" << content.size(); + throw qpid::Exception(error.str()); + } + } + }; + + +public: + void testFragmented() + { + string data = "abcdefghijklmnopqrstuvwxyz"; + u_int32_t framesize = 5; + string out[] = {"abcde", "fghij", "klmno", "pqrst", "uvwxy", "z"}; + load(data, 6, out, framesize); + } + + void testWhole() + { + string data = "abcdefghijklmnopqrstuvwxyz"; + u_int32_t framesize = 50; + string out[] = {data}; + load(data, 1, out, framesize); + } + + void testHalved() + { + string data = "abcdefghijklmnopqrstuvwxyz"; + u_int32_t framesize = 13; + string out[] = {"abcdefghijklm", "nopqrstuvwxyz"}; + load(data, 2, out, framesize); + } + + void load(string& in, size_t outCount, string* out, u_int32_t framesize) + { + TestMessageStore store(in); + LazyLoadedContent content(&store, 1, in.size()); + DummyHandler handler; + u_int16_t channel = 3; + content.send(&handler, channel, framesize); + check(handler, channel, outCount, out); + } + + void check(DummyHandler& handler, u_int16_t channel, size_t expectedChunkCount, string* expectedChunks) + { + CPPUNIT_ASSERT_EQUAL(expectedChunkCount, handler.frames.size()); + + for (unsigned int i = 0; i < expectedChunkCount; i++) { + AMQContentBody::shared_ptr chunk(dynamic_pointer_cast<AMQContentBody, AMQBody>(handler.frames[i]->getBody())); + CPPUNIT_ASSERT(chunk); + CPPUNIT_ASSERT_EQUAL(expectedChunks[i], chunk->getData()); + CPPUNIT_ASSERT_EQUAL(channel, handler.frames[i]->getChannel()); + } + } +}; + +// Make this test suite a plugin. +CPPUNIT_PLUGIN_IMPLEMENT(); +CPPUNIT_TEST_SUITE_REGISTRATION(LazyLoadedContentTest); + diff --git a/cpp/test/unit/qpid/broker/MessageBuilderTest.cpp b/cpp/test/unit/qpid/broker/MessageBuilderTest.cpp index a5f7911fc8..fa80f8f939 100644 --- a/cpp/test/unit/qpid/broker/MessageBuilderTest.cpp +++ b/cpp/test/unit/qpid/broker/MessageBuilderTest.cpp @@ -56,13 +56,19 @@ class MessageBuilderTest : public CppUnit::TestCase header = new Buffer(msg->encodedHeaderSize()); msg->encodeHeader(*header); content = new Buffer(contentBufferSize); - msg->encodeContent(*content); - } else if (!header || !content) { - throw qpid::Exception("Buffers not initialised!"); + msg->setPersistenceId(1); } else { - msg->encodeContent(*content); + throw qpid::Exception("Message already staged!"); + } + } + + void appendContent(u_int64_t msgId, const string& data) + { + if (msgId == 1) { + content->putRawData(data); + } else { + throw qpid::Exception("Invalid message id!"); } - msg->setPersistenceId(1); } Message::shared_ptr getRestoredMessage() @@ -159,7 +165,7 @@ class MessageBuilderTest : public CppUnit::TestCase void testStaging(){ DummyHandler handler; - TestMessageStore store(50);//more than enough for two frames of 14 bytes + TestMessageStore store(14); MessageBuilder builder(&handler, &store, 5); string data1("abcdefg"); diff --git a/cpp/test/unit/qpid/broker/MessageTest.cpp b/cpp/test/unit/qpid/broker/MessageTest.cpp index ec724894a5..b497588c6c 100644 --- a/cpp/test/unit/qpid/broker/MessageTest.cpp +++ b/cpp/test/unit/qpid/broker/MessageTest.cpp @@ -77,13 +77,10 @@ class MessageTest : public CppUnit::TestCase DummyHandler handler; msg->deliver(&handler, 0, "ignore", 0, 100); - CPPUNIT_ASSERT_EQUAL((size_t) 4, handler.frames.size()); - AMQContentBody::shared_ptr contentBody1(dynamic_pointer_cast<AMQContentBody, AMQBody>(handler.frames[2]->getBody())); - AMQContentBody::shared_ptr contentBody2(dynamic_pointer_cast<AMQContentBody, AMQBody>(handler.frames[3]->getBody())); - CPPUNIT_ASSERT(contentBody1); - CPPUNIT_ASSERT(contentBody2); - CPPUNIT_ASSERT_EQUAL(data1, contentBody1->getData()); - CPPUNIT_ASSERT_EQUAL(data2, contentBody2->getData()); + CPPUNIT_ASSERT_EQUAL((size_t) 3, handler.frames.size()); + AMQContentBody::shared_ptr contentBody(dynamic_pointer_cast<AMQContentBody, AMQBody>(handler.frames[2]->getBody())); + CPPUNIT_ASSERT(contentBody); + CPPUNIT_ASSERT_EQUAL(data1 + data2, contentBody->getData()); } }; |