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> |
| 22 | #include <boost/bind.hpp> |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 23 | #include <boost/foreach.hpp> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 24 | #include <boost/format.hpp> |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 25 | #include <boost/make_shared.hpp> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 26 | #include <boost/shared_ptr.hpp> |
| 27 | #include <boost/thread.hpp> |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 28 | #include <thrift/server/TSimpleServer.h> |
| 29 | #include <thrift/server/TThreadPoolServer.h> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 30 | #include <thrift/server/TThreadedServer.h> |
| 31 | #include <thrift/protocol/TBinaryProtocol.h> |
| 32 | #include <thrift/transport/TServerSocket.h> |
| 33 | #include <thrift/transport/TSocket.h> |
| 34 | #include <thrift/transport/TTransport.h> |
| 35 | #include "gen-cpp/ParentService.h" |
| 36 | #include "TestPortFixture.h" |
| 37 | #include <vector> |
| 38 | |
| 39 | using apache::thrift::concurrency::Guard; |
| 40 | using apache::thrift::concurrency::Monitor; |
| 41 | using apache::thrift::concurrency::Mutex; |
| 42 | using apache::thrift::concurrency::Synchronized; |
| 43 | using apache::thrift::protocol::TBinaryProtocol; |
| 44 | using apache::thrift::protocol::TBinaryProtocolFactory; |
| 45 | using apache::thrift::protocol::TProtocol; |
| 46 | using apache::thrift::protocol::TProtocolFactory; |
| 47 | using apache::thrift::transport::TServerSocket; |
| 48 | using apache::thrift::transport::TServerTransport; |
| 49 | using apache::thrift::transport::TSocket; |
| 50 | using apache::thrift::transport::TTransport; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 51 | using apache::thrift::transport::TTransportException; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 52 | using apache::thrift::transport::TTransportFactory; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 53 | using apache::thrift::server::TServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 54 | using apache::thrift::server::TServerEventHandler; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 55 | using apache::thrift::server::TSimpleServer; |
| 56 | using apache::thrift::server::TThreadPoolServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 57 | using apache::thrift::server::TThreadedServer; |
| 58 | using apache::thrift::test::ParentServiceClient; |
| 59 | using apache::thrift::test::ParentServiceIf; |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 60 | using apache::thrift::test::ParentServiceIfFactory; |
| 61 | using apache::thrift::test::ParentServiceIfSingletonFactory; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 62 | using apache::thrift::test::ParentServiceProcessor; |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 63 | using apache::thrift::test::ParentServiceProcessorFactory; |
| 64 | using apache::thrift::TProcessor; |
| 65 | using apache::thrift::TProcessorFactory; |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 66 | using boost::posix_time::milliseconds; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * preServe runs after listen() is successful, when we can connect |
| 70 | */ |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 71 | class TServerReadyEventHandler : public TServerEventHandler, public Monitor { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 72 | public: |
| 73 | TServerReadyEventHandler() : isListening_(false), accepted_(0) {} |
| 74 | virtual ~TServerReadyEventHandler() {} |
| 75 | virtual void preServe() { |
| 76 | Synchronized sync(*this); |
| 77 | isListening_ = true; |
| 78 | notify(); |
| 79 | } |
| 80 | virtual void* createContext(boost::shared_ptr<TProtocol> input, |
| 81 | boost::shared_ptr<TProtocol> output) { |
| 82 | Synchronized sync(*this); |
| 83 | ++accepted_; |
| 84 | notify(); |
| 85 | |
| 86 | (void)input; |
| 87 | (void)output; |
| 88 | return NULL; |
| 89 | } |
| 90 | bool isListening() const { return isListening_; } |
| 91 | uint64_t acceptedCount() const { return accepted_; } |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 92 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 93 | private: |
| 94 | bool isListening_; |
| 95 | uint64_t accepted_; |
| 96 | }; |
| 97 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 98 | /** |
| 99 | * Reusing another generated test, just something to serve up |
| 100 | */ |
| 101 | class ParentHandler : public ParentServiceIf { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 102 | public: |
| 103 | ParentHandler() : generation_(0) {} |
| 104 | |
| 105 | int32_t incrementGeneration() { |
| 106 | Guard g(mutex_); |
| 107 | return ++generation_; |
| 108 | } |
| 109 | |
| 110 | int32_t getGeneration() { |
| 111 | Guard g(mutex_); |
| 112 | return generation_; |
| 113 | } |
| 114 | |
| 115 | void addString(const std::string& s) { |
| 116 | Guard g(mutex_); |
| 117 | strings_.push_back(s); |
| 118 | } |
| 119 | |
| 120 | void getStrings(std::vector<std::string>& _return) { |
| 121 | Guard g(mutex_); |
| 122 | _return = strings_; |
| 123 | } |
| 124 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 125 | void getDataWait(std::string& _return, const int32_t length) { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 126 | THRIFT_UNUSED_VARIABLE(_return); |
| 127 | THRIFT_UNUSED_VARIABLE(length); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 130 | void onewayWait() {} |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 131 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 132 | void exceptionWait(const std::string& message) { THRIFT_UNUSED_VARIABLE(message); } |
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 unexpectedExceptionWait(const std::string& message) { THRIFT_UNUSED_VARIABLE(message); } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 135 | |
| 136 | protected: |
| 137 | Mutex mutex_; |
| 138 | int32_t generation_; |
| 139 | std::vector<std::string> strings_; |
| 140 | }; |
| 141 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 142 | void autoSocketCloser(TSocket* pSock) { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 143 | pSock->close(); |
| 144 | delete pSock; |
| 145 | } |
| 146 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 147 | template <class TServerType> |
| 148 | class TServerIntegrationTestFixture : public TestPortFixture { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 149 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 150 | TServerIntegrationTestFixture(const boost::shared_ptr<TProcessorFactory>& _processorFactory) |
| 151 | : pServer(new TServerType(_processorFactory, |
| 152 | boost::shared_ptr<TServerTransport>( |
| 153 | new TServerSocket("localhost", m_serverPort)), |
| 154 | boost::shared_ptr<TTransportFactory>(new TTransportFactory), |
| 155 | boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory))), |
| 156 | pEventHandler(boost::shared_ptr<TServerReadyEventHandler>(new TServerReadyEventHandler)) { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 157 | pServer->setServerEventHandler(pEventHandler); |
| 158 | } |
| 159 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 160 | TServerIntegrationTestFixture(const boost::shared_ptr<TProcessor>& _processor) |
| 161 | : pServer( |
| 162 | new TServerType(_processor, |
| 163 | boost::shared_ptr<TServerTransport>(new TServerSocket("localhost", 0)), |
| 164 | boost::shared_ptr<TTransportFactory>(new TTransportFactory), |
| 165 | boost::shared_ptr<TProtocolFactory>(new TBinaryProtocolFactory))), |
| 166 | pEventHandler(boost::shared_ptr<TServerReadyEventHandler>(new TServerReadyEventHandler)) { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 167 | pServer->setServerEventHandler(pEventHandler); |
| 168 | } |
| 169 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 170 | void startServer() { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 171 | pServerThread.reset(new boost::thread(boost::bind(&TServerType::serve, pServer.get()))); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 172 | |
| 173 | // block until listen() completes so clients will be able to connect |
| 174 | Synchronized sync(*(pEventHandler.get())); |
| 175 | while (!pEventHandler->isListening()) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 176 | pEventHandler->wait(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 179 | BOOST_TEST_MESSAGE("server is listening"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void blockUntilAccepted(uint64_t numAccepted) { |
| 183 | Synchronized sync(*(pEventHandler.get())); |
| 184 | while (pEventHandler->acceptedCount() < numAccepted) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 185 | pEventHandler->wait(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 188 | BOOST_TEST_MESSAGE(boost::format("server has accepted %1%") % numAccepted); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void stopServer() { |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 192 | if (pServerThread) { |
| 193 | pServer->stop(); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 194 | BOOST_TEST_MESSAGE("server stop completed"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 195 | |
| 196 | pServerThread->join(); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 197 | BOOST_TEST_MESSAGE("server thread joined"); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 198 | pServerThread.reset(); |
| 199 | } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 202 | ~TServerIntegrationTestFixture() { stopServer(); } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 203 | |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 204 | int getServerPort() { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 205 | TServerSocket* pSock = dynamic_cast<TServerSocket*>(pServer->getServerTransport().get()); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 206 | return pSock->getPort(); |
| 207 | } |
| 208 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 209 | void delayClose(boost::shared_ptr<TTransport> toClose, boost::posix_time::time_duration after) { |
| 210 | boost::this_thread::sleep(after); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 211 | toClose->close(); |
| 212 | } |
| 213 | |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 214 | void baseline(int64_t numToMake, int64_t expectedHWM) { |
| 215 | startServer(); |
| 216 | std::vector<boost::shared_ptr<TSocket> > holdSockets; |
| 217 | std::vector<boost::shared_ptr<boost::thread> > holdThreads; |
| 218 | |
| 219 | for (int64_t i = 0; i < numToMake; ++i) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 220 | boost::shared_ptr<TSocket> pClientSock(new TSocket("localhost", getServerPort()), |
| 221 | autoSocketCloser); |
| 222 | holdSockets.push_back(pClientSock); |
| 223 | boost::shared_ptr<TProtocol> pClientProtocol(new TBinaryProtocol(pClientSock)); |
| 224 | ParentServiceClient client(pClientProtocol); |
| 225 | pClientSock->open(); |
| 226 | client.incrementGeneration(); |
| 227 | holdThreads.push_back(boost::shared_ptr<boost::thread>( |
| 228 | new boost::thread(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 229 | this, |
| 230 | pClientSock, |
| 231 | milliseconds(100 * numToMake))))); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | BOOST_CHECK_EQUAL(expectedHWM, pServer->getConcurrentClientCountHWM()); |
| 235 | stopServer(); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 236 | BOOST_FOREACH (boost::shared_ptr<boost::thread> pThread, holdThreads) { pThread->join(); } |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 237 | holdThreads.clear(); |
| 238 | holdSockets.clear(); |
| 239 | } |
| 240 | |
| 241 | boost::shared_ptr<TServerType> pServer; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 242 | boost::shared_ptr<TServerReadyEventHandler> pEventHandler; |
| 243 | boost::shared_ptr<boost::thread> pServerThread; |
| 244 | }; |
| 245 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 246 | template <class TServerType> |
| 247 | class TServerIntegrationProcessorFactoryTestFixture |
| 248 | : public TServerIntegrationTestFixture<TServerType> { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 249 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 250 | TServerIntegrationProcessorFactoryTestFixture() |
| 251 | : TServerIntegrationTestFixture<TServerType>(boost::make_shared<ParentServiceProcessorFactory>( |
| 252 | boost::make_shared<ParentServiceIfSingletonFactory>( |
| 253 | boost::make_shared<ParentHandler>()))) {} |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 254 | }; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 255 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 256 | template <class TServerType> |
| 257 | class TServerIntegrationProcessorTestFixture : public TServerIntegrationTestFixture<TServerType> { |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 258 | public: |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 259 | TServerIntegrationProcessorTestFixture() |
| 260 | : TServerIntegrationTestFixture<TServerType>( |
| 261 | boost::make_shared<ParentServiceProcessor>(boost::make_shared<ParentHandler>())) {} |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | BOOST_AUTO_TEST_SUITE(constructors) |
| 265 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 266 | BOOST_FIXTURE_TEST_CASE(test_simple_factory, |
| 267 | TServerIntegrationProcessorFactoryTestFixture<TSimpleServer>) { |
| 268 | baseline(3, 1); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 271 | BOOST_FIXTURE_TEST_CASE(test_simple, TServerIntegrationProcessorTestFixture<TSimpleServer>) { |
| 272 | baseline(3, 1); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 275 | BOOST_FIXTURE_TEST_CASE(test_threaded_factory, |
| 276 | TServerIntegrationProcessorFactoryTestFixture<TThreadedServer>) { |
| 277 | baseline(10, 10); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 278 | } |
| 279 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 280 | BOOST_FIXTURE_TEST_CASE(test_threaded, TServerIntegrationProcessorTestFixture<TThreadedServer>) { |
| 281 | baseline(10, 10); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 284 | BOOST_FIXTURE_TEST_CASE(test_threaded_bound, |
| 285 | TServerIntegrationProcessorTestFixture<TThreadedServer>) { |
| 286 | pServer->setConcurrentClientLimit(4); |
| 287 | baseline(10, 4); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 290 | BOOST_FIXTURE_TEST_CASE(test_threadpool_factory, |
| 291 | TServerIntegrationProcessorFactoryTestFixture<TThreadPoolServer>) { |
| 292 | pServer->getThreadManager()->threadFactory( |
| 293 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 294 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 295 | pServer->getThreadManager()->start(); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 296 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 297 | // thread factory has 4 threads as a default |
| 298 | // thread factory however is a bad way to limit concurrent clients |
| 299 | // as accept() will be called to grab a 5th client socket, in this case |
| 300 | // and then the thread factory will block adding the thread to manage |
| 301 | // that client. |
| 302 | baseline(10, 5); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 303 | } |
| 304 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 305 | BOOST_FIXTURE_TEST_CASE(test_threadpool, |
| 306 | TServerIntegrationProcessorTestFixture<TThreadPoolServer>) { |
| 307 | pServer->getThreadManager()->threadFactory( |
| 308 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 309 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 310 | pServer->getThreadManager()->start(); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 311 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 312 | // thread factory has 4 threads as a default |
| 313 | // thread factory however is a bad way to limit concurrent clients |
| 314 | // as accept() will be called to grab a 5th client socket, in this case |
| 315 | // and then the thread factory will block adding the thread to manage |
| 316 | // that client. |
| 317 | baseline(10, 5); |
Konrad Grochowski | 24ea0bf | 2015-05-07 14:59:29 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 320 | BOOST_FIXTURE_TEST_CASE(test_threadpool_bound, |
| 321 | TServerIntegrationProcessorTestFixture<TThreadPoolServer>) { |
| 322 | pServer->getThreadManager()->threadFactory( |
| 323 | boost::shared_ptr<apache::thrift::concurrency::ThreadFactory>( |
| 324 | new apache::thrift::concurrency::PlatformThreadFactory)); |
| 325 | pServer->getThreadManager()->start(); |
| 326 | pServer->setConcurrentClientLimit(4); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 327 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 328 | baseline(10, 4); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | BOOST_AUTO_TEST_SUITE_END() |
| 332 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 333 | BOOST_FIXTURE_TEST_SUITE(TServerIntegrationTest, |
| 334 | TServerIntegrationProcessorTestFixture<TThreadedServer>) |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 335 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 336 | BOOST_AUTO_TEST_CASE(test_stop_with_interruptable_clients_connected) { |
| 337 | // This tests THRIFT-2441 new behavior: stopping the server disconnects clients |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 338 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 339 | startServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 340 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 341 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 342 | autoSocketCloser); |
| 343 | pClientSock1->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 344 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 345 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 346 | autoSocketCloser); |
| 347 | pClientSock2->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 348 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 349 | // Ensure they have been accepted |
| 350 | blockUntilAccepted(2); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 351 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 352 | // The test fixture destructor will force the sockets to disconnect |
| 353 | // Prior to THRIFT-2441, pServer->stop() would hang until clients disconnected |
| 354 | stopServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 355 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 356 | // extra proof the server end disconnected the clients |
| 357 | uint8_t buf[1]; |
| 358 | BOOST_CHECK_EQUAL(0, pClientSock1->read(&buf[0], 1)); // 0 = disconnected |
| 359 | BOOST_CHECK_EQUAL(0, pClientSock2->read(&buf[0], 1)); // 0 = disconnected |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 362 | BOOST_AUTO_TEST_CASE(test_stop_with_uninterruptable_clients_connected) { |
| 363 | // This tests pre-THRIFT-2441 behavior: stopping the server blocks until clients |
| 364 | // disconnect. |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 365 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 366 | boost::dynamic_pointer_cast<TServerSocket>(pServer->getServerTransport()) |
| 367 | ->setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 368 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 369 | startServer(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 370 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 371 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 372 | autoSocketCloser); |
| 373 | pClientSock1->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 374 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 375 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 376 | autoSocketCloser); |
| 377 | pClientSock2->open(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 378 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 379 | // Ensure they have been accepted |
| 380 | blockUntilAccepted(2); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 381 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 382 | boost::thread t1(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 383 | this, |
| 384 | pClientSock1, |
| 385 | milliseconds(250))); |
| 386 | boost::thread t2(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 387 | this, |
| 388 | pClientSock2, |
| 389 | milliseconds(250))); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 390 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 391 | // Once the clients disconnect the server will stop |
| 392 | stopServer(); |
| 393 | t1.join(); |
| 394 | t2.join(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 395 | } |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 396 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 397 | BOOST_AUTO_TEST_CASE(test_concurrent_client_limit) { |
| 398 | startServer(); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 399 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 400 | BOOST_CHECK_EQUAL(INT64_MAX, pServer->getConcurrentClientLimit()); |
| 401 | pServer->setConcurrentClientLimit(2); |
| 402 | BOOST_CHECK_EQUAL(0, pServer->getConcurrentClientCount()); |
| 403 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientLimit()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 404 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 405 | boost::shared_ptr<TSocket> pClientSock1(new TSocket("localhost", getServerPort()), |
| 406 | autoSocketCloser); |
| 407 | pClientSock1->open(); |
| 408 | blockUntilAccepted(1); |
| 409 | BOOST_CHECK_EQUAL(1, pServer->getConcurrentClientCount()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 410 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 411 | boost::shared_ptr<TSocket> pClientSock2(new TSocket("localhost", getServerPort()), |
| 412 | autoSocketCloser); |
| 413 | pClientSock2->open(); |
| 414 | blockUntilAccepted(2); |
| 415 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCount()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 416 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 417 | // a third client cannot connect until one of the other two closes |
| 418 | boost::thread t2(boost::bind(&TServerIntegrationTestFixture::delayClose, |
| 419 | this, |
| 420 | pClientSock2, |
| 421 | milliseconds(250))); |
| 422 | boost::shared_ptr<TSocket> pClientSock3(new TSocket("localhost", getServerPort()), |
| 423 | autoSocketCloser); |
| 424 | pClientSock2->open(); |
| 425 | blockUntilAccepted(2); |
| 426 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCount()); |
| 427 | BOOST_CHECK_EQUAL(2, pServer->getConcurrentClientCountHWM()); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 428 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 429 | stopServer(); |
| 430 | t2.join(); |
Jim King | 79c9911 | 2015-04-30 07:10:08 -0400 | [diff] [blame] | 431 | } |
| 432 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 433 | BOOST_AUTO_TEST_SUITE_END() |