Thrift-2029:Port C++ tests to Windows
Client: cpp
Patch: Ben Craig
Updates cpp tests to work with windows and c++11
diff --git a/lib/cpp/test/TransportTest.cpp b/lib/cpp/test/TransportTest.cpp
index 4233e6e..78c2764 100755
--- a/lib/cpp/test/TransportTest.cpp
+++ b/lib/cpp/test/TransportTest.cpp
@@ -16,17 +16,11 @@
* specific language governing permissions and limitations
* under the License.
*/
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE // needed for getopt_long
-#endif
-
#include <stdlib.h>
#include <time.h>
-#include <unistd.h>
-#include <getopt.h>
-#include <signal.h>
#include <sstream>
-#include <tr1/functional>
+#include <fstream>
+#include <thrift/cxxfunctional.h>
#include <boost/mpl/list.hpp>
#include <boost/shared_array.hpp>
@@ -40,10 +34,15 @@
#include <thrift/transport/TZlibTransport.h>
#include <thrift/transport/TSocket.h>
+#include <thrift/concurrency/FunctionRunner.h>
+#if _WIN32
+ #include <thrift/windows/TWinsockSingleton.h>
+#endif
+
+
using namespace apache::thrift::transport;
static boost::mt19937 rng;
-static const char* tmp_dir = "/tmp";
void initrand(unsigned int seed) {
rng.seed(seed);
@@ -83,8 +82,8 @@
return desc.str();
}
- uint32_t getMin() const { return generator_.distribution().min(); }
- uint32_t getMax() const { return generator_.distribution().max(); }
+ 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> >
@@ -206,6 +205,8 @@
typedef CoupledZlibTransportsT<CoupledMemoryBuffers>
CoupledZlibTransports;
+#ifndef _WIN32
+// FD transport doesn't make much sense on Windows.
/**
* Coupled TFDTransports.
*/
@@ -222,6 +223,7 @@
out.reset(new TFDTransport(pipes[1], TFDTransport::CLOSE_ON_DESTROY));
}
};
+#endif
/**
* Coupled TSockets
@@ -229,8 +231,8 @@
class CoupledSocketTransports : public CoupledTransports<TSocket> {
public:
CoupledSocketTransports() {
- int sockets[2];
- if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
+ THRIFT_SOCKET sockets[2] = {0};
+ if (THRIFT_SOCKETPAIR(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
return;
}
@@ -240,20 +242,30 @@
}
};
+//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:
CoupledFileTransports() {
+#ifndef _WIN32
+ const char* tmp_dir = "/tmp";
+ #define FILENAME_SUFFIX "/thrift.transport_test"
+#else
+ const char* tmp_dir = getenv("TMP");
+ #define FILENAME_SUFFIX "\\thrift.transport_test"
+#endif
+
// Create a temporary file to use
- size_t filename_len = strlen(tmp_dir) + 32;
- filename = new char[filename_len];
- snprintf(filename, filename_len,
- "%s/thrift.transport_test.XXXXXX", tmp_dir);
- fd = mkstemp(filename);
- if (fd < 0) {
- return;
+ filename.resize(strlen(tmp_dir) + strlen(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);
}
in.reset(new TFileTransport(filename, true));
@@ -261,16 +273,12 @@
}
~CoupledFileTransports() {
- if (fd >= 0) {
- close(fd);
- unlink(filename);
- }
- delete[] filename;
+ remove(filename.c_str());
}
- char* filename;
- int fd;
+ std::string filename;
};
+#endif
/**
* Wrapper around another CoupledTransports implementation that exposes the
@@ -337,31 +345,33 @@
TriggerInfo* next;
};
-TriggerInfo* triggerInfo;
-unsigned int numTriggersFired;
+apache::thrift::concurrency::Monitor g_alarm_monitor;
+TriggerInfo* g_triggerInfo;
+unsigned int g_numTriggersFired;
+bool g_teardown = false;
-void set_alarm();
+void alarm_handler() {
+ TriggerInfo *info = NULL;
+ {
+ apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+ // The alarm timed out, which almost certainly means we're stuck
+ // on a transport that is incorrectly blocked.
+ ++g_numTriggersFired;
-void alarm_handler(int signum) {
- (void) signum;
- // The alarm timed out, which almost certainly means we're stuck
- // on a transport that is incorrectly blocked.
- ++numTriggersFired;
+ // Note: we print messages to stdout instead of stderr, since
+ // tools/test/runner only records stdout messages in the failure messages for
+ // boost tests. (boost prints its test info to stdout.)
+ printf("Timeout alarm expired; attempting to unblock transport\n");
+ if (g_triggerInfo == NULL) {
+ printf(" trigger stack is empty!\n");
+ }
- // Note: we print messages to stdout instead of stderr, since
- // tools/test/runner only records stdout messages in the failure messages for
- // boost tests. (boost prints its test info to stdout.)
- printf("Timeout alarm expired; attempting to unblock transport\n");
- if (triggerInfo == NULL) {
- printf(" trigger stack is empty!\n");
+ // Pop off the first TriggerInfo.
+ // If there is another one, schedule an alarm for it.
+ info = g_triggerInfo;
+ g_triggerInfo = info->next;
}
- // Pop off the first TriggerInfo.
- // If there is another one, schedule an alarm for it.
- TriggerInfo* info = triggerInfo;
- triggerInfo = info->next;
- set_alarm();
-
// Write some data to the transport to hopefully unblock it.
uint8_t* buf = new uint8_t[info->writeLength];
memset(buf, 'b', info->writeLength);
@@ -372,21 +382,28 @@
delete info;
}
-void set_alarm() {
- if (triggerInfo == NULL) {
- // clear any alarm
- alarm(0);
- return;
+void alarm_handler_wrapper() {
+ 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_triggerInfo == NULL) {
+ timeout = 0;
+ } else {
+ timeout = g_triggerInfo->timeoutSeconds * 1000;
+ }
+
+ int waitResult = g_alarm_monitor.waitForTimeRelative(timeout);
+ if(waitResult == THRIFT_ETIMEDOUT)
+ fireHandler = true;
+ }
+ if(fireHandler)
+ alarm_handler(); //calling outside the lock
}
-
- struct sigaction action;
- memset(&action, 0, sizeof(action));
- action.sa_handler = alarm_handler;
- action.sa_flags = SA_RESETHAND;
- sigemptyset(&action.sa_mask);
- sigaction(SIGALRM, &action, NULL);
-
- alarm(triggerInfo->timeoutSeconds);
}
/**
@@ -401,28 +418,34 @@
const boost::shared_ptr<TTransport> &transport,
uint32_t write_len) {
TriggerInfo* info = new TriggerInfo(seconds, transport, write_len);
-
- if (triggerInfo == NULL) {
- // This is the first trigger.
- // Set triggerInfo, and schedule the alarm
- triggerInfo = info;
- set_alarm();
- } else {
- // Add this trigger to the end of the list
- TriggerInfo* prev = triggerInfo;
- while (prev->next) {
- prev = prev->next;
+ {
+ apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+ if (g_triggerInfo == NULL) {
+ // This is the first trigger.
+ // Set g_triggerInfo, and schedule the alarm
+ g_triggerInfo = info;
+ g_alarm_monitor.notify();
+ } else {
+ // Add this trigger to the end of the list
+ TriggerInfo* prev = g_triggerInfo;
+ while (prev->next) {
+ prev = prev->next;
+ }
+ prev->next = info;
}
-
- prev->next = info;
}
}
void clear_triggers() {
- TriggerInfo *info = triggerInfo;
- alarm(0);
- triggerInfo = NULL;
- numTriggersFired = 0;
+ TriggerInfo *info = NULL;
+
+ {
+ apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+ info = g_triggerInfo;
+ g_triggerInfo = NULL;
+ g_numTriggersFired = 0;
+ g_alarm_monitor.notify();
+ }
while (info != NULL) {
TriggerInfo* next = info->next;
@@ -586,7 +609,7 @@
transports.out->flush();
set_trigger(3, transports.out, 1);
uint32_t bytes_read = transports.in->read(read_buf, 10);
- BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 9);
clear_triggers();
@@ -608,13 +631,13 @@
// Read 1 byte, to force the transport to read the frame
uint32_t bytes_read = transports.in->read(read_buf, 1);
- BOOST_CHECK_EQUAL(bytes_read, 1);
+ BOOST_CHECK_EQUAL(bytes_read, 1u);
// Read more than what is remaining and verify the transport does not block
set_trigger(3, transports.out, 1);
bytes_read = transports.in->read(read_buf, 10);
- BOOST_CHECK_EQUAL(numTriggersFired, 0);
- BOOST_CHECK_EQUAL(bytes_read, 9);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, 0u);
+ BOOST_CHECK_EQUAL(bytes_read, 9u);
clear_triggers();
}
@@ -666,7 +689,7 @@
while (total_read < 9) {
set_trigger(3, transports.out, 1);
bytes_read = transports.in->read(read_buf, 10);
- BOOST_REQUIRE_EQUAL(numTriggersFired, (unsigned int) 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);
@@ -694,7 +717,7 @@
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(numTriggersFired, (unsigned int) 0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
BOOST_CHECK(borrowed_buf == NULL);
clear_triggers();
@@ -720,10 +743,10 @@
add_trigger(1, transports.out, 8);
uint32_t bytes_read = transports.in->read(read_buf, 10);
if (bytes_read == 0) {
- BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
clear_triggers();
} else {
- BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 1);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 1);
BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 2);
}
@@ -744,7 +767,7 @@
uint32_t borrow_len = 10;
const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len);
BOOST_CHECK(borrowed_buf == NULL);
- BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0);
+ BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int) 0);
clear_triggers();
}
@@ -828,6 +851,7 @@
TEST_BLOCKING_BEHAVIOR(CoupledMemoryBuffers);
+#ifndef _WIN32
// TFDTransport tests
// Since CoupledFDTransports tests with a pipe, writes will block
// if there is too much outstanding unread data in the pipe.
@@ -851,6 +875,7 @@
rand4k, rand4k, fd_max_outstanding);
TEST_BLOCKING_BEHAVIOR(CoupledFDTransports);
+#endif //_WIN32
// TSocket tests
uint32_t socket_max_outstanding = 4096;
@@ -876,6 +901,8 @@
TEST_BLOCKING_BEHAVIOR(CoupledSocketTransports);
+//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.
//
@@ -893,6 +920,7 @@
TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k);
TEST_BLOCKING_BEHAVIOR(CoupledFileTransports);
+#endif
// Add some tests that access TBufferedTransport and TFramedTransport
// via TTransport pointers and TBufferBase pointers.
@@ -928,7 +956,7 @@
maxOutstanding << ")";
boost::unit_test::callback0<> test_func =
- std::tr1::bind(test_rw<CoupledTransports>, totalSize,
+ apache::thrift::stdcxx::bind(test_rw<CoupledTransports>, totalSize,
wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen,
maxOutstanding);
boost::unit_test::test_case* tc =
@@ -942,37 +970,37 @@
char name[1024];
boost::unit_test::test_case* tc;
- snprintf(name, sizeof(name), "%s::test_read_part_available()",
+ 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);
- snprintf(name, sizeof(name), "%s::test_read_part_available_in_chunks()",
+ 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);
- snprintf(name, sizeof(name), "%s::test_read_partial_midframe()",
+ 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);
- snprintf(name, sizeof(name), "%s::test_read_none_available()",
+ 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);
- snprintf(name, sizeof(name), "%s::test_borrow_part_available()",
+ 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);
- snprintf(name, sizeof(name), "%s::test_borrow_none_available()",
+ THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_none_available()",
transportName);
tc = boost::unit_test::make_test_case(
test_borrow_none_available<CoupledTransports>, name);
@@ -991,103 +1019,43 @@
* General Initialization
**************************************************************************/
-void print_usage(FILE* f, const char* argv0) {
- fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
- fprintf(f, "Options:\n");
- fprintf(f, " --seed=<N>, -s <N>\n");
- fprintf(f, " --tmp-dir=DIR, -t DIR\n");
- fprintf(f, " --help\n");
-}
+struct global_fixture {
+ boost::shared_ptr<apache::thrift::concurrency::Thread> alarmThread_;
+ global_fixture() {
+ #if _WIN32
+ apache::thrift::transport::TWinsockSingleton::create();
+ #endif
-struct Options {
- int seed;
- bool haveSeed;
- float sizeMultiplier;
+ apache::thrift::concurrency::PlatformThreadFactory factory;
+ factory.setDetached(false);
+
+ alarmThread_ = factory.newThread(
+ apache::thrift::concurrency::FunctionRunner::create(alarm_handler_wrapper));
+ alarmThread_->start();
+ }
+ ~global_fixture() {
+ {
+ apache::thrift::concurrency::Synchronized s(g_alarm_monitor);
+ g_teardown = true;
+ g_alarm_monitor.notify();
+ }
+ alarmThread_->join();
+ }
};
-void parse_args(int argc, char* argv[], Options* options) {
- bool have_seed = false;
- options->sizeMultiplier = 1;
-
- struct option long_opts[] = {
- { "help", false, NULL, 'h' },
- { "seed", true, NULL, 's' },
- { "tmp-dir", true, NULL, 't' },
- { "size-multiplier", true, NULL, 'x' },
- { NULL, 0, NULL, 0 }
- };
-
- while (true) {
- optopt = 1;
- int optchar = getopt_long(argc, argv, "hs:t:x:", long_opts, NULL);
- if (optchar == -1) {
- break;
- }
-
- switch (optchar) {
- case 't':
- tmp_dir = optarg;
- break;
- case 's': {
- char *endptr;
- options->seed = strtol(optarg, &endptr, 0);
- if (endptr == optarg || *endptr != '\0') {
- fprintf(stderr, "invalid seed value \"%s\": must be an integer\n",
- optarg);
- exit(1);
- }
- have_seed = true;
- break;
- }
- case 'h':
- print_usage(stdout, argv[0]);
- exit(0);
- case 'x': {
- char *endptr;
- options->sizeMultiplier = strtof(optarg, &endptr);
- if (endptr == optarg || *endptr != '\0') {
- fprintf(stderr, "invalid size multiplier \"%s\": must be a number\n",
- optarg);
- exit(1);
- }
- if (options->sizeMultiplier < 0) {
- fprintf(stderr, "invalid size multiplier \"%s\": "
- "must be non-negative\n", optarg);
- exit(1);
- }
- break;
- }
- case '?':
- exit(1);
- default:
- // Only happens if someone adds another option to the optarg string,
- // but doesn't update the switch statement to handle it.
- fprintf(stderr, "unknown option \"-%c\"\n", optchar);
- exit(1);
- }
- }
-
- if (!have_seed) {
- // choose a seed now if the user didn't specify one
- struct timeval tv;
- struct timezone tz;
- gettimeofday(&tv, &tz);
- options->seed = tv.tv_sec ^ tv.tv_usec;
- }
-}
+BOOST_GLOBAL_FIXTURE(global_fixture)
boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
- // Parse arguments
- Options options;
- parse_args(argc, argv, &options);
+ struct timeval tv;
+ THRIFT_GETTIMEOFDAY(&tv, NULL);
+ int seed = tv.tv_sec ^ tv.tv_usec;
- initrand(options.seed);
+ initrand(seed);
boost::unit_test::test_suite* suite =
&boost::unit_test::framework::master_test_suite();
suite->p_name.value = "TransportTest";
- TransportTestGen transport_test_generator(suite, options.sizeMultiplier);
+ TransportTestGen transport_test_generator(suite, 1);
transport_test_generator.generate();
-
return NULL;
}