From 2a4bfd6d0cdd32c9ade36a5c481b952f522f4fb2 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 7 Apr 2008 23:45:00 +0000 Subject: [PATCH] Clean up the unit tests. - Make Python tests cwd-agnostic. - Use boost::test. - Add a benchmark. - Use a library to clean up test/Makefile.am. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@665640 13f79535-47bb-0310-9956-ffa450edef68 --- test/Benchmark.cpp | 88 ++++++++++++++++++++++++++++++++++++++ test/DebugProtoTest.cpp | 2 +- test/DebugProtoTest.thrift | 2 +- test/DenseProtoTest.cpp | 2 +- test/JSONProtoTest.cpp | 2 +- test/Makefile.am | 52 ++++++++++++++-------- test/ReflectionTest.cpp | 2 +- test/TMemoryBufferTest.cpp | 18 +++----- test/ThriftTest.thrift | 10 +++++ test/UnitTestMain.cpp | 2 + test/py/RunClientServer.py | 7 ++- 11 files changed, 151 insertions(+), 36 deletions(-) create mode 100644 test/Benchmark.cpp create mode 100644 test/UnitTestMain.cpp diff --git a/test/Benchmark.cpp b/test/Benchmark.cpp new file mode 100644 index 00000000..eede9ceb --- /dev/null +++ b/test/Benchmark.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include "gen-cpp/DebugProtoTest_types.h" +#include +#include "../lib/cpp/src/protocol/TDebugProtocol.h" + +class Timer { +public: + timeval vStart; + + Timer() { + gettimeofday(&vStart, 0); + } + void start() { + gettimeofday(&vStart, 0); + } + + double frame() { + timeval vEnd; + gettimeofday(&vEnd, 0); + double dstart = vStart.tv_sec + ((double)vStart.tv_usec / 1000000.0); + double dend = vEnd.tv_sec + ((double)vEnd.tv_usec / 1000000.0); + return dend - dstart; + } + +}; + +int main() { + using namespace std; + using namespace thrift::test::debug; + using namespace facebook::thrift::transport; + using namespace facebook::thrift::protocol; + using namespace boost; + + OneOfEach ooe; + ooe.im_true = true; + ooe.im_false = false; + ooe.a_bite = 0xd6; + ooe.integer16 = 27000; + ooe.integer32 = 1<<24; + ooe.integer64 = (uint64_t)6000 * 1000 * 1000; + ooe.double_precision = M_PI; + ooe.some_characters = "JSON THIS! \"\1"; + ooe.zomg_unicode = "\xd7\n\a\t"; + ooe.base64 = "\1\2\3\255"; + + shared_ptr buf(new TMemoryBuffer()); + + int num = 1000000; + + { + Timer timer; + + for (int i = 0; i < num; i ++) { + buf->resetBuffer(); + TBinaryProtocol prot(buf); + ooe.write(&prot); + } + cout << "Write: " << num / (1000 * timer.frame()) << " kHz" << endl; + } + + uint8_t* data; + uint32_t datasize; + + buf->getBuffer(&data, &datasize); + + { + + Timer timer; + + for (int i = 0; i < num; i ++) { + OneOfEach ooe2; + shared_ptr buf2(new TMemoryBuffer(data, datasize)); + //buf2->resetBuffer(data, datasize); + TBinaryProtocol prot(buf2); + ooe2.read(&prot); + + //cout << facebook::thrift::ThriftDebugString(ooe2) << endl << endl; + } + cout << " Read: " << num / (1000 * timer.frame()) << " kHz" << endl; + } + + + return 0; +} diff --git a/test/DebugProtoTest.cpp b/test/DebugProtoTest.cpp index d736d957..98ff43c7 100644 --- a/test/DebugProtoTest.cpp +++ b/test/DebugProtoTest.cpp @@ -6,7 +6,7 @@ int main() { using std::cout; using std::endl; - using namespace thrift::test; + using namespace thrift::test::debug; OneOfEach ooe; diff --git a/test/DebugProtoTest.thrift b/test/DebugProtoTest.thrift index fb86bf4c..515635e3 100644 --- a/test/DebugProtoTest.thrift +++ b/test/DebugProtoTest.thrift @@ -1,4 +1,4 @@ -namespace cpp thrift.test +namespace cpp thrift.test.debug namespace java thrift.test struct Doubles { diff --git a/test/DenseProtoTest.cpp b/test/DenseProtoTest.cpp index f50facaf..c979e8f5 100644 --- a/test/DenseProtoTest.cpp +++ b/test/DenseProtoTest.cpp @@ -40,7 +40,7 @@ int main() { using std::cout; using std::endl; using boost::shared_ptr; - using namespace thrift::test; + using namespace thrift::test::debug; using namespace facebook::thrift::transport; using namespace facebook::thrift::protocol; diff --git a/test/JSONProtoTest.cpp b/test/JSONProtoTest.cpp index 30d712f9..cc72b648 100644 --- a/test/JSONProtoTest.cpp +++ b/test/JSONProtoTest.cpp @@ -7,7 +7,7 @@ int main() { using std::cout; using std::endl; - using namespace thrift::test; + using namespace thrift::test::debug; using facebook::thrift::transport::TMemoryBuffer; using facebook::thrift::protocol::TJSONProtocol; diff --git a/test/Makefile.am b/test/Makefile.am index c6907909..5de2f1b2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -4,61 +4,77 @@ if ENABLE_JAVA SUBDIRS += java endif +noinst_LTLIBRARIES = libtestgencpp.la +libtestgencpp_la_SOURCES = \ + gen-cpp/DebugProtoTest_types.cpp \ + gen-cpp/OptionalRequiredTest_types.cpp \ + gen-cpp/DebugProtoTest_types.cpp \ + gen-cpp/PartiallyReflectable.cpp \ + gen-cpp/Service.cpp \ + gen-cpp/StressTest_types.cpp \ + gen-cpp/SecondService.cpp \ + gen-cpp/ThriftTest_constants.cpp \ + gen-cpp/ThriftTest.cpp \ + gen-cpp/ThriftTest_types.cpp + +libtestgencpp_la_LIBADD = $(top_srcdir)/lib/cpp/libthrift.la + +noinst_PROGRAMS = Benchmark + +Benchmark_SOURCES = \ + Benchmark.cpp + +Benchmark_LDADD = libtestgencpp.la + check_PROGRAMS = \ DebugProtoTest \ JSONProtoTest \ OptionalRequiredTest \ - ReflectionTest + ReflectionTest \ + UnitTests TESTS = \ $(check_PROGRAMS) +UnitTests_SOURCES = \ + UnitTestMain.cpp \ + TMemoryBufferTest.cpp + +UnitTests_LDADD = libtestgencpp.la # # DebugProtoTest # DebugProtoTest_SOURCES = \ - gen-cpp/DebugProtoTest_types.cpp \ DebugProtoTest.cpp -DebugProtoTest_LDADD = \ - $(top_srcdir)/lib/cpp/libthrift.la +DebugProtoTest_LDADD = libtestgencpp.la # # JSONProtoTest # JSONProtoTest_SOURCES = \ - gen-cpp/DebugProtoTest_types.cpp \ JSONProtoTest.cpp -JSONProtoTest_LDADD = \ - $(top_srcdir)/lib/cpp/libthrift.la +JSONProtoTest_LDADD = libtestgencpp.la # # OptionalRequiredTest # OptionalRequiredTest_SOURCES = \ - gen-cpp/OptionalRequiredTest_types.cpp \ OptionalRequiredTest.cpp -OptionalRequiredTest_LDADD = \ - $(top_srcdir)/lib/cpp/libthrift.la +OptionalRequiredTest_LDADD = libtestgencpp.la # # ReflectionTest # ReflectionTest_SOURCES = \ - gen-cpp/DebugProtoTest_types.cpp \ - gen-cpp/PartiallyReflectable.cpp \ - gen-cpp/Service.cpp \ - gen-cpp/StressTest_types.cpp \ ReflectionTest.cpp -ReflectionTest_LDADD = \ - $(top_srcdir)/lib/cpp/libthrift.la - +ReflectionTest_LDADD = libtestgencpp.la # # Common thrift code generation rules @@ -74,6 +90,8 @@ gen-cpp/OptionalRequiredTest_types.cpp: OptionalRequiredTest.thrift gen-cpp/Service.cpp gen-cpp/StressTest_types.cpp: StressTest.thrift $(THRIFT) --gen cpp:dense,reflection_limited $< +gen-cpp/SecondService.cpp gen-cpp/ThriftTest_constants.cpp gen-cpp/ThriftTest.cpp gen-cpp/ThriftTest_types.cpp: ThriftTest.thrift + $(THRIFT) --gen cpp:dense,reflection_limited $< INCLUDES = \ -I$(top_srcdir)/lib/cpp/src diff --git a/test/ReflectionTest.cpp b/test/ReflectionTest.cpp index 8193a3ec..d60b52cc 100644 --- a/test/ReflectionTest.cpp +++ b/test/ReflectionTest.cpp @@ -8,7 +8,7 @@ int main() { using std::endl; facebook::thrift::reflection::limited::Service srv1; - thrift::test::PartiallyReflectableIf::getStaticLimitedReflection(srv1); + thrift::test::debug::PartiallyReflectableIf::getStaticLimitedReflection(srv1); cout << facebook::thrift::ThriftDebugString(srv1) << endl << endl; facebook::thrift::reflection::limited::Service srv2; diff --git a/test/TMemoryBufferTest.cpp b/test/TMemoryBufferTest.cpp index 2b2f90c0..970ec470 100644 --- a/test/TMemoryBufferTest.cpp +++ b/test/TMemoryBufferTest.cpp @@ -1,11 +1,4 @@ -/* -thrift -cpp ThriftTest.thrift -g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \ - TMemoryBufferTest.cpp gen-cpp/ThriftTest_types.cpp \ - ../lib/cpp/.libs/libthrift.a -o TMemoryBufferTest -./TMemoryBufferTest -*/ - +#include #include #include #include @@ -13,9 +6,9 @@ g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \ #include #include "gen-cpp/ThriftTest_types.h" +BOOST_AUTO_TEST_SUITE( TMemoryBufferTest ) -int main(int argc, char** argv) { - { +BOOST_AUTO_TEST_CASE( test_roundtrip ) { using facebook::thrift::transport::TMemoryBuffer; using facebook::thrift::protocol::TBinaryProtocol; using boost::shared_ptr; @@ -41,6 +34,7 @@ int main(int argc, char** argv) { assert(a == a2); } +BOOST_AUTO_TEST_CASE( test_copy ) { using facebook::thrift::transport::TMemoryBuffer; using std::string; @@ -66,6 +60,7 @@ int main(int argc, char** argv) { assert(str4 == "67891234"); } +BOOST_AUTO_TEST_CASE( test_exceptions ) { using facebook::thrift::transport::TTransportException; using facebook::thrift::transport::TMemoryBuffer; @@ -90,5 +85,4 @@ int main(int argc, char** argv) { } } - -} +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift index b11e1f28..275b574f 100644 --- a/test/ThriftTest.thrift +++ b/test/ThriftTest.thrift @@ -120,3 +120,13 @@ struct VersioningTestV2 { 11: string newstring, 12: i32 end_in_both } + +struct ListTypeVersioningV1 { + 1: list myints; + 2: string hello; +} + +struct ListTypeVersioningV2 { + 1: list strings; + 2: string hello; +} \ No newline at end of file diff --git a/test/UnitTestMain.cpp b/test/UnitTestMain.cpp new file mode 100644 index 00000000..75c2fb19 --- /dev/null +++ b/test/UnitTestMain.cpp @@ -0,0 +1,2 @@ +#define BOOST_TEST_MODULE thrift +#include diff --git a/test/py/RunClientServer.py b/test/py/RunClientServer.py index 21f45310..f05dc5d0 100755 --- a/test/py/RunClientServer.py +++ b/test/py/RunClientServer.py @@ -5,10 +5,13 @@ import sys import os import signal -serverproc = subprocess.Popen([sys.executable, "TestServer.py"]) +def relfile(fname): + return os.path.join(os.path.dirname(__file__), fname) + +serverproc = subprocess.Popen([sys.executable, relfile("TestServer.py")]) try: - ret = subprocess.call([sys.executable, "TestClient.py"]) + ret = subprocess.call([sys.executable, relfile("TestClient.py")]) if ret != 0: raise Exception("subprocess failed") finally: -- 2.17.1