Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #define BOOST_TEST_MODULE TServerIntegrationTest |
| 21 | #include <boost/test/auto_unit_test.hpp> |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 22 | #include <boost/atomic.hpp> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 23 | #include <boost/bind.hpp> |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 24 | #include <boost/date_time/posix_time/ptime.hpp> |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 25 | #include <boost/foreach.hpp> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 26 | #include <boost/format.hpp> |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 27 | #include <boost/make_shared.hpp> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 28 | #include <boost/shared_ptr.hpp> |
| 29 | #include <boost/thread.hpp> |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 30 | #include <thrift/server/TSimpleServer.h> |
| 31 | #include <thrift/server/TThreadPoolServer.h> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 32 | #include <thrift/server/TThreadedServer.h> |
| 33 | #include <thrift/protocol/TBinaryProtocol.h> |
| 34 | #include <thrift/transport/TServerSocket.h> |
| 35 | #include <thrift/transport/TSocket.h> |
| 36 | #include <thrift/transport/TTransport.h> |
| 37 | #include "gen-cpp/ParentService.h" |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 38 | #include <string> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 39 | #include <vector> |
| 40 | |
| 41 | using apache::thrift::concurrency::Guard; |
| 42 | using apache::thrift::concurrency::Monitor; |
| 43 | using apache::thrift::concurrency::Mutex; |
| 44 | using apache::thrift::concurrency::Synchronized; |
| 45 | using apache::thrift::protocol::TBinaryProtocol; |
| 46 | using apache::thrift::protocol::TBinaryProtocolFactory; |
| 47 | using apache::thrift::protocol::TProtocol; |
| 48 | using apache::thrift::protocol::TProtocolFactory; |
| 49 | using apache::thrift::transport::TServerSocket; |
| 50 | using apache::thrift::transport::TServerTransport; |
| 51 | using apache::thrift::transport::TSocket; |
| 52 | using apache::thrift::transport::TTransport; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 53 | using apache::thrift::transport::TTransportException; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 54 | using apache::thrift::transport::TTransportFactory; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 55 | using apache::thrift::server::TServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 56 | using apache::thrift::server::TServerEventHandler; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 57 | using apache::thrift::server::TSimpleServer; |
| 58 | using apache::thrift::server::TThreadPoolServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 59 | using apache::thrift::server::TThreadedServer; |
| 60 | using apache::thrift::test::ParentServiceClient; |
| 61 | using apache::thrift::test::ParentServiceIf; |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 62 | using apache::thrift::test::ParentServiceIfFactory; |
| 63 | using apache::thrift::test::ParentServiceIfSingletonFactory; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 64 | using apache::thrift::test::ParentServiceProcessor; |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 65 | using apache::thrift::test::ParentServiceProcessorFactory; |
| 66 | using apache::thrift::TProcessor; |
| 67 | using apache::thrift::TProcessorFactory; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 68 | using boost::posix_time::milliseconds; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * preServe runs after listen() is successful, when we can connect |
| 72 | */ |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 73 | class TServerReadyEventHandler : public TServerEventHandler, public Monitor { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 74 | public: |
| 75 | TServerReadyEventHandler() : isListening_(false), accepted_(0) {} |
| 76 | virtual ~TServerReadyEventHandler() {} |
| 77 | virtual void preServe() { |
| 78 | Synchronized sync(*this); |
| 79 | isListening_ = true; |
| 80 | notify(); |
| 81 | } |
| 82 | virtual void* createContext(boost::shared_ptr<TProtocol> input, |
| 83 | boost::shared_ptr<TProtocol> output) { |
| 84 | Synchronized sync(*this); |
| 85 | ++accepted_; |
| 86 | notify(); |
| 87 | |
| 88 | (void)input; |
| 89 | (void)output; |
| 90 | return NULL; |
| 91 | } |
| 92 | bool isListening() const { return isListening_; } |
| 93 | uint64_t acceptedCount() const { return accepted_; } |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 94 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 95 | private: |
| 96 | bool isListening_; |
| 97 | uint64_t accepted_; |
| 98 | }; |
| 99 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 100 | /** |
| 101 | * Reusing another generated test, just something to serve up |
| 102 | */ |
| 103 | class ParentHandler : public ParentServiceIf { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 104 | public: |
| 105 | ParentHandler() : generation_(0) {} |
| 106 | |
| 107 | int32_t incrementGeneration() { |
| 108 | Guard g(mutex_); |
| 109 | return ++generation_; |
| 110 | } |
| 111 | |
| 112 | int32_t getGeneration() { |
| 113 | Guard g(mutex_); |
| 114 | return generation_; |
| 115 | } |
| 116 | |
| 117 | void addString(const std::string& s) { |
| 118 | Guard g(mutex_); |
| 119 | strings_.push_back(s); |
| 120 | } |
| 121 | |
| 122 | void getStrings(std::vector<std::string>& _return) { |
| 123 | Guard g(mutex_); |
| 124 | _return = strings_; |
| 125 | } |
| 126 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 127 | void getDataWait(std::string& _return, const int32_t length) { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 128 | THRIFT_UNUSED_VARIABLE(_return); |
| 129 | THRIFT_UNUSED_VARIABLE(length); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 132 | void onewayWait() {} |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 133 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 134 | void exceptionWait(const std::string& message) { THRIFT_UNUSED_VARIABLE(message); } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 135 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 136 | void unexpectedExceptionWait(const std::string& message) { THRIFT_UNUSED_VARIABLE(message); } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 137 | |
| 138 | protected: |
| 139 | Mutex mutex_; |
| 140 | int32_t generation_; |
| 141 | std::vector<std::string> strings_; |
| 142 | }; |
| 143 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 144 | void autoSocketCloser(TSocket* pSock) { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 145 | pSock->close(); |
| 146 | delete pSock; |
| 147 | } |
| 148 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 149 | template <class TServerType> |
John Sirois | 919752c | 2016-02-13 12:35:58 -0700 | [diff] [blame] | 150 | class TServerIntegrationTestFixture { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 151 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 152 | TServerIntegrationTestFixture(const boost::shared_ptr<TProcessorFactory>& _processorFactory) |
| 153 | : pServer(new TServerType(_processorFactory, |
| 154 | boost::shared_ptr<TServerTransport>( |
John Sirois | 919752c | 2016-02-13 12:35:58 -0700 | [diff] [blame] | 155 | new TServerSocket("localhost", 0)), |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 156 | boost::shared_ptr<TTransportFactory>(new TTransportFactory), |
| 157 | boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory))), |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 158 | pEventHandler(boost::shared_ptr<TServerReadyEventHandler>(new TServerReadyEventHandler)), |
| 159 | bStressDone(false), |
| 160 | bStressConnectionCount(0), |
| 161 | bStressRequestCount(0) { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 162 | pServer->setServerEventHandler(pEventHandler); |
| 163 | } |
| 164 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 165 | TServerIntegrationTestFixture(const boost::shared_ptr<TProcessor>& _processor) |
| 166 | : pServer( |
| 167 | new TServerType(_processor, |
| 168 | boost::shared_ptr<TServerTransport>(new TServerSocket("localhost", 0)), |
| 169 | boost::shared_ptr<TTransportFactory>(new TTransportFactory), |
| 170 | boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory))), |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 171 | pEventHandler(boost::shared_ptr<TServerReadyEventHandler>(new TServerReadyEventHandler)), |
| 172 | bStressDone(false), |
| 173 | bStressConnectionCount(0), |
| 174 | bStressRequestCount(0) { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 175 | pServer->setServerEventHandler(pEventHandler); |
| 176 | } |
| 177 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 178 | void startServer() { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 179 | pServerThread.reset(new boost::thread(boost::bind(&TServerType::serve, pServer.get()))); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 180 | |
| 181 | // block until listen() completes so clients will be able to connect |
| 182 | Synchronized sync(*(pEventHandler.get())); |
| 183 | while (!pEventHandler->isListening()) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 184 | pEventHandler->wait(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 187 | BOOST_TEST_MESSAGE(" server is listening"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | void blockUntilAccepted(uint64_t numAccepted) { |
| 191 | Synchronized sync(*(pEventHandler.get())); |
| 192 | while (pEventHandler->acceptedCount() < numAccepted) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 193 | pEventHandler->wait(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 194 | } |
| 195 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 196 | BOOST_TEST_MESSAGE(boost::format(" server has accepted %1%") % numAccepted); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void stopServer() { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 200 | if (pServerThread) { |
| 201 | pServer->stop(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 202 | BOOST_TEST_MESSAGE(" server stop completed"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 203 | |
| 204 | pServerThread->join(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 205 | BOOST_TEST_MESSAGE(" server thread joined"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 206 | pServerThread.reset(); |
| 207 | } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 210 | ~TServerIntegrationTestFixture() { stopServer(); } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 211 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 212 | /** |
| 213 | * Performs a baseline test where some clients are opened and issue a single operation |
| 214 | * and then disconnect at different intervals. |
| 215 | * \param[in] numToMake the number of concurrent clients |
| 216 | * \param[in] expectedHWM the high water mark we expect of concurrency |
| 217 | * \param[in] purpose a description of the test for logging purposes |
| 218 | */ |
| 219 | void baseline(int64_t numToMake, int64_t expectedHWM, const std::string& purpose) { |
| 220 | BOOST_TEST_MESSAGE(boost::format("Testing %1%: %2% with %3% clients, expect %4% HWM") |
| 221 | % typeid(TServerType).name() % purpose % numToMake % expectedHWM); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 222 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 223 | startServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 224 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 225 | std::vector<boost::shared_ptr<TSocket> > holdSockets; |
| 226 | std::vector<boost::shared_ptr<boost::thread> > holdThreads; |
| 227 | |
| 228 | for (int64_t i = 0; i < numToMake; ++i) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 229 | boost::shared_ptr<TSocket> pClientSock(new TSocket("localhost", getServerPort()), |
| 230 | autoSocketCloser); |
| 231 | holdSockets.push_back(pClientSock); |
| 232 | boost::shared_ptr<TProtocol> pClientProtocol(new TBinaryProtocol(pClientSock)); |
| 233 | ParentServiceClient client(pClientProtocol); |
| 234 | pClientSock->open(); |
| 235 | client.incrementGeneration(); |
| 236 | holdThreads.push_back(boost::shared_ptr<boost::thread>( |
| 237 | new boost::thread(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 238 | this, |
| 239 | pClientSock, |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 240 | milliseconds(10 * numToMake))))); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | BOOST_CHECK_EQUAL(expectedHWM, pServer->getConcurrentClientCountHWM()); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 244 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 245 | BOOST_FOREACH (boost::shared_ptr<boost::thread> pThread, holdThreads) { pThread->join(); } |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 246 | holdThreads.clear(); |
| 247 | holdSockets.clear(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 248 | |
| 249 | stopServer(); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Helper method used to close a connection after a delay. |
| 254 | * \param[in] toClose the connection to close |
| 255 | * \param[in] after the delay to impose |
| 256 | */ |
| 257 | void delayClose(boost::shared_ptr<TTransport> toClose, boost::posix_time::time_duration after) { |
| 258 | boost::this_thread::sleep(after); |
| 259 | toClose->close(); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * \returns the server port number |
| 264 | */ |
| 265 | int getServerPort() { |
| 266 | TServerSocket* pSock = dynamic_cast<TServerSocket*>(pServer->getServerTransport().get()); |
| 267 | return pSock->getPort(); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Performs a stress test by spawning threads that connect, do a number of operations |
| 272 | * and disconnect, then a random delay, then do it over again. This is done for a fixed |
| 273 | * period of time to test for concurrency correctness. |
| 274 | * \param[in] numToMake the number of concurrent clients |
| 275 | */ |
| 276 | void stress(int64_t numToMake, const boost::posix_time::time_duration& duration) { |
| 277 | BOOST_TEST_MESSAGE(boost::format("Stress testing %1% with %2% clients for %3% seconds") |
| 278 | % typeid(TServerType).name() % numToMake % duration.total_seconds()); |
| 279 | |
| 280 | startServer(); |
| 281 | |
| 282 | std::vector<boost::shared_ptr<boost::thread> > holdThreads; |
| 283 | for (int64_t i = 0; i < numToMake; ++i) { |
| 284 | holdThreads.push_back(boost::shared_ptr<boost::thread>( |
| 285 | new boost::thread(boost::bind(&TServerIntegrationTestFixture::stressor, this)))); |
| 286 | } |
| 287 | |
| 288 | boost::this_thread::sleep(duration); |
| 289 | bStressDone = true; |
| 290 | |
| 291 | BOOST_TEST_MESSAGE(boost::format(" serviced %1% connections (HWM %2%) totaling %3% requests") |
| 292 | % bStressConnectionCount % pServer->getConcurrentClientCountHWM() % bStressRequestCount); |
| 293 | |
| 294 | BOOST_FOREACH (boost::shared_ptr<boost::thread> pThread, holdThreads) { pThread->join(); } |
| 295 | holdThreads.clear(); |
| 296 | |
| 297 | BOOST_CHECK(bStressRequestCount > 0); |
| 298 | |
| 299 | stopServer(); |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Helper method to stress the system |
| 304 | */ |
| 305 | void stressor() { |
| 306 | while (!bStressDone) { |
| 307 | boost::shared_ptr<TSocket> pSocket(new TSocket("localhost", getServerPort()), autoSocketCloser); |
| 308 | boost::shared_ptr<TProtocol> pProtocol(new TBinaryProtocol(pSocket)); |
| 309 | ParentServiceClient client(pProtocol); |
| 310 | pSocket->open(); |
| 311 | bStressConnectionCount.fetch_add(1, boost::memory_order_relaxed); |
| 312 | for (int i = 0; i < rand() % 1000; ++i) { |
| 313 | client.incrementGeneration(); |
| 314 | bStressRequestCount.fetch_add(1, boost::memory_order_relaxed); |
| 315 | } |
| 316 | } |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | boost::shared_ptr<TServerType> pServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 320 | boost::shared_ptr<TServerReadyEventHandler> pEventHandler; |
| 321 | boost::shared_ptr<boost::thread> pServerThread; |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 322 | bool bStressDone; |
| 323 | boost::atomic_int64_t bStressConnectionCount; |
| 324 | boost::atomic_int64_t bStressRequestCount; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 325 | }; |
| 326 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 327 | template <class TServerType> |
| 328 | class TServerIntegrationProcessorFactoryTestFixture |
| 329 | : public TServerIntegrationTestFixture<TServerType> { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 330 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 331 | TServerIntegrationProcessorFactoryTestFixture() |
| 332 | : TServerIntegrationTestFixture<TServerType>(boost::make_shared<ParentServiceProcessorFactory>( |
| 333 | boost::make_shared<ParentServiceIfSingletonFactory>( |
| 334 | boost::make_shared<ParentHandler>()))) {} |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 335 | }; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 336 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 337 | template <class TServerType> |
| 338 | class TServerIntegrationProcessorTestFixture : public TServerIntegrationTestFixture<TServerType> { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 339 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 340 | TServerIntegrationProcessorTestFixture() |
| 341 | : TServerIntegrationTestFixture<TServerType>( |
| 342 | boost::make_shared<ParentServiceProcessor>(boost::make_shared<ParentHandler>())) {} |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | BOOST_AUTO_TEST_SUITE(constructors) |
| 346 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 347 | BOOST_FIXTURE_TEST_CASE(test_simple_factory, |
| 348 | TServerIntegrationProcessorFactoryTestFixture<TSimpleServer>) { |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 349 | baseline(3, 1, "factory"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 350 | } |
| 351 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 352 | BOOST_FIXTURE_TEST_CASE(test_simple, TServerIntegrationProcessorTestFixture<TSimpleServer>) { |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 353 | baseline(3, 1, "processor"); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 356 | BOOST_FIXTURE_TEST_CASE(test_threaded_factory, |
| 357 | TServerIntegrationProcessorFactoryTestFixture<TThreadedServer>) { |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 358 | baseline(10, 10, "factory"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 359 | } |
| 360 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 361 | BOOST_FIXTURE_TEST_CASE(test_threaded, TServerIntegrationProcessorTestFixture<TThreadedServer>) { |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 362 | baseline(10, 10, "processor"); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 365 | BOOST_FIXTURE_TEST_CASE(test_threaded_bound, |
| 366 | TServerIntegrationProcessorTestFixture<TThreadedServer>) { |
| 367 | pServer->setConcurrentClientLimit(4); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 368 | baseline(10, 4, "limit by server framework"); |
| 369 | } |
| 370 | |
| 371 | BOOST_FIXTURE_TEST_CASE(test_threaded_stress, |
| 372 | TServerIntegrationProcessorFactoryTestFixture<TThreadedServer>) { |
| 373 | stress(10, boost::posix_time::seconds(3)); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 374 | } |
| 375 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 376 | BOOST_FIXTURE_TEST_CASE(test_threadpool_factory, |
| 377 | TServerIntegrationProcessorFactoryTestFixture<TThreadPoolServer>) { |
| 378 | pServer->getThreadManager()->threadFactory( |
| 379 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 380 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 381 | pServer->getThreadManager()->start(); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 382 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 383 | // thread factory has 4 threads as a default |
| 384 | // thread factory however is a bad way to limit concurrent clients |
| 385 | // as accept() will be called to grab a 5th client socket, in this case |
| 386 | // and then the thread factory will block adding the thread to manage |
| 387 | // that client. |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 388 | baseline(10, 5, "limit by thread manager"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 389 | } |
| 390 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 391 | BOOST_FIXTURE_TEST_CASE(test_threadpool, |
| 392 | TServerIntegrationProcessorTestFixture<TThreadPoolServer>) { |
| 393 | pServer->getThreadManager()->threadFactory( |
| 394 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 395 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 396 | pServer->getThreadManager()->start(); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 397 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 398 | // thread factory has 4 threads as a default |
| 399 | // thread factory however is a bad way to limit concurrent clients |
| 400 | // as accept() will be called to grab a 5th client socket, in this case |
| 401 | // and then the thread factory will block adding the thread to manage |
| 402 | // that client. |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 403 | baseline(10, 5, "limit by thread manager"); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 406 | BOOST_FIXTURE_TEST_CASE(test_threadpool_bound, |
| 407 | TServerIntegrationProcessorTestFixture<TThreadPoolServer>) { |
| 408 | pServer->getThreadManager()->threadFactory( |
| 409 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 410 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 411 | pServer->getThreadManager()->start(); |
| 412 | pServer->setConcurrentClientLimit(4); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 413 | |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 414 | baseline(10, 4, "server framework connection limit"); |
| 415 | } |
| 416 | |
| 417 | BOOST_FIXTURE_TEST_CASE(test_threadpool_stress, |
| 418 | TServerIntegrationProcessorTestFixture<TThreadPoolServer>) { |
| 419 | pServer->getThreadManager()->threadFactory( |
| 420 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 421 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 422 | pServer->getThreadManager()->start(); |
| 423 | |
| 424 | stress(10, boost::posix_time::seconds(3)); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | BOOST_AUTO_TEST_SUITE_END() |
| 428 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 429 | BOOST_FIXTURE_TEST_SUITE(TServerIntegrationTest, |
| 430 | TServerIntegrationProcessorTestFixture<TThreadedServer>) |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 431 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 432 | BOOST_AUTO_TEST_CASE(test_stop_with_interruptable_clients_connected) { |
| 433 | // This tests THRIFT-2441 new behavior: stopping the server disconnects clients |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 434 | BOOST_TEST_MESSAGE("Testing stop with interruptable clients"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 435 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 436 | startServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 437 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 438 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 439 | autoSocketCloser); |
| 440 | pClientSock1->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 441 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 442 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 443 | autoSocketCloser); |
| 444 | pClientSock2->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 445 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 446 | // Ensure they have been accepted |
| 447 | blockUntilAccepted(2); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 448 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 449 | // The test fixture destructor will force the sockets to disconnect |
| 450 | // Prior to THRIFT-2441, pServer->stop() would hang until clients disconnected |
| 451 | stopServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 452 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 453 | // extra proof the server end disconnected the clients |
| 454 | uint8_t buf[1]; |
| 455 | BOOST_CHECK_EQUAL(0, pClientSock1->read(&buf[0], 1)); // 0 = disconnected |
| 456 | BOOST_CHECK_EQUAL(0, pClientSock2->read(&buf[0], 1)); // 0 = disconnected |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 457 | } |
| 458 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 459 | BOOST_AUTO_TEST_CASE(test_stop_with_uninterruptable_clients_connected) { |
| 460 | // This tests pre-THRIFT-2441 behavior: stopping the server blocks until clients |
| 461 | // disconnect. |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 462 | BOOST_TEST_MESSAGE("Testing stop with uninterruptable clients"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 463 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 464 | boost::dynamic_pointer_cast<TServerSocket>(pServer->getServerTransport()) |
| 465 | ->setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 466 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 467 | startServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 468 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 469 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 470 | autoSocketCloser); |
| 471 | pClientSock1->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 472 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 473 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 474 | autoSocketCloser); |
| 475 | pClientSock2->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 476 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 477 | // Ensure they have been accepted |
| 478 | blockUntilAccepted(2); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 479 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 480 | boost::thread t1(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 481 | this, |
| 482 | pClientSock1, |
| 483 | milliseconds(250))); |
| 484 | boost::thread t2(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 485 | this, |
| 486 | pClientSock2, |
| 487 | milliseconds(250))); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 488 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 489 | // Once the clients disconnect the server will stop |
| 490 | stopServer(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 491 | BOOST_CHECK(pServer->getConcurrentClientCountHWM() > 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 492 | t1.join(); |
| 493 | t2.join(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 494 | } |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 495 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 496 | BOOST_AUTO_TEST_CASE(test_concurrent_client_limit) { |
| 497 | startServer(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 498 | BOOST_TEST_MESSAGE("Testing the concurrent client limit"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 499 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 500 | BOOST_CHECK_EQUAL(INT64_MAX, pServer->getConcurrentClientLimit()); |
| 501 | pServer->setConcurrentClientLimit(2); |
| 502 | BOOST_CHECK_EQUAL(0, pServer->getConcurrentClientCount()); |
| 503 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientLimit()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 504 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 505 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 506 | autoSocketCloser); |
| 507 | pClientSock1->open(); |
| 508 | blockUntilAccepted(1); |
| 509 | BOOST_CHECK_EQUAL(1, pServer->getConcurrentClientCount()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 510 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 511 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 512 | autoSocketCloser); |
| 513 | pClientSock2->open(); |
| 514 | blockUntilAccepted(2); |
| 515 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCount()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 516 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 517 | // a third client cannot connect until one of the other two closes |
| 518 | boost::thread t2(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 519 | this, |
| 520 | pClientSock2, |
| 521 | milliseconds(250))); |
| 522 | boost::shared_ptr<TSocket> pClientSock3(new TSocket("localhost", getServerPort()), |
| 523 | autoSocketCloser); |
| 524 | pClientSock2->open(); |
| 525 | blockUntilAccepted(2); |
| 526 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCount()); |
| 527 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCountHWM()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 528 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 529 | stopServer(); |
Jim King | 5a3f855 | 2016-04-05 12:17:51 -0400 | [diff] [blame] | 530 | BOOST_CHECK(pServer->getConcurrentClientCountHWM() > 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 531 | t2.join(); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 534 | BOOST_AUTO_TEST_SUITE_END() |