THRIFT-3626 Fix lib/cpp/test/TSocketInterruptTest.cpp to use ephemeral ports.
Client: Test (C++)
Patch: John Sirois
Instead of using a fixed port, use an ephemeral port to improve
robustness and make way for parallelizability.
This change has an undesirable amount of repeated code but this fact is
not different from the prior so being a bit expedient here. Ideally
setup of a `TServerSocket` listening on an ephemeral port would be DRYed
up into a helper of some sort.
This closes #857
diff --git a/lib/cpp/test/TSocketInterruptTest.cpp b/lib/cpp/test/TSocketInterruptTest.cpp
index 02da3ee..27b89eb 100644
--- a/lib/cpp/test/TSocketInterruptTest.cpp
+++ b/lib/cpp/test/TSocketInterruptTest.cpp
@@ -26,14 +26,13 @@
#include <boost/thread/thread.hpp>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TServerSocket.h>
-#include "TestPortFixture.h"
using apache::thrift::transport::TServerSocket;
using apache::thrift::transport::TSocket;
using apache::thrift::transport::TTransport;
using apache::thrift::transport::TTransportException;
-BOOST_FIXTURE_TEST_SUITE(TSocketInterruptTest, TestPortFixture)
+BOOST_AUTO_TEST_SUITE(TSocketInterruptTest)
void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) {
uint8_t buf[4];
@@ -51,9 +50,10 @@
}
BOOST_AUTO_TEST_CASE(test_interruptable_child_read) {
- TServerSocket sock1("localhost", m_serverPort);
+ TServerSocket sock1("localhost", 0);
sock1.listen();
- TSocket clientSock("localhost", m_serverPort);
+ int port = sock1.getPort();
+ TSocket clientSock("localhost", port);
clientSock.open();
boost::shared_ptr<TTransport> accepted = sock1.accept();
boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted));
@@ -68,10 +68,11 @@
}
BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) {
- TServerSocket sock1("localhost", m_serverPort);
+ TServerSocket sock1("localhost", 0);
sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
sock1.listen();
- TSocket clientSock("localhost", m_serverPort);
+ int port = sock1.getPort();
+ TSocket clientSock("localhost", port);
clientSock.open();
boost::shared_ptr<TTransport> accepted = sock1.accept();
boost::thread readThread(boost::bind(readerWorker, accepted, 0));
@@ -89,7 +90,7 @@
}
BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) {
- TServerSocket sock1("localhost", m_serverPort);
+ TServerSocket sock1("localhost", 0);
sock1.listen();
BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
sock1.close();
@@ -100,9 +101,10 @@
}
BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) {
- TServerSocket sock1("localhost", m_serverPort);
+ TServerSocket sock1("localhost", 0);
sock1.listen();
- TSocket clientSock("localhost", m_serverPort);
+ int port = sock1.getPort();
+ TSocket clientSock("localhost", port);
clientSock.open();
boost::shared_ptr<TTransport> accepted = sock1.accept();
// peek() will return false if child is interrupted
@@ -118,10 +120,11 @@
}
BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) {
- TServerSocket sock1("localhost", m_serverPort);
+ TServerSocket sock1("localhost", 0);
sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
sock1.listen();
- TSocket clientSock("localhost", m_serverPort);
+ int port = sock1.getPort();
+ TSocket clientSock("localhost", port);
clientSock.open();
boost::shared_ptr<TTransport> accepted = sock1.accept();
// peek() will return false when remote side is closed