You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by hc...@apache.org on 2014/11/18 11:33:55 UTC
[03/37] thrift git commit: Revert "THRIFT-2729: C++ - .clang-format
created and applied"
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/TransportTest.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp
old mode 100644
new mode 100755
index 0305732..c1cb976
--- a/lib/cpp/test/TransportTest.cpp
+++ b/lib/cpp/test/TransportTest.cpp
@@ -36,9 +36,10 @@
#include <thrift/concurrency/FunctionRunner.h>
#if _WIN32
-#include <thrift/windows/TWinsockSingleton.h>
+ #include <thrift/windows/TWinsockSingleton.h>
#endif
+
using namespace apache::thrift::transport;
static boost::mt19937 rng;
@@ -48,14 +49,14 @@ void initrand(unsigned int seed) {
}
class SizeGenerator {
-public:
+ public:
virtual ~SizeGenerator() {}
virtual uint32_t nextSize() = 0;
virtual std::string describe() const = 0;
};
class ConstantSizeGenerator : public SizeGenerator {
-public:
+ public:
ConstantSizeGenerator(uint32_t value) : value_(value) {}
uint32_t nextSize() { return value_; }
std::string describe() const {
@@ -64,14 +65,14 @@ public:
return desc.str();
}
-private:
+ private:
uint32_t value_;
};
class RandomSizeGenerator : public SizeGenerator {
-public:
- RandomSizeGenerator(uint32_t min, uint32_t max)
- : generator_(rng, boost::uniform_int<int>(min, max)) {}
+ public:
+ RandomSizeGenerator(uint32_t min, uint32_t max) :
+ generator_(rng, boost::uniform_int<int>(min, max)) {}
uint32_t nextSize() { return generator_(); }
@@ -84,8 +85,9 @@ public:
uint32_t getMin() const { return (generator_.distribution().min)(); }
uint32_t getMax() const { return (generator_.distribution().max)(); }
-private:
- boost::variate_generator<boost::mt19937&, boost::uniform_int<int> > generator_;
+ private:
+ boost::variate_generator< boost::mt19937&, boost::uniform_int<int> >
+ generator_;
};
/**
@@ -96,15 +98,16 @@ private:
* to make a copy of the generator to bind it to the test function.)
*/
class GenericSizeGenerator : public SizeGenerator {
-public:
- GenericSizeGenerator(uint32_t value) : generator_(new ConstantSizeGenerator(value)) {}
- GenericSizeGenerator(uint32_t min, uint32_t max)
- : generator_(new RandomSizeGenerator(min, max)) {}
+ public:
+ GenericSizeGenerator(uint32_t value) :
+ generator_(new ConstantSizeGenerator(value)) {}
+ GenericSizeGenerator(uint32_t min, uint32_t max) :
+ generator_(new RandomSizeGenerator(min, max)) {}
uint32_t nextSize() { return generator_->nextSize(); }
std::string describe() const { return generator_->describe(); }
-private:
+ private:
boost::shared_ptr<SizeGenerator> generator_;
};
@@ -122,7 +125,7 @@ private:
*/
template <class Transport_>
class CoupledTransports {
-public:
+ public:
virtual ~CoupledTransports() {}
typedef Transport_ TransportType;
@@ -131,17 +134,18 @@ public:
boost::shared_ptr<Transport_> in;
boost::shared_ptr<Transport_> out;
-private:
+ private:
CoupledTransports(const CoupledTransports&);
- CoupledTransports& operator=(const CoupledTransports&);
+ CoupledTransports &operator=(const CoupledTransports&);
};
/**
* Coupled TMemoryBuffers
*/
class CoupledMemoryBuffers : public CoupledTransports<TMemoryBuffer> {
-public:
- CoupledMemoryBuffers() : buf(new TMemoryBuffer) {
+ public:
+ CoupledMemoryBuffers() :
+ buf(new TMemoryBuffer) {
in = buf;
out = buf;
}
@@ -155,7 +159,7 @@ public:
*/
template <class WrapperTransport_, class InnerCoupledTransports_>
class CoupledWrapperTransportsT : public CoupledTransports<WrapperTransport_> {
-public:
+ public:
CoupledWrapperTransportsT() {
if (inner_.in) {
this->in.reset(new WrapperTransport_(inner_.in));
@@ -172,27 +176,34 @@ public:
* Coupled TBufferedTransports.
*/
template <class InnerTransport_>
-class CoupledBufferedTransportsT
- : public CoupledWrapperTransportsT<TBufferedTransport, InnerTransport_> {};
+class CoupledBufferedTransportsT :
+ public CoupledWrapperTransportsT<TBufferedTransport, InnerTransport_> {
+};
-typedef CoupledBufferedTransportsT<CoupledMemoryBuffers> CoupledBufferedTransports;
+typedef CoupledBufferedTransportsT<CoupledMemoryBuffers>
+ CoupledBufferedTransports;
/**
* Coupled TFramedTransports.
*/
template <class InnerTransport_>
-class CoupledFramedTransportsT
- : public CoupledWrapperTransportsT<TFramedTransport, InnerTransport_> {};
+class CoupledFramedTransportsT :
+ public CoupledWrapperTransportsT<TFramedTransport, InnerTransport_> {
+};
-typedef CoupledFramedTransportsT<CoupledMemoryBuffers> CoupledFramedTransports;
+typedef CoupledFramedTransportsT<CoupledMemoryBuffers>
+ CoupledFramedTransports;
/**
* Coupled TZlibTransports.
*/
template <class InnerTransport_>
-class CoupledZlibTransportsT : public CoupledWrapperTransportsT<TZlibTransport, InnerTransport_> {};
+class CoupledZlibTransportsT :
+ public CoupledWrapperTransportsT<TZlibTransport, InnerTransport_> {
+};
-typedef CoupledZlibTransportsT<CoupledMemoryBuffers> CoupledZlibTransports;
+typedef CoupledZlibTransportsT<CoupledMemoryBuffers>
+ CoupledZlibTransports;
#ifndef _WIN32
// FD transport doesn't make much sense on Windows.
@@ -200,7 +211,7 @@ typedef CoupledZlibTransportsT<CoupledMemoryBuffers> CoupledZlibTransports;
* Coupled TFDTransports.
*/
class CoupledFDTransports : public CoupledTransports<TFDTransport> {
-public:
+ public:
CoupledFDTransports() {
int pipes[2];
@@ -218,7 +229,7 @@ public:
* Coupled TSockets
*/
class CoupledSocketTransports : public CoupledTransports<TSocket> {
-public:
+ public:
CoupledSocketTransports() {
THRIFT_SOCKET sockets[2] = {0};
if (THRIFT_SOCKETPAIR(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
@@ -231,34 +242,39 @@ public:
}
};
-// These could be made to work on Windows, but I don't care enough to make it happen
+//These could be made to work on Windows, but I don't care enough to make it happen
#ifndef _WIN32
/**
* Coupled TFileTransports
*/
class CoupledFileTransports : public CoupledTransports<TFileTransport> {
-public:
+ public:
CoupledFileTransports() {
#ifndef _WIN32
const char* tmp_dir = "/tmp";
-#define FILENAME_SUFFIX "/thrift.transport_test"
+ #define FILENAME_SUFFIX "/thrift.transport_test"
#else
const char* tmp_dir = getenv("TMP");
-#define FILENAME_SUFFIX "\\thrift.transport_test"
+ #define FILENAME_SUFFIX "\\thrift.transport_test"
#endif
// Create a temporary file to use
filename.resize(strlen(tmp_dir) + strlen(FILENAME_SUFFIX));
- THRIFT_SNPRINTF(&filename[0], filename.size(), "%s" FILENAME_SUFFIX, tmp_dir);
-#undef FILENAME_SUFFIX
+ THRIFT_SNPRINTF(&filename[0], filename.size(),
+ "%s" FILENAME_SUFFIX, tmp_dir);
+ #undef FILENAME_SUFFIX
- { std::ofstream dummy_creation(filename.c_str(), std::ofstream::trunc); }
+ {
+ std::ofstream dummy_creation(filename.c_str(), std::ofstream::trunc);
+ }
in.reset(new TFileTransport(filename, true));
out.reset(new TFileTransport(filename));
}
- ~CoupledFileTransports() { remove(filename.c_str()); }
+ ~CoupledFileTransports() {
+ remove(filename.c_str());
+ }
std::string filename;
};
@@ -274,7 +290,7 @@ public:
*/
template <class CoupledTransports_>
class CoupledTTransports : public CoupledTransports<TTransport> {
-public:
+ public:
CoupledTTransports() : transports() {
in = transports.in;
out = transports.out;
@@ -292,7 +308,7 @@ public:
*/
template <class CoupledTransports_>
class CoupledBufferBases : public CoupledTransports<TBufferBase> {
-public:
+ public:
CoupledBufferBases() : transports() {
in = transports.in;
out = transports.out;
@@ -316,8 +332,12 @@ public:
**************************************************************************/
struct TriggerInfo {
- TriggerInfo(int seconds, const boost::shared_ptr<TTransport>& transport, uint32_t writeLength)
- : timeoutSeconds(seconds), transport(transport), writeLength(writeLength), next(NULL) {}
+ TriggerInfo(int seconds, const boost::shared_ptr<TTransport>& transport,
+ uint32_t writeLength) :
+ timeoutSeconds(seconds),
+ transport(transport),
+ writeLength(writeLength),
+ next(NULL) {}
int timeoutSeconds;
boost::shared_ptr<TTransport> transport;
@@ -331,7 +351,7 @@ unsigned int g_numTriggersFired;
bool g_teardown = false;
void alarm_handler() {
- TriggerInfo* info = NULL;
+ TriggerInfo *info = NULL;
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
// The alarm timed out, which almost certainly means we're stuck
@@ -363,26 +383,26 @@ void alarm_handler() {
}
void alarm_handler_wrapper() {
- int64_t timeout = 0; // timeout of 0 means wait forever
- while (true) {
+ int64_t timeout = 0; //timeout of 0 means wait forever
+ while(true) {
bool fireHandler = false;
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
- if (g_teardown)
- return;
- // calculate timeout
+ if(g_teardown)
+ return;
+ //calculate timeout
if (g_triggerInfo == NULL) {
timeout = 0;
} else {
- timeout = g_triggerInfo->timeoutSeconds * 1000;
+ timeout = g_triggerInfo->timeoutSeconds * 1000;
}
int waitResult = g_alarm_monitor.waitForTimeRelative(timeout);
- if (waitResult == THRIFT_ETIMEDOUT)
+ if(waitResult == THRIFT_ETIMEDOUT)
fireHandler = true;
}
- if (fireHandler)
- alarm_handler(); // calling outside the lock
+ if(fireHandler)
+ alarm_handler(); //calling outside the lock
}
}
@@ -395,7 +415,7 @@ void alarm_handler_wrapper() {
* to the end.)
*/
void add_trigger(unsigned int seconds,
- const boost::shared_ptr<TTransport>& transport,
+ const boost::shared_ptr<TTransport> &transport,
uint32_t write_len) {
TriggerInfo* info = new TriggerInfo(seconds, transport, write_len);
{
@@ -417,7 +437,7 @@ void add_trigger(unsigned int seconds,
}
void clear_triggers() {
- TriggerInfo* info = NULL;
+ TriggerInfo *info = NULL;
{
apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
@@ -435,7 +455,7 @@ void clear_triggers() {
}
void set_trigger(unsigned int seconds,
- const boost::shared_ptr<TTransport>& transport,
+ const boost::shared_ptr<TTransport> &transport,
uint32_t write_len) {
clear_triggers();
add_trigger(seconds, transport, write_len);
@@ -479,8 +499,10 @@ void test_rw(uint32_t totalSize,
BOOST_REQUIRE(transports.in != NULL);
BOOST_REQUIRE(transports.out != NULL);
- boost::shared_array<uint8_t> wbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]);
- boost::shared_array<uint8_t> rbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]);
+ boost::shared_array<uint8_t> wbuf =
+ boost::shared_array<uint8_t>(new uint8_t[totalSize]);
+ boost::shared_array<uint8_t> rbuf =
+ boost::shared_array<uint8_t>(new uint8_t[totalSize]);
// store some data in wbuf
for (uint32_t n = 0; n < totalSize; ++n) {
@@ -500,7 +522,8 @@ void test_rw(uint32_t totalSize,
// Make sure (total_written - total_read) + wchunk_size
// is less than maxOutstanding
- if (maxOutstanding > 0 && wchunk_size > maxOutstanding - (total_written - total_read)) {
+ if (maxOutstanding > 0 &&
+ wchunk_size > maxOutstanding - (total_written - total_read)) {
wchunk_size = maxOutstanding - (total_written - total_read);
}
@@ -514,7 +537,8 @@ void test_rw(uint32_t totalSize,
try {
transports.out->write(wbuf.get() + total_written, write_size);
- } catch (TTransportException& te) {
+ }
+ catch (TTransportException & te) {
if (te.getType() == TTransportException::TIMED_OUT)
break;
throw te;
@@ -548,15 +572,17 @@ void test_rw(uint32_t totalSize,
try {
bytes_read = transports.in->read(rbuf.get() + total_read, read_size);
} catch (TTransportException& e) {
- BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size << ") threw exception \""
- << e.what() << "\"; written so far: " << total_written << " / "
- << totalSize << " bytes");
+ BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size <<
+ ") threw exception \"" << e.what() <<
+ "\"; written so far: " << total_written << " / " <<
+ totalSize << " bytes");
}
BOOST_REQUIRE_MESSAGE(bytes_read > 0,
- "read(pos=" << total_read << ", size=" << read_size << ") returned "
- << bytes_read << "; written so far: " << total_written
- << " / " << totalSize << " bytes");
+ "read(pos=" << total_read << ", size=" <<
+ read_size << ") returned " << bytes_read <<
+ "; written so far: " << total_written << " / " <<
+ totalSize << " bytes");
chunk_read += bytes_read;
total_read += bytes_read;
}
@@ -566,6 +592,7 @@ void test_rw(uint32_t totalSize,
BOOST_CHECK_EQUAL(memcmp(rbuf.get(), wbuf.get(), totalSize), 0);
}
+
template <class CoupledTransports>
void test_read_part_available() {
CoupledTransports transports;
@@ -582,8 +609,8 @@ void test_read_part_available() {
transports.out->flush();
set_trigger(3, transports.out, 1);
uint32_t bytes_read = transports.in->read(read_buf, 10);
- BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
- BOOST_CHECK_EQUAL(bytes_read, (uint32_t)9);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
+ BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 9);
clear_triggers();
}
@@ -649,7 +676,7 @@ void test_read_partial_midframe() {
// Now read 4 bytes, so that we are partway through the written data.
uint32_t bytes_read = transports.in->read(read_buf, 4);
- BOOST_CHECK_EQUAL(bytes_read, (uint32_t)4);
+ BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 4);
// Now attempt to read 10 bytes. Only 9 more are available.
//
@@ -662,13 +689,13 @@ void test_read_partial_midframe() {
while (total_read < 9) {
set_trigger(3, transports.out, 1);
bytes_read = transports.in->read(read_buf, 10);
- BOOST_REQUIRE_EQUAL(g_numTriggersFired, (unsigned int)0);
- BOOST_REQUIRE_GT(bytes_read, (uint32_t)0);
+ BOOST_REQUIRE_EQUAL(g_numTriggersFired, (unsigned int) 0);
+ BOOST_REQUIRE_GT(bytes_read, (uint32_t) 0);
total_read += bytes_read;
- BOOST_REQUIRE_LE(total_read, (uint32_t)9);
+ BOOST_REQUIRE_LE(total_read, (uint32_t) 9);
}
- BOOST_CHECK_EQUAL(total_read, (uint32_t)9);
+ BOOST_CHECK_EQUAL(total_read, (uint32_t) 9);
clear_triggers();
}
@@ -690,7 +717,7 @@ void test_borrow_part_available() {
set_trigger(3, transports.out, 1);
uint32_t borrow_len = 10;
const uint8_t* borrowed_buf = transports.in->borrow(read_buf, &borrow_len);
- BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
BOOST_CHECK(borrowed_buf == NULL);
clear_triggers();
@@ -716,11 +743,11 @@ void test_read_none_available() {
add_trigger(1, transports.out, 8);
uint32_t bytes_read = transports.in->read(read_buf, 10);
if (bytes_read == 0) {
- BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
clear_triggers();
} else {
- BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)1);
- BOOST_CHECK_EQUAL(bytes_read, (uint32_t)2);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 1);
+ BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 2);
}
clear_triggers();
@@ -740,7 +767,7 @@ void test_borrow_none_available() {
uint32_t borrow_len = 10;
const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len);
BOOST_CHECK(borrowed_buf == NULL);
- BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
clear_triggers();
}
@@ -761,40 +788,47 @@ void test_borrow_none_available() {
* is compiler-dependent. gcc returns mangled names.)
**************************************************************************/
-#define ADD_TEST_RW(CoupledTransports, totalSize, ...) \
- addTestRW<CoupledTransports>(BOOST_STRINGIZE(CoupledTransports), totalSize, ##__VA_ARGS__);
-
-#define TEST_RW(CoupledTransports, totalSize, ...) \
- do { \
- /* Add the test as specified, to test the non-virtual function calls */ \
- ADD_TEST_RW(CoupledTransports, totalSize, ##__VA_ARGS__); \
- /* \
- * Also test using the transport as a TTransport*, to test \
- * the read_virt()/write_virt() calls \
- */ \
- ADD_TEST_RW(CoupledTTransports<CoupledTransports>, totalSize, ##__VA_ARGS__); \
- /* Test wrapping the transport with TBufferedTransport */ \
- ADD_TEST_RW(CoupledBufferedTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \
- /* Test wrapping the transport with TFramedTransports */ \
- ADD_TEST_RW(CoupledFramedTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \
- /* Test wrapping the transport with TZlibTransport */ \
- ADD_TEST_RW(CoupledZlibTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \
+#define ADD_TEST_RW(CoupledTransports, totalSize, ...) \
+ addTestRW< CoupledTransports >(BOOST_STRINGIZE(CoupledTransports), \
+ totalSize, ## __VA_ARGS__);
+
+#define TEST_RW(CoupledTransports, totalSize, ...) \
+ do { \
+ /* Add the test as specified, to test the non-virtual function calls */ \
+ ADD_TEST_RW(CoupledTransports, totalSize, ## __VA_ARGS__); \
+ /* \
+ * Also test using the transport as a TTransport*, to test \
+ * the read_virt()/write_virt() calls \
+ */ \
+ ADD_TEST_RW(CoupledTTransports<CoupledTransports>, \
+ totalSize, ## __VA_ARGS__); \
+ /* Test wrapping the transport with TBufferedTransport */ \
+ ADD_TEST_RW(CoupledBufferedTransportsT<CoupledTransports>, \
+ totalSize, ## __VA_ARGS__); \
+ /* Test wrapping the transport with TFramedTransports */ \
+ ADD_TEST_RW(CoupledFramedTransportsT<CoupledTransports>, \
+ totalSize, ## __VA_ARGS__); \
+ /* Test wrapping the transport with TZlibTransport */ \
+ ADD_TEST_RW(CoupledZlibTransportsT<CoupledTransports>, \
+ totalSize, ## __VA_ARGS__); \
} while (0)
-#define ADD_TEST_BLOCKING(CoupledTransports) \
- addTestBlocking<CoupledTransports>(BOOST_STRINGIZE(CoupledTransports));
+#define ADD_TEST_BLOCKING(CoupledTransports) \
+ addTestBlocking< CoupledTransports >(BOOST_STRINGIZE(CoupledTransports));
-#define TEST_BLOCKING_BEHAVIOR(CoupledTransports) \
- ADD_TEST_BLOCKING(CoupledTransports); \
- ADD_TEST_BLOCKING(CoupledTTransports<CoupledTransports>); \
- ADD_TEST_BLOCKING(CoupledBufferedTransportsT<CoupledTransports>); \
- ADD_TEST_BLOCKING(CoupledFramedTransportsT<CoupledTransports>); \
+#define TEST_BLOCKING_BEHAVIOR(CoupledTransports) \
+ ADD_TEST_BLOCKING(CoupledTransports); \
+ ADD_TEST_BLOCKING(CoupledTTransports<CoupledTransports>); \
+ ADD_TEST_BLOCKING(CoupledBufferedTransportsT<CoupledTransports>); \
+ ADD_TEST_BLOCKING(CoupledFramedTransportsT<CoupledTransports>); \
ADD_TEST_BLOCKING(CoupledZlibTransportsT<CoupledTransports>);
class TransportTestGen {
-public:
- TransportTestGen(boost::unit_test::test_suite* suite, float sizeMultiplier)
- : suite_(suite), sizeMultiplier_(sizeMultiplier) {}
+ public:
+ TransportTestGen(boost::unit_test::test_suite* suite,
+ float sizeMultiplier) :
+ suite_(suite),
+ sizeMultiplier_(sizeMultiplier) {}
void generate() {
GenericSizeGenerator rand4k(1, 4096);
@@ -805,15 +839,15 @@ public:
*/
// TMemoryBuffer tests
- TEST_RW(CoupledMemoryBuffers, 1024 * 1024, 0, 0);
- TEST_RW(CoupledMemoryBuffers, 1024 * 256, rand4k, rand4k);
- TEST_RW(CoupledMemoryBuffers, 1024 * 256, 167, 163);
- TEST_RW(CoupledMemoryBuffers, 1024 * 16, 1, 1);
+ TEST_RW(CoupledMemoryBuffers, 1024*1024, 0, 0);
+ TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k);
+ TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163);
+ TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1);
- TEST_RW(CoupledMemoryBuffers, 1024 * 256, 0, 0, rand4k, rand4k);
- TEST_RW(CoupledMemoryBuffers, 1024 * 256, rand4k, rand4k, rand4k, rand4k);
- TEST_RW(CoupledMemoryBuffers, 1024 * 256, 167, 163, rand4k, rand4k);
- TEST_RW(CoupledMemoryBuffers, 1024 * 16, 1, 1, rand4k, rand4k);
+ TEST_RW(CoupledMemoryBuffers, 1024*256, 0, 0, rand4k, rand4k);
+ TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k, rand4k, rand4k);
+ TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163, rand4k, rand4k);
+ TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1, rand4k, rand4k);
TEST_BLOCKING_BEHAVIOR(CoupledMemoryBuffers);
@@ -822,57 +856,68 @@ public:
// Since CoupledFDTransports tests with a pipe, writes will block
// if there is too much outstanding unread data in the pipe.
uint32_t fd_max_outstanding = 4096;
- TEST_RW(CoupledFDTransports, 1024 * 1024, 0, 0, 0, 0, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 256, rand4k, rand4k, 0, 0, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 256, 167, 163, 0, 0, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 16, 1, 1, 0, 0, fd_max_outstanding);
-
- TEST_RW(CoupledFDTransports, 1024 * 256, 0, 0, rand4k, rand4k, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 256, rand4k, rand4k, rand4k, rand4k, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 256, 167, 163, rand4k, rand4k, fd_max_outstanding);
- TEST_RW(CoupledFDTransports, 1024 * 16, 1, 1, rand4k, rand4k, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*1024, 0, 0,
+ 0, 0, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k,
+ 0, 0, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*256, 167, 163,
+ 0, 0, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*16, 1, 1,
+ 0, 0, fd_max_outstanding);
+
+ TEST_RW(CoupledFDTransports, 1024*256, 0, 0,
+ rand4k, rand4k, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k,
+ rand4k, rand4k, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*256, 167, 163,
+ rand4k, rand4k, fd_max_outstanding);
+ TEST_RW(CoupledFDTransports, 1024*16, 1, 1,
+ rand4k, rand4k, fd_max_outstanding);
TEST_BLOCKING_BEHAVIOR(CoupledFDTransports);
#endif //_WIN32
// TSocket tests
uint32_t socket_max_outstanding = 4096;
- TEST_RW(CoupledSocketTransports, 1024 * 1024, 0, 0, 0, 0, socket_max_outstanding);
- TEST_RW(CoupledSocketTransports, 1024 * 256, rand4k, rand4k, 0, 0, socket_max_outstanding);
- TEST_RW(CoupledSocketTransports, 1024 * 256, 167, 163, 0, 0, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*1024, 0, 0,
+ 0, 0, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k,
+ 0, 0, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*256, 167, 163,
+ 0, 0, socket_max_outstanding);
// Doh. Apparently writing to a socket has some additional overhead for
// each send() call. If we have more than ~400 outstanding 1-byte write
// requests, additional send() calls start blocking.
- TEST_RW(CoupledSocketTransports, 1024 * 16, 1, 1, 0, 0, socket_max_outstanding);
- TEST_RW(CoupledSocketTransports, 1024 * 256, 0, 0, rand4k, rand4k, socket_max_outstanding);
- TEST_RW(CoupledSocketTransports,
- 1024 * 256,
- rand4k,
- rand4k,
- rand4k,
- rand4k,
- socket_max_outstanding);
- TEST_RW(CoupledSocketTransports, 1024 * 256, 167, 163, rand4k, rand4k, socket_max_outstanding);
- TEST_RW(CoupledSocketTransports, 1024 * 16, 1, 1, rand4k, rand4k, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*16, 1, 1,
+ 0, 0, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*256, 0, 0,
+ rand4k, rand4k, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k,
+ rand4k, rand4k, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*256, 167, 163,
+ rand4k, rand4k, socket_max_outstanding);
+ TEST_RW(CoupledSocketTransports, 1024*16, 1, 1,
+ rand4k, rand4k, socket_max_outstanding);
TEST_BLOCKING_BEHAVIOR(CoupledSocketTransports);
-// These could be made to work on Windows, but I don't care enough to make it happen
+//These could be made to work on Windows, but I don't care enough to make it happen
#ifndef _WIN32
// TFileTransport tests
// We use smaller buffer sizes here, since TFileTransport is fairly slow.
//
// TFileTransport can't write more than 16MB at once
- uint32_t max_write_at_once = 1024 * 1024 * 16 - 4;
- TEST_RW(CoupledFileTransports, 1024 * 1024, max_write_at_once, 0);
- TEST_RW(CoupledFileTransports, 1024 * 128, rand4k, rand4k);
- TEST_RW(CoupledFileTransports, 1024 * 128, 167, 163);
- TEST_RW(CoupledFileTransports, 1024 * 2, 1, 1);
-
- TEST_RW(CoupledFileTransports, 1024 * 64, 0, 0, rand4k, rand4k);
- TEST_RW(CoupledFileTransports, 1024 * 64, rand4k, rand4k, rand4k, rand4k);
- TEST_RW(CoupledFileTransports, 1024 * 64, 167, 163, rand4k, rand4k);
- TEST_RW(CoupledFileTransports, 1024 * 2, 1, 1, rand4k, rand4k);
+ uint32_t max_write_at_once = 1024*1024*16 - 4;
+ TEST_RW(CoupledFileTransports, 1024*1024, max_write_at_once, 0);
+ TEST_RW(CoupledFileTransports, 1024*128, rand4k, rand4k);
+ TEST_RW(CoupledFileTransports, 1024*128, 167, 163);
+ TEST_RW(CoupledFileTransports, 1024*2, 1, 1);
+
+ TEST_RW(CoupledFileTransports, 1024*64, 0, 0, rand4k, rand4k);
+ TEST_RW(CoupledFileTransports, 1024*64,
+ rand4k, rand4k, rand4k, rand4k);
+ TEST_RW(CoupledFileTransports, 1024*64, 167, 163, rand4k, rand4k);
+ TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k);
TEST_BLOCKING_BEHAVIOR(CoupledFileTransports);
#endif
@@ -880,45 +925,23 @@ public:
// Add some tests that access TBufferedTransport and TFramedTransport
// via TTransport pointers and TBufferBase pointers.
ADD_TEST_RW(CoupledTTransports<CoupledBufferedTransports>,
- 1024 * 1024,
- rand4k,
- rand4k,
- rand4k,
- rand4k);
+ 1024*1024, rand4k, rand4k, rand4k, rand4k);
ADD_TEST_RW(CoupledBufferBases<CoupledBufferedTransports>,
- 1024 * 1024,
- rand4k,
- rand4k,
- rand4k,
- rand4k);
+ 1024*1024, rand4k, rand4k, rand4k, rand4k);
ADD_TEST_RW(CoupledTTransports<CoupledFramedTransports>,
- 1024 * 1024,
- rand4k,
- rand4k,
- rand4k,
- rand4k);
+ 1024*1024, rand4k, rand4k, rand4k, rand4k);
ADD_TEST_RW(CoupledBufferBases<CoupledFramedTransports>,
- 1024 * 1024,
- rand4k,
- rand4k,
- rand4k,
- rand4k);
+ 1024*1024, rand4k, rand4k, rand4k, rand4k);
// Test using TZlibTransport via a TTransport pointer
ADD_TEST_RW(CoupledTTransports<CoupledZlibTransports>,
- 1024 * 1024,
- rand4k,
- rand4k,
- rand4k,
- rand4k);
+ 1024*1024, rand4k, rand4k, rand4k, rand4k);
}
-private:
+ private:
template <class CoupledTransports>
- void addTestRW(const char* transport_name,
- uint32_t totalSize,
- GenericSizeGenerator wSizeGen,
- GenericSizeGenerator rSizeGen,
+ void addTestRW(const char* transport_name, uint32_t totalSize,
+ GenericSizeGenerator wSizeGen, GenericSizeGenerator rSizeGen,
GenericSizeGenerator wChunkSizeGen = 0,
GenericSizeGenerator rChunkSizeGen = 0,
uint32_t maxOutstanding = 0,
@@ -927,50 +950,60 @@ private:
totalSize = static_cast<uint32_t>(totalSize * sizeMultiplier_);
std::ostringstream name;
- name << transport_name << "::test_rw(" << totalSize << ", " << wSizeGen.describe() << ", "
- << rSizeGen.describe() << ", " << wChunkSizeGen.describe() << ", "
- << rChunkSizeGen.describe() << ", " << maxOutstanding << ")";
-
- boost::unit_test::callback0<> test_func
- = apache::thrift::stdcxx::bind(test_rw<CoupledTransports>,
- totalSize,
- wSizeGen,
- rSizeGen,
- wChunkSizeGen,
- rChunkSizeGen,
- maxOutstanding);
- boost::unit_test::test_case* tc = boost::unit_test::make_test_case(test_func, name.str());
+ name << transport_name << "::test_rw(" << totalSize << ", " <<
+ wSizeGen.describe() << ", " << rSizeGen.describe() << ", " <<
+ wChunkSizeGen.describe() << ", " << rChunkSizeGen.describe() << ", " <<
+ maxOutstanding << ")";
+
+ boost::unit_test::callback0<> test_func =
+ apache::thrift::stdcxx::bind(test_rw<CoupledTransports>, totalSize,
+ wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen,
+ maxOutstanding);
+ boost::unit_test::test_case* tc =
+ boost::unit_test::make_test_case(test_func, name.str());
suite_->add(tc, expectedFailures);
}
template <class CoupledTransports>
- void addTestBlocking(const char* transportName, uint32_t expectedFailures = 0) {
+ void addTestBlocking(const char* transportName,
+ uint32_t expectedFailures = 0) {
char name[1024];
boost::unit_test::test_case* tc;
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available()", transportName);
- tc = boost::unit_test::make_test_case(test_read_part_available<CoupledTransports>, name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_read_part_available<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available_in_chunks()", transportName);
- tc = boost::unit_test::make_test_case(test_read_part_available_in_chunks<CoupledTransports>,
- name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available_in_chunks()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_read_part_available_in_chunks<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_partial_midframe()", transportName);
- tc = boost::unit_test::make_test_case(test_read_partial_midframe<CoupledTransports>, name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_partial_midframe()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_read_partial_midframe<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_none_available()", transportName);
- tc = boost::unit_test::make_test_case(test_read_none_available<CoupledTransports>, name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_none_available()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_read_none_available<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_part_available()", transportName);
- tc = boost::unit_test::make_test_case(test_borrow_part_available<CoupledTransports>, name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_part_available()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_borrow_part_available<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
- THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_none_available()", transportName);
- tc = boost::unit_test::make_test_case(test_borrow_none_available<CoupledTransports>, name);
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_none_available()",
+ transportName);
+ tc = boost::unit_test::make_test_case(
+ test_borrow_none_available<CoupledTransports>, name);
suite_->add(tc, expectedFailures);
}
@@ -989,15 +1022,15 @@ private:
struct global_fixture {
boost::shared_ptr<apache::thrift::concurrency::Thread> alarmThread_;
global_fixture() {
-#if _WIN32
+ #if _WIN32
apache::thrift::transport::TWinsockSingleton::create();
-#endif
+ #endif
apache::thrift::concurrency::PlatformThreadFactory factory;
factory.setDetached(false);
alarmThread_ = factory.newThread(
- apache::thrift::concurrency::FunctionRunner::create(alarm_handler_wrapper));
+ apache::thrift::concurrency::FunctionRunner::create(alarm_handler_wrapper));
alarmThread_->start();
}
~global_fixture() {
@@ -1021,7 +1054,8 @@ boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
initrand(seed);
- boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
+ boost::unit_test::test_suite* suite =
+ &boost::unit_test::framework::master_test_suite();
suite->p_name.value = "TransportTest";
TransportTestGen transport_test_generator(suite, 1);
transport_test_generator.generate();
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/ZlibTest.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/ZlibTest.cpp b/lib/cpp/test/ZlibTest.cpp
index 14b1a37..49c5514 100644
--- a/lib/cpp/test/ZlibTest.cpp
+++ b/lib/cpp/test/ZlibTest.cpp
@@ -48,24 +48,26 @@ boost::mt19937 rng;
*/
class SizeGenerator {
-public:
+ public:
virtual ~SizeGenerator() {}
virtual unsigned int getSize() = 0;
};
class ConstantSizeGenerator : public SizeGenerator {
-public:
+ public:
ConstantSizeGenerator(unsigned int value) : value_(value) {}
- virtual unsigned int getSize() { return value_; }
+ virtual unsigned int getSize() {
+ return value_;
+ }
-private:
+ private:
unsigned int value_;
};
class LogNormalSizeGenerator : public SizeGenerator {
-public:
- LogNormalSizeGenerator(double mean, double std_dev)
- : gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
+ public:
+ LogNormalSizeGenerator(double mean, double std_dev) :
+ gen_(rng, boost::lognormal_distribution<double>(mean, std_dev)) {}
virtual unsigned int getSize() {
// Loop until we get a size of 1 or more
@@ -77,8 +79,8 @@ public:
}
}
-private:
- boost::variate_generator<boost::mt19937, boost::lognormal_distribution<double> > gen_;
+ private:
+ boost::variate_generator< boost::mt19937, boost::lognormal_distribution<double> > gen_;
};
uint8_t* gen_uniform_buffer(uint32_t buf_len, uint8_t c) {
@@ -93,10 +95,10 @@ uint8_t* gen_compressible_buffer(uint32_t buf_len) {
// Generate small runs of alternately increasing and decreasing bytes
boost::uniform_smallint<uint32_t> run_length_distribution(1, 64);
boost::uniform_smallint<uint8_t> byte_distribution(0, UINT8_MAX);
- boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
- byte_generator(rng, byte_distribution);
- boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint32_t> >
- run_len_generator(rng, run_length_distribution);
+ boost::variate_generator< boost::mt19937, boost::uniform_smallint<uint8_t> >
+ byte_generator(rng, byte_distribution);
+ boost::variate_generator< boost::mt19937, boost::uniform_smallint<uint32_t> >
+ run_len_generator(rng, run_length_distribution);
uint32_t idx = 0;
int8_t step = 1;
@@ -123,8 +125,8 @@ uint8_t* gen_random_buffer(uint32_t buf_len) {
uint8_t* buf = new uint8_t[buf_len];
boost::uniform_smallint<uint8_t> distribution(0, UINT8_MAX);
- boost::variate_generator<boost::mt19937, boost::uniform_smallint<uint8_t> >
- generator(rng, distribution);
+ boost::variate_generator< boost::mt19937, boost::uniform_smallint<uint8_t> >
+ generator(rng, distribution);
for (uint32_t n = 0; n < buf_len; ++n) {
buf[n] = generator();
@@ -165,7 +167,7 @@ void test_separate_checksum(const uint8_t* buf, uint32_t buf_len) {
membuf->appendBufferToString(tmp_buf);
zlib_trans.reset(new TZlibTransport(membuf,
TZlibTransport::DEFAULT_URBUF_SIZE,
- static_cast<uint32_t>(tmp_buf.length() - 1)));
+ static_cast<uint32_t>(tmp_buf.length()-1)));
boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
uint32_t got = zlib_trans->readAll(mirror.get(), buf_len);
@@ -184,7 +186,8 @@ void test_incomplete_checksum(const uint8_t* buf, uint32_t buf_len) {
string tmp_buf;
membuf->appendBufferToString(tmp_buf);
tmp_buf.erase(tmp_buf.length() - 1);
- membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
+ membuf->resetBuffer(const_cast<uint8_t*>(
+ reinterpret_cast<const uint8_t*>(tmp_buf.data())),
static_cast<uint32_t>(tmp_buf.length()));
boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
@@ -199,8 +202,7 @@ void test_incomplete_checksum(const uint8_t* buf, uint32_t buf_len) {
}
}
-void test_read_write_mix(const uint8_t* buf,
- uint32_t buf_len,
+void test_read_write_mix(const uint8_t* buf, uint32_t buf_len,
const boost::shared_ptr<SizeGenerator>& write_gen,
const boost::shared_ptr<SizeGenerator>& read_gen) {
// Try it with a mix of read/write sizes.
@@ -230,7 +232,7 @@ void test_read_write_mix(const uint8_t* buf,
}
uint32_t got = zlib_trans->read(mirror.get() + tot, read_len);
BOOST_REQUIRE_LE(got, expected_read_len);
- BOOST_REQUIRE_NE(got, (uint32_t)0);
+ BOOST_REQUIRE_NE(got, (uint32_t) 0);
tot += got;
}
@@ -262,7 +264,8 @@ void test_invalid_checksum(const uint8_t* buf, uint32_t buf_len) {
// error when only modifying checksum bytes.
int index = static_cast<int>(tmp_buf.size() - 1);
tmp_buf[index]++;
- membuf->resetBuffer(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(tmp_buf.data())),
+ membuf->resetBuffer(const_cast<uint8_t*>(
+ reinterpret_cast<const uint8_t*>(tmp_buf.data())),
static_cast<uint32_t>(tmp_buf.length()));
boost::shared_array<uint8_t> mirror(new uint8_t[buf_len]);
@@ -320,22 +323,21 @@ void test_no_write() {
TZlibTransport w_zlib_trans(membuf);
}
- BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t)0);
+ BOOST_CHECK_EQUAL(membuf->available_read(), (uint32_t) 0);
}
/*
* Initialization
*/
-#define ADD_TEST_CASE(suite, name, function, ...) \
- do { \
- ::std::ostringstream name_ss; \
- name_ss << name << "-" << BOOST_STRINGIZE(function); \
- ::boost::unit_test::test_case* tc \
- = ::boost::unit_test::make_test_case(::apache::thrift::stdcxx::bind(function, \
- ##__VA_ARGS__), \
- name_ss.str()); \
- (suite)->add(tc); \
+#define ADD_TEST_CASE(suite, name, function, ...) \
+ do { \
+ ::std::ostringstream name_ss; \
+ name_ss << name << "-" << BOOST_STRINGIZE(function); \
+ ::boost::unit_test::test_case* tc = ::boost::unit_test::make_test_case( \
+ ::apache::thrift::stdcxx::bind(function, ## __VA_ARGS__), \
+ name_ss.str()); \
+ (suite)->add(tc); \
} while (0)
void add_tests(boost::unit_test::test_suite* suite,
@@ -348,30 +350,20 @@ void add_tests(boost::unit_test::test_suite* suite,
ADD_TEST_CASE(suite, name, test_invalid_checksum, buf, buf_len);
ADD_TEST_CASE(suite, name, test_write_after_flush, buf, buf_len);
- boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1 << 15));
+ boost::shared_ptr<SizeGenerator> size_32k(new ConstantSizeGenerator(1<<15));
boost::shared_ptr<SizeGenerator> size_lognormal(new LogNormalSizeGenerator(20, 30));
- ADD_TEST_CASE(suite, name << "-constant", test_read_write_mix, buf, buf_len, size_32k, size_32k);
- ADD_TEST_CASE(suite,
- name << "-lognormal-write",
- test_read_write_mix,
- buf,
- buf_len,
- size_lognormal,
- size_32k);
- ADD_TEST_CASE(suite,
- name << "-lognormal-read",
- test_read_write_mix,
- buf,
- buf_len,
- size_32k,
- size_lognormal);
- ADD_TEST_CASE(suite,
- name << "-lognormal-both",
- test_read_write_mix,
- buf,
- buf_len,
- size_lognormal,
- size_lognormal);
+ ADD_TEST_CASE(suite, name << "-constant",
+ test_read_write_mix, buf, buf_len,
+ size_32k, size_32k);
+ ADD_TEST_CASE(suite, name << "-lognormal-write",
+ test_read_write_mix, buf, buf_len,
+ size_lognormal, size_32k);
+ ADD_TEST_CASE(suite, name << "-lognormal-read",
+ test_read_write_mix, buf, buf_len,
+ size_32k, size_lognormal);
+ ADD_TEST_CASE(suite, name << "-lognormal-both",
+ test_read_write_mix, buf, buf_len,
+ size_lognormal, size_lognormal);
// Test with a random size distribution,
// but use the exact same distribution for reading as for writing.
@@ -381,13 +373,9 @@ void add_tests(boost::unit_test::test_suite* suite,
// both start with random number generators in the same state.
boost::shared_ptr<SizeGenerator> write_size_gen(new LogNormalSizeGenerator(20, 30));
boost::shared_ptr<SizeGenerator> read_size_gen(new LogNormalSizeGenerator(20, 30));
- ADD_TEST_CASE(suite,
- name << "-lognormal-same-distribution",
- test_read_write_mix,
- buf,
- buf_len,
- write_size_gen,
- read_size_gen);
+ ADD_TEST_CASE(suite, name << "-lognormal-same-distribution",
+ test_read_write_mix, buf, buf_len,
+ write_size_gen, read_size_gen);
}
void print_usage(FILE* f, const char* argv0) {
@@ -404,10 +392,11 @@ boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
printf("seed: %" PRIu32 "\n", seed);
rng.seed(seed);
- boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite();
+ boost::unit_test::test_suite* suite =
+ &boost::unit_test::framework::master_test_suite();
suite->p_name.value = "ZlibTest";
- uint32_t buf_len = 1024 * 32;
+ uint32_t buf_len = 1024*32;
add_tests(suite, gen_uniform_buffer(buf_len, 'a'), buf_len, "uniform");
add_tests(suite, gen_compressible_buffer(buf_len), buf_len, "compressible");
add_tests(suite, gen_random_buffer(buf_len), buf_len, "random");
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/concurrency/Tests.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/concurrency/Tests.cpp b/lib/cpp/test/concurrency/Tests.cpp
index 0d81d7e..c80bb88 100644
--- a/lib/cpp/test/concurrency/Tests.cpp
+++ b/lib/cpp/test/concurrency/Tests.cpp
@@ -29,7 +29,7 @@ int main(int argc, char** argv) {
std::string arg;
- std::vector<std::string> args(argc - 1 > 1 ? argc - 1 : 1);
+ std::vector<std::string> args(argc - 1 > 1 ? argc - 1 : 1);
args[0] = "all";
@@ -45,9 +45,9 @@ int main(int argc, char** argv) {
std::cout << "ThreadFactory tests..." << std::endl;
- size_t count = 1000;
- size_t floodLoops = 1;
- size_t floodCount = 100000;
+ size_t count = 1000;
+ size_t floodLoops = 1;
+ size_t floodCount = 100000;
std::cout << "\t\tThreadFactory reap N threads test: N = " << count << std::endl;
@@ -89,6 +89,7 @@ int main(int argc, char** argv) {
std::cout << "\t\t\tscall per ms: " << count / (time01 - time00) << std::endl;
}
+
if (runAll || args[0].compare("timer-manager") == 0) {
std::cout << "TimerManager tests..." << std::endl;
@@ -112,17 +113,16 @@ int main(int argc, char** argv) {
int64_t delay = 10LL;
- std::cout << "\t\tThreadManager load test: worker count: " << workerCount
- << " task count: " << taskCount << " delay: " << delay << std::endl;
+ std::cout << "\t\tThreadManager load test: worker count: " << workerCount << " task count: " << taskCount << " delay: " << delay << std::endl;
ThreadManagerTests threadManagerTests;
assert(threadManagerTests.loadTest(taskCount, delay, workerCount));
- std::cout << "\t\tThreadManager block test: worker count: " << workerCount
- << " delay: " << delay << std::endl;
+ std::cout << "\t\tThreadManager block test: worker count: " << workerCount << " delay: " << delay << std::endl;
assert(threadManagerTests.blockTest(delay, workerCount));
+
}
}
@@ -140,12 +140,11 @@ int main(int argc, char** argv) {
int64_t delay = 10LL;
- for (size_t workerCount = minWorkerCount; workerCount < maxWorkerCount; workerCount *= 2) {
+ for (size_t workerCount = minWorkerCount; workerCount < maxWorkerCount; workerCount*= 2) {
size_t taskCount = workerCount * tasksPerWorker;
- std::cout << "\t\tThreadManager load test: worker count: " << workerCount
- << " task count: " << taskCount << " delay: " << delay << std::endl;
+ std::cout << "\t\tThreadManager load test: worker count: " << workerCount << " task count: " << taskCount << " delay: " << delay << std::endl;
ThreadManagerTests threadManagerTests;
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/concurrency/ThreadFactoryTests.h
----------------------------------------------------------------------
diff --git a/lib/cpp/test/concurrency/ThreadFactoryTests.h b/lib/cpp/test/concurrency/ThreadFactoryTests.h
old mode 100644
new mode 100755
index d7431a8..2d97337
--- a/lib/cpp/test/concurrency/ThreadFactoryTests.h
+++ b/lib/cpp/test/concurrency/ThreadFactoryTests.h
@@ -27,10 +27,7 @@
#include <iostream>
#include <set>
-namespace apache {
-namespace thrift {
-namespace concurrency {
-namespace test {
+namespace apache { namespace thrift { namespace concurrency { namespace test {
using boost::shared_ptr;
using namespace apache::thrift::concurrency;
@@ -43,14 +40,18 @@ using namespace apache::thrift::concurrency;
class ThreadFactoryTests {
public:
+
static const double TEST_TOLERANCE;
- class Task : public Runnable {
+ class Task: public Runnable {
public:
+
Task() {}
- void run() { std::cout << "\t\t\tHello World" << std::endl; }
+ void run() {
+ std::cout << "\t\t\tHello World" << std::endl;
+ }
};
/**
@@ -76,17 +77,20 @@ public:
/**
* Reap N threads
*/
- class ReapNTask : public Runnable {
+ class ReapNTask: public Runnable {
- public:
- ReapNTask(Monitor& monitor, int& activeCount) : _monitor(monitor), _count(activeCount) {}
+ public:
+
+ ReapNTask(Monitor& monitor, int& activeCount) :
+ _monitor(monitor),
+ _count(activeCount) {}
void run() {
Synchronized s(_monitor);
_count--;
- // std::cout << "\t\t\tthread count: " << _count << std::endl;
+ //std::cout << "\t\t\tthread count: " << _count << std::endl;
if (_count == 0) {
_monitor.notify();
@@ -98,15 +102,15 @@ public:
int& _count;
};
- bool reapNThreads(int loop = 1, int count = 10) {
+ bool reapNThreads(int loop=1, int count=10) {
- PlatformThreadFactory threadFactory = PlatformThreadFactory();
+ PlatformThreadFactory threadFactory = PlatformThreadFactory();
Monitor* monitor = new Monitor();
- for (int lix = 0; lix < loop; lix++) {
+ for(int lix = 0; lix < loop; lix++) {
- int* activeCount = new int(count);
+ int* activeCount = new int(count);
std::set<shared_ptr<Thread> > threads;
@@ -114,25 +118,20 @@ public:
for (tix = 0; tix < count; tix++) {
try {
- threads.insert(
- threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, *activeCount))));
- } catch (SystemResourceException& e) {
- std::cout << "\t\t\tfailed to create " << lix* count + tix << " thread " << e.what()
- << std::endl;
+ threads.insert(threadFactory.newThread(shared_ptr<Runnable>(new ReapNTask(*monitor, *activeCount))));
+ } catch(SystemResourceException& e) {
+ std::cout << "\t\t\tfailed to create " << lix * count + tix << " thread " << e.what() << std::endl;
throw e;
}
}
tix = 0;
- for (std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin();
- thread != threads.end();
- tix++, ++thread) {
+ for (std::set<shared_ptr<Thread> >::const_iterator thread = threads.begin(); thread != threads.end(); tix++, ++thread) {
try {
(*thread)->start();
- } catch (SystemResourceException& e) {
- std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what()
- << std::endl;
+ } catch(SystemResourceException& e) {
+ std::cout << "\t\t\tfailed to start " << lix * count + tix << " thread " << e.what() << std::endl;
throw e;
}
}
@@ -144,7 +143,7 @@ public:
}
}
delete activeCount;
- std::cout << "\t\t\treaped " << lix* count << " threads" << std::endl;
+ std::cout << "\t\t\treaped " << lix * count << " threads" << std::endl;
}
std::cout << "\t\t\tSuccess!" << std::endl;
@@ -152,12 +151,21 @@ public:
return true;
}
- class SynchStartTask : public Runnable {
+ class SynchStartTask: public Runnable {
- public:
- enum STATE { UNINITIALIZED, STARTING, STARTED, STOPPING, STOPPED };
+ public:
+
+ enum STATE {
+ UNINITIALIZED,
+ STARTING,
+ STARTED,
+ STOPPING,
+ STOPPED
+ };
- SynchStartTask(Monitor& monitor, volatile STATE& state) : _monitor(monitor), _state(state) {}
+ SynchStartTask(Monitor& monitor, volatile STATE& state) :
+ _monitor(monitor),
+ _state(state) {}
void run() {
{
@@ -181,9 +189,9 @@ public:
}
}
- private:
+ private:
Monitor& _monitor;
- volatile STATE& _state;
+ volatile STATE& _state;
};
bool synchStartTest() {
@@ -192,10 +200,9 @@ public:
SynchStartTask::STATE state = SynchStartTask::UNINITIALIZED;
- shared_ptr<SynchStartTask> task
- = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state));
+ shared_ptr<SynchStartTask> task = shared_ptr<SynchStartTask>(new SynchStartTask(monitor, state));
- PlatformThreadFactory threadFactory = PlatformThreadFactory();
+ PlatformThreadFactory threadFactory = PlatformThreadFactory();
shared_ptr<Thread> thread = threadFactory.newThread(task);
@@ -219,8 +226,8 @@ public:
Synchronized s(monitor);
try {
- monitor.wait(100);
- } catch (TimedOutException& e) {
+ monitor.wait(100);
+ } catch(TimedOutException& e) {
}
if (state == SynchStartTask::STARTED) {
@@ -246,7 +253,7 @@ public:
/** See how accurate monitor timeout is. */
- bool monitorTimeoutTest(size_t count = 1000, int64_t timeout = 10) {
+ bool monitorTimeoutTest(size_t count=1000, int64_t timeout=10) {
Monitor monitor;
@@ -256,8 +263,8 @@ public:
{
Synchronized s(monitor);
try {
- monitor.wait(timeout);
- } catch (TimedOutException& e) {
+ monitor.wait(timeout);
+ } catch(TimedOutException& e) {
}
}
}
@@ -266,32 +273,31 @@ public:
double error = ((endTime - startTime) - (count * timeout)) / (double)(count * timeout);
- if (error < 0.0) {
+ if (error < 0.0) {
error *= 1.0;
}
bool success = error < ThreadFactoryTests::TEST_TOLERANCE;
- std::cout << "\t\t\t" << (success ? "Success" : "Failure")
- << "! expected time: " << count * timeout
- << "ms elapsed time: " << endTime - startTime << "ms error%: " << error * 100.0
- << std::endl;
+ std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << count * timeout << "ms elapsed time: "<< endTime - startTime << "ms error%: " << error * 100.0 << std::endl;
return success;
}
+
class FloodTask : public Runnable {
public:
- FloodTask(const size_t id) : _id(id) {}
- ~FloodTask() {
- if (_id % 1000 == 0) {
+
+ FloodTask(const size_t id) :_id(id) {}
+ ~FloodTask(){
+ if(_id % 1000 == 0) {
std::cout << "\t\tthread " << _id << " done" << std::endl;
}
}
- void run() {
- if (_id % 1000 == 0) {
+ void run(){
+ if(_id % 1000 == 0) {
std::cout << "\t\tthread " << _id << " started" << std::endl;
}
@@ -300,41 +306,42 @@ public:
const size_t _id;
};
- void foo(PlatformThreadFactory* tf) { (void)tf; }
+ void foo(PlatformThreadFactory *tf) {
+ (void) tf;
+ }
- bool floodNTest(size_t loop = 1, size_t count = 100000) {
+ bool floodNTest(size_t loop=1, size_t count=100000) {
bool success = false;
- for (size_t lix = 0; lix < loop; lix++) {
+ for(size_t lix = 0; lix < loop; lix++) {
PlatformThreadFactory threadFactory = PlatformThreadFactory();
threadFactory.setDetached(true);
- for (size_t tix = 0; tix < count; tix++) {
+ for(size_t tix = 0; tix < count; tix++) {
- try {
+ try {
- shared_ptr<FloodTask> task(new FloodTask(lix * count + tix));
+ shared_ptr<FloodTask> task(new FloodTask(lix * count + tix ));
- shared_ptr<Thread> thread = threadFactory.newThread(task);
+ shared_ptr<Thread> thread = threadFactory.newThread(task);
- thread->start();
+ thread->start();
- THRIFT_SLEEP_USEC(1);
+ THRIFT_SLEEP_USEC(1);
- } catch (TException& e) {
+ } catch (TException& e) {
- std::cout << "\t\t\tfailed to start " << lix* count + tix << " thread " << e.what()
- << std::endl;
+ std::cout << "\t\t\tfailed to start " << lix * count + tix << " thread " << e.what() << std::endl;
- return success;
+ return success;
+ }
}
- }
- std::cout << "\t\t\tflooded " << (lix + 1) * count << " threads" << std::endl;
+ std::cout << "\t\t\tflooded " << (lix + 1) * count << " threads" << std::endl;
- success = true;
+ success = true;
}
return success;
@@ -342,7 +349,5 @@ public:
};
const double ThreadFactoryTests::TEST_TOLERANCE = .20;
-}
-}
-}
-} // apache::thrift::concurrency::test
+
+}}}} // apache::thrift::concurrency::test
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/concurrency/ThreadManagerTests.h
----------------------------------------------------------------------
diff --git a/lib/cpp/test/concurrency/ThreadManagerTests.h b/lib/cpp/test/concurrency/ThreadManagerTests.h
old mode 100644
new mode 100755
index 27bf6c5..c08448b
--- a/lib/cpp/test/concurrency/ThreadManagerTests.h
+++ b/lib/cpp/test/concurrency/ThreadManagerTests.h
@@ -29,10 +29,7 @@
#include <set>
#include <stdint.h>
-namespace apache {
-namespace thrift {
-namespace concurrency {
-namespace test {
+namespace apache { namespace thrift { namespace concurrency { namespace test {
using namespace apache::thrift::concurrency;
@@ -41,11 +38,15 @@ class ThreadManagerTests {
static const double TEST_TOLERANCE;
public:
- class Task : public Runnable {
+ class Task: public Runnable {
public:
- Task(Monitor& monitor, size_t& count, int64_t timeout)
- : _monitor(monitor), _count(count), _timeout(timeout), _done(false) {}
+
+ Task(Monitor& monitor, size_t& count, int64_t timeout) :
+ _monitor(monitor),
+ _count(count),
+ _timeout(timeout),
+ _done(false) {}
void run() {
@@ -56,9 +57,9 @@ public:
try {
_sleep.wait(_timeout);
- } catch (TimedOutException& e) {
+ } catch(TimedOutException& e) {
;
- } catch (...) {
+ }catch(...) {
assert(0);
}
}
@@ -95,7 +96,7 @@ public:
* completes. Verify that all tasks completed and that thread manager cleans
* up properly on delete.
*/
- bool loadTest(size_t count = 100, int64_t timeout = 100LL, size_t workerCount = 4) {
+ bool loadTest(size_t count=100, int64_t timeout=100LL, size_t workerCount=4) {
Monitor monitor;
@@ -103,8 +104,7 @@ public:
shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount);
- shared_ptr<PlatformThreadFactory> threadFactory
- = shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
+ shared_ptr<PlatformThreadFactory> threadFactory = shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
#ifndef USE_BOOST_THREAD
threadFactory->setPriority(PosixThreadFactory::HIGHEST);
@@ -117,23 +117,20 @@ public:
for (size_t ix = 0; ix < count; ix++) {
- tasks.insert(shared_ptr<ThreadManagerTests::Task>(
- new ThreadManagerTests::Task(monitor, activeCount, timeout)));
+ tasks.insert(shared_ptr<ThreadManagerTests::Task>(new ThreadManagerTests::Task(monitor, activeCount, timeout)));
}
int64_t time00 = Util::currentTime();
- for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin();
- ix != tasks.end();
- ix++) {
+ for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
- threadManager->add(*ix);
+ threadManager->add(*ix);
}
{
Synchronized s(monitor);
- while (activeCount > 0) {
+ while(activeCount > 0) {
monitor.wait();
}
@@ -148,9 +145,7 @@ public:
int64_t minTime = 9223372036854775807LL;
int64_t maxTime = 0;
- for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin();
- ix != tasks.end();
- ix++) {
+ for (std::set<shared_ptr<ThreadManagerTests::Task> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
shared_ptr<ThreadManagerTests::Task> task = *ix;
@@ -174,43 +169,43 @@ public:
maxTime = delta;
}
- averageTime += delta;
+ averageTime+= delta;
}
averageTime /= count;
- std::cout << "\t\t\tfirst start: " << firstTime << "ms Last end: " << lastTime
- << "ms min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime
- << "ms" << std::endl;
+ std::cout << "\t\t\tfirst start: " << firstTime << "ms Last end: " << lastTime << "ms min: " << minTime << "ms max: " << maxTime << "ms average: " << averageTime << "ms" << std::endl;
double expectedTime = ((count + (workerCount - 1)) / workerCount) * timeout;
double error = ((time01 - time00) - expectedTime) / expectedTime;
if (error < 0) {
- error *= -1.0;
+ error*= -1.0;
}
bool success = error < TEST_TOLERANCE;
- std::cout << "\t\t\t" << (success ? "Success" : "Failure")
- << "! expected time: " << expectedTime << "ms elapsed time: " << time01 - time00
- << "ms error%: " << error * 100.0 << std::endl;
+ std::cout << "\t\t\t" << (success ? "Success" : "Failure") << "! expected time: " << expectedTime << "ms elapsed time: "<< time01 - time00 << "ms error%: " << error * 100.0 << std::endl;
return success;
}
- class BlockTask : public Runnable {
+ class BlockTask: public Runnable {
public:
- BlockTask(Monitor& monitor, Monitor& bmonitor, size_t& count)
- : _monitor(monitor), _bmonitor(bmonitor), _count(count) {}
+
+ BlockTask(Monitor& monitor, Monitor& bmonitor, size_t& count) :
+ _monitor(monitor),
+ _bmonitor(bmonitor),
+ _count(count) {}
void run() {
{
Synchronized s(_bmonitor);
_bmonitor.wait();
+
}
{
@@ -234,8 +229,8 @@ public:
* Block test. Create pendingTaskCountMax tasks. Verify that we block adding the
* pendingTaskCountMax + 1th task. Verify that we unblock when a task completes */
- bool blockTest(int64_t timeout = 100LL, size_t workerCount = 2) {
- (void)timeout;
+ bool blockTest(int64_t timeout=100LL, size_t workerCount=2) {
+ (void) timeout;
bool success = false;
try {
@@ -247,11 +242,9 @@ public:
size_t activeCounts[] = {workerCount, pendingTaskMaxCount, 1};
- shared_ptr<ThreadManager> threadManager
- = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount);
+ shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount, pendingTaskMaxCount);
- shared_ptr<PlatformThreadFactory> threadFactory
- = shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
+ shared_ptr<PlatformThreadFactory> threadFactory = shared_ptr<PlatformThreadFactory>(new PlatformThreadFactory());
#ifndef USE_BOOST_THREAD
threadFactory->setPriority(PosixThreadFactory::HIGHEST);
@@ -264,49 +257,43 @@ public:
for (size_t ix = 0; ix < workerCount; ix++) {
- tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(
- new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[0])));
+ tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[0])));
}
for (size_t ix = 0; ix < pendingTaskMaxCount; ix++) {
- tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(
- new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[1])));
+ tasks.insert(shared_ptr<ThreadManagerTests::BlockTask>(new ThreadManagerTests::BlockTask(monitor, bmonitor,activeCounts[1])));
}
- for (std::set<shared_ptr<ThreadManagerTests::BlockTask> >::iterator ix = tasks.begin();
- ix != tasks.end();
- ix++) {
+ for (std::set<shared_ptr<ThreadManagerTests::BlockTask> >::iterator ix = tasks.begin(); ix != tasks.end(); ix++) {
threadManager->add(*ix);
}
- if (!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) {
+ if(!(success = (threadManager->totalTaskCount() == pendingTaskMaxCount + workerCount))) {
throw TException("Unexpected pending task count");
}
- shared_ptr<ThreadManagerTests::BlockTask> extraTask(
- new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[2]));
+ shared_ptr<ThreadManagerTests::BlockTask> extraTask(new ThreadManagerTests::BlockTask(monitor, bmonitor, activeCounts[2]));
try {
threadManager->add(extraTask, 1);
throw TException("Unexpected success adding task in excess of pending task count");
- } catch (TooManyPendingTasksException& e) {
+ } catch(TooManyPendingTasksException& e) {
throw TException("Should have timed out adding task in excess of pending task count");
- } catch (TimedOutException& e) {
+ } catch(TimedOutException& e) {
// Expected result
}
try {
threadManager->add(extraTask, -1);
throw TException("Unexpected success adding task in excess of pending task count");
- } catch (TimedOutException& e) {
+ } catch(TimedOutException& e) {
throw TException("Unexpected timeout adding task in excess of pending task count");
- } catch (TooManyPendingTasksException& e) {
+ } catch(TooManyPendingTasksException& e) {
// Expected result
}
- std::cout << "\t\t\t"
- << "Pending tasks " << threadManager->pendingTaskCount() << std::endl;
+ std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl;
{
Synchronized s(bmonitor);
@@ -317,24 +304,21 @@ public:
{
Synchronized s(monitor);
- while (activeCounts[0] != 0) {
+ while(activeCounts[0] != 0) {
monitor.wait();
}
}
- std::cout << "\t\t\t"
- << "Pending tasks " << threadManager->pendingTaskCount() << std::endl;
+ std::cout << "\t\t\t" << "Pending tasks " << threadManager->pendingTaskCount() << std::endl;
try {
threadManager->add(extraTask, 1);
- } catch (TimedOutException& e) {
- std::cout << "\t\t\t"
- << "add timed out unexpectedly" << std::endl;
+ } catch(TimedOutException& e) {
+ std::cout << "\t\t\t" << "add timed out unexpectedly" << std::endl;
throw TException("Unexpected timeout adding task");
- } catch (TooManyPendingTasksException& e) {
- std::cout << "\t\t\t"
- << "add encountered too many pending exepctions" << std::endl;
+ } catch(TooManyPendingTasksException& e) {
+ std::cout << "\t\t\t" << "add encountered too many pending exepctions" << std::endl;
throw TException("Unexpected timeout adding task");
}
@@ -349,7 +333,7 @@ public:
{
Synchronized s(monitor);
- while (activeCounts[1] != 0) {
+ while(activeCounts[1] != 0) {
monitor.wait();
}
}
@@ -365,28 +349,26 @@ public:
{
Synchronized s(monitor);
- while (activeCounts[2] != 0) {
+ while(activeCounts[2] != 0) {
monitor.wait();
}
}
- if (!(success = (threadManager->totalTaskCount() == 0))) {
+ if(!(success = (threadManager->totalTaskCount() == 0))) {
throw TException("Unexpected pending task count");
}
- } catch (TException& e) {
+ } catch(TException& e) {
std::cout << "ERROR: " << e.what() << std::endl;
}
std::cout << "\t\t\t" << (success ? "Success" : "Failure") << std::endl;
return success;
- }
+ }
};
const double ThreadManagerTests::TEST_TOLERANCE = .20;
-}
-}
-}
-} // apache::thrift::concurrency
+
+}}}} // apache::thrift::concurrency
using namespace apache::thrift::concurrency::test;
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/concurrency/TimerManagerTests.h
----------------------------------------------------------------------
diff --git a/lib/cpp/test/concurrency/TimerManagerTests.h b/lib/cpp/test/concurrency/TimerManagerTests.h
index dda16ed..62eb4f4 100644
--- a/lib/cpp/test/concurrency/TimerManagerTests.h
+++ b/lib/cpp/test/concurrency/TimerManagerTests.h
@@ -25,10 +25,7 @@
#include <assert.h>
#include <iostream>
-namespace apache {
-namespace thrift {
-namespace concurrency {
-namespace test {
+namespace apache { namespace thrift { namespace concurrency { namespace test {
using namespace apache::thrift::concurrency;
@@ -36,15 +33,16 @@ class TimerManagerTests {
static const double TEST_TOLERANCE;
-public:
- class Task : public Runnable {
- public:
- Task(Monitor& monitor, int64_t timeout)
- : _timeout(timeout),
- _startTime(Util::currentTime()),
- _monitor(monitor),
- _success(false),
- _done(false) {}
+ public:
+ class Task: public Runnable {
+ public:
+
+ Task(Monitor& monitor, int64_t timeout) :
+ _timeout(timeout),
+ _startTime(Util::currentTime()),
+ _monitor(monitor),
+ _success(false),
+ _done(false) {}
~Task() { std::cerr << this << std::endl; }
@@ -56,20 +54,20 @@ public:
int64_t delta = _endTime - _startTime;
- delta = delta > _timeout ? delta - _timeout : _timeout - delta;
+
+ delta = delta > _timeout ? delta - _timeout : _timeout - delta;
float error = delta / _timeout;
- if (error < TEST_TOLERANCE) {
+ if(error < TEST_TOLERANCE) {
_success = true;
}
_done = true;
- std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; // debug
+ std::cout << "\t\t\tTimerManagerTests::Task[" << this << "] done" << std::endl; //debug
- {
- Synchronized s(_monitor);
+ {Synchronized s(_monitor);
_monitor.notifyAll();
}
}
@@ -88,10 +86,9 @@ public:
* properly clean up itself and the remaining orphaned timeout task when the
* manager goes out of scope and its destructor is called.
*/
- bool test00(int64_t timeout = 1000LL) {
+ bool test00(int64_t timeout=1000LL) {
- shared_ptr<TimerManagerTests::Task> orphanTask
- = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
+ shared_ptr<TimerManagerTests::Task> orphanTask = shared_ptr<TimerManagerTests::Task>(new TimerManagerTests::Task(_monitor, 10 * timeout));
{
@@ -116,13 +113,12 @@ public:
// Wait for 1 second in order to give timerManager a chance to start sleeping in response
// to adding orphanTask. We need to do this so we can verify that adding the second task
// kicks the dispatcher out of the current wait and starts the new 1 second wait.
- _monitor.wait(1000);
- assert(
- 0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
- } catch (TimedOutException& ex) {
+ _monitor.wait (1000);
+ assert (0 == "ERROR: This wait should time out. TimerManager dispatcher may have a problem.");
+ } catch (TimedOutException &ex) {
}
- task.reset(new TimerManagerTests::Task(_monitor, timeout));
+ task.reset (new TimerManagerTests::Task(_monitor, timeout));
timerManager.add(task, timeout);
@@ -131,6 +127,7 @@ public:
assert(task->_done);
+
std::cout << "\t\t\t" << (task->_success ? "Success" : "Failure") << "!" << std::endl;
}
@@ -147,7 +144,5 @@ public:
};
const double TimerManagerTests::TEST_TOLERANCE = .20;
-}
-}
-}
-} // apache::thrift::concurrency
+
+}}}} // apache::thrift::concurrency
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/processor/EventLog.cpp
----------------------------------------------------------------------
diff --git a/lib/cpp/test/processor/EventLog.cpp b/lib/cpp/test/processor/EventLog.cpp
index d4b8372..0ac3028 100644
--- a/lib/cpp/test/processor/EventLog.cpp
+++ b/lib/cpp/test/processor/EventLog.cpp
@@ -36,11 +36,10 @@ void debug(const char* fmt, ...) {
fprintf(stderr, "\n");
}
+
}
-namespace apache {
-namespace thrift {
-namespace test {
+namespace apache { namespace thrift { namespace test {
uint32_t EventLog::nextId_ = 0;
@@ -75,12 +74,11 @@ EventLog::EventLog() {
debug("New log: %d", id_);
}
-void EventLog::append(EventType type,
- uint32_t connectionId,
- uint32_t callId,
+void EventLog::append(EventType type, uint32_t connectionId, uint32_t callId,
const string& message) {
Synchronized s(monitor_);
- debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type, message.c_str());
+ debug("%d <-- %u, %u, %s \"%s\"", id_, connectionId, callId, type,
+ message.c_str());
Event e(type, connectionId, callId, message);
events_.push_back(e);
@@ -127,6 +125,5 @@ Event EventLog::waitForConnEvent(uint32_t connId, int64_t timeout) {
}
}
}
-}
-}
-} // apache::thrift::test
+
+}}} // apache::thrift::test
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/processor/EventLog.h
----------------------------------------------------------------------
diff --git a/lib/cpp/test/processor/EventLog.h b/lib/cpp/test/processor/EventLog.h
index 4f8275d..d731cec 100644
--- a/lib/cpp/test/processor/EventLog.h
+++ b/lib/cpp/test/processor/EventLog.h
@@ -21,9 +21,7 @@
#include <thrift/concurrency/Monitor.h>
-namespace apache {
-namespace thrift {
-namespace test {
+namespace apache { namespace thrift { namespace test {
// Initially I made EventType an enum, but using char* results
// in much more readable error messages when there is a mismatch.
@@ -33,17 +31,21 @@ namespace test {
typedef const char* EventType;
struct Event {
- Event(EventType type, uint32_t connectionId, uint32_t callId, const std::string& message)
- : type(type), connectionId(connectionId), callId(callId), message(message) {}
+ Event(EventType type, uint32_t connectionId, uint32_t callId,
+ const std::string& message) :
+ type(type),
+ connectionId(connectionId),
+ callId(callId),
+ message(message) {}
EventType type;
- uint32_t connectionId;
- uint32_t callId;
- std::string message;
+ uint32_t connectionId;
+ uint32_t callId;
+ std::string message;
};
class EventLog {
-public:
+ public:
static EventType ET_LOG_END;
static EventType ET_CONN_CREATED;
static EventType ET_CONN_DESTROYED;
@@ -71,15 +73,13 @@ public:
EventLog();
- void append(EventType type,
- uint32_t connectionId,
- uint32_t callId,
+ void append(EventType type, uint32_t connectionId, uint32_t callId,
const std::string& message = "");
Event waitForEvent(int64_t timeout = 500);
Event waitForConnEvent(uint32_t connId, int64_t timeout = 500);
-protected:
+ protected:
typedef std::list<Event> EventList;
concurrency::Monitor monitor_;
@@ -88,8 +88,7 @@ protected:
static uint32_t nextId_;
};
-}
-}
-} // apache::thrift::test
+
+}}} // apache::thrift::test
#endif // _THRIFT_TEST_EVENTLOG_H_
http://git-wip-us.apache.org/repos/asf/thrift/blob/240120c8/lib/cpp/test/processor/Handlers.h
----------------------------------------------------------------------
diff --git a/lib/cpp/test/processor/Handlers.h b/lib/cpp/test/processor/Handlers.h
index d894107..75f4349 100644
--- a/lib/cpp/test/processor/Handlers.h
+++ b/lib/cpp/test/processor/Handlers.h
@@ -23,14 +23,15 @@
#include "gen-cpp/ParentService.h"
#include "gen-cpp/ChildService.h"
-namespace apache {
-namespace thrift {
-namespace test {
+namespace apache { namespace thrift { namespace test {
class ParentHandler : virtual public ParentServiceIf {
-public:
- ParentHandler(const boost::shared_ptr<EventLog>& log)
- : triggerMonitor(&mutex_), generation_(0), wait_(false), log_(log) {}
+ public:
+ ParentHandler(const boost::shared_ptr<EventLog>& log) :
+ triggerMonitor(&mutex_),
+ generation_(0),
+ wait_(false),
+ log_(log) { }
int32_t incrementGeneration() {
concurrency::Guard g(mutex_);
@@ -115,7 +116,7 @@ public:
triggerMonitor.notifyAll();
}
-protected:
+ protected:
/**
* blockUntilTriggered() won't return until triggerPendingCalls() is invoked
* in another thread.
@@ -140,8 +141,10 @@ protected:
};
class ChildHandler : public ParentHandler, virtual public ChildServiceIf {
-public:
- ChildHandler(const boost::shared_ptr<EventLog>& log) : ParentHandler(log), value_(0) {}
+ public:
+ ChildHandler(const boost::shared_ptr<EventLog>& log) :
+ ParentHandler(log),
+ value_(0) {}
int32_t setValue(int32_t value) {
concurrency::Guard g(mutex_);
@@ -159,16 +162,18 @@ public:
return value_;
}
-protected:
+ protected:
int32_t value_;
};
struct ConnContext {
-public:
+ public:
ConnContext(boost::shared_ptr<protocol::TProtocol> in,
boost::shared_ptr<protocol::TProtocol> out,
- uint32_t id)
- : input(in), output(out), id(id) {}
+ uint32_t id) :
+ input(in),
+ output(out),
+ id(id) {}
boost::shared_ptr<protocol::TProtocol> input;
boost::shared_ptr<protocol::TProtocol> output;
@@ -176,18 +181,22 @@ public:
};
struct CallContext {
-public:
- CallContext(ConnContext* context, uint32_t id, const std::string& name)
- : connContext(context), name(name), id(id) {}
+ public:
+ CallContext(ConnContext *context, uint32_t id, const std::string& name) :
+ connContext(context),
+ name(name),
+ id(id) {}
- ConnContext* connContext;
+ ConnContext *connContext;
std::string name;
uint32_t id;
};
class ServerEventHandler : public server::TServerEventHandler {
-public:
- ServerEventHandler(const boost::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {}
+ public:
+ ServerEventHandler(const boost::shared_ptr<EventLog>& log) :
+ nextId_(1),
+ log_(log) {}
virtual void preServe() {}
@@ -200,8 +209,8 @@ public:
}
virtual void deleteContext(void* serverContext,
- boost::shared_ptr<protocol::TProtocol> input,
- boost::shared_ptr<protocol::TProtocol> output) {
+ boost::shared_ptr<protocol::TProtocol>input,
+ boost::shared_ptr<protocol::TProtocol>output) {
ConnContext* context = reinterpret_cast<ConnContext*>(serverContext);
if (input != context->input) {
@@ -216,21 +225,22 @@ public:
delete context;
}
- virtual void processContext(void* serverContext,
- boost::shared_ptr<transport::TTransport> transport) {
-// TODO: We currently don't test the behavior of the processContext()
-// calls. The various server implementations call processContext() at
-// slightly different times, and it is too annoying to try and account for
-// their various differences.
-//
-// TThreadedServer, TThreadPoolServer, and TSimpleServer usually wait until
-// they see the first byte of a request before calling processContext().
-// However, they don't wait for the first byte of the very first request,
-// and instead immediately call processContext() before any data is
-// received.
-//
-// TNonblockingServer always waits until receiving the full request before
-// calling processContext().
+ virtual void processContext(
+ void* serverContext,
+ boost::shared_ptr<transport::TTransport> transport) {
+ // TODO: We currently don't test the behavior of the processContext()
+ // calls. The various server implementations call processContext() at
+ // slightly different times, and it is too annoying to try and account for
+ // their various differences.
+ //
+ // TThreadedServer, TThreadPoolServer, and TSimpleServer usually wait until
+ // they see the first byte of a request before calling processContext().
+ // However, they don't wait for the first byte of the very first request,
+ // and instead immediately call processContext() before any data is
+ // received.
+ //
+ // TNonblockingServer always waits until receiving the full request before
+ // calling processContext().
#if 0
ConnContext* context = reinterpret_cast<ConnContext*>(serverContext);
log_->append(EventLog::ET_PROCESS, context->id, 0);
@@ -240,14 +250,16 @@ public:
#endif
}
-protected:
+ protected:
uint32_t nextId_;
boost::shared_ptr<EventLog> log_;
};
class ProcessorEventHandler : public TProcessorEventHandler {
-public:
- ProcessorEventHandler(const boost::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {}
+ public:
+ ProcessorEventHandler(const boost::shared_ptr<EventLog>& log) :
+ nextId_(1),
+ log_(log) {}
void* getContext(const char* fnName, void* serverContext) {
ConnContext* connContext = reinterpret_cast<ConnContext*>(serverContext);
@@ -255,65 +267,71 @@ public:
CallContext* context = new CallContext(connContext, nextId_, fnName);
++nextId_;
- log_->append(EventLog::ET_CALL_STARTED, connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_CALL_STARTED, connContext->id, context->id,
+ fnName);
return context;
}
void freeContext(void* ctx, const char* fnName) {
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_CALL_FINISHED, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_CALL_FINISHED, context->connContext->id,
+ context->id, fnName);
delete context;
}
void preRead(void* ctx, const char* fnName) {
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_PRE_READ, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_PRE_READ, context->connContext->id, context->id,
+ fnName);
}
void postRead(void* ctx, const char* fnName, uint32_t bytes) {
THRIFT_UNUSED_VARIABLE(bytes);
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_POST_READ, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_POST_READ, context->connContext->id, context->id,
+ fnName);
}
void preWrite(void* ctx, const char* fnName) {
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_PRE_WRITE, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_PRE_WRITE, context->connContext->id, context->id,
+ fnName);
}
void postWrite(void* ctx, const char* fnName, uint32_t bytes) {
THRIFT_UNUSED_VARIABLE(bytes);
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_POST_WRITE, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_POST_WRITE, context->connContext->id,
+ context->id, fnName);
}
void asyncComplete(void* ctx, const char* fnName) {
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_ASYNC_COMPLETE, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_ASYNC_COMPLETE, context->connContext->id,
+ context->id, fnName);
}
void handlerError(void* ctx, const char* fnName) {
CallContext* context = reinterpret_cast<CallContext*>(ctx);
checkName(context, fnName);
- log_->append(EventLog::ET_HANDLER_ERROR, context->connContext->id, context->id, fnName);
+ log_->append(EventLog::ET_HANDLER_ERROR, context->connContext->id,
+ context->id, fnName);
}
-protected:
+ protected:
void checkName(const CallContext* context, const char* fnName) {
// Note: we can't use BOOST_CHECK_EQUAL here, since the handler runs in a
// different thread from the test functions. Just abort if the names are
// different
if (context->name != fnName) {
- fprintf(stderr,
- "call context name mismatch: \"%s\" != \"%s\"\n",
- context->name.c_str(),
- fnName);
+ fprintf(stderr, "call context name mismatch: \"%s\" != \"%s\"\n",
+ context->name.c_str(), fnName);
fflush(stderr);
abort();
}
@@ -322,8 +340,7 @@ protected:
uint32_t nextId_;
boost::shared_ptr<EventLog> log_;
};
-}
-}
-} // apache::thrift::test
+
+}}} // apache::thrift::test
#endif // _THRIFT_PROCESSOR_TEST_HANDLERS_H_