Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [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 TNonblockingServerTest |
| 21 | #include <boost/test/unit_test.hpp> |
| 22 | #include <boost/smart_ptr.hpp> |
| 23 | |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 24 | #include "thrift/concurrency/Monitor.h" |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 25 | #include "thrift/concurrency/Thread.h" |
| 26 | #include "thrift/server/TNonblockingServer.h" |
| 27 | |
| 28 | #include "gen-cpp/ParentService.h" |
| 29 | |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 30 | #include <event.h> |
| 31 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 32 | using namespace apache::thrift; |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 33 | using apache::thrift::concurrency::Guard; |
| 34 | using apache::thrift::concurrency::Monitor; |
| 35 | using apache::thrift::concurrency::Mutex; |
| 36 | using apache::thrift::server::TServerEventHandler; |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 37 | |
| 38 | struct Handler : public test::ParentServiceIf { |
| 39 | void addString(const std::string& s) { strings_.push_back(s); } |
| 40 | void getStrings(std::vector<std::string>& _return) { _return = strings_; } |
| 41 | std::vector<std::string> strings_; |
| 42 | |
| 43 | // dummy overrides not used in this test |
| 44 | int32_t incrementGeneration() { return 0; } |
| 45 | int32_t getGeneration() { return 0; } |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame] | 46 | void getDataWait(std::string&, const int32_t) {} |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 47 | void onewayWait() {} |
| 48 | void exceptionWait(const std::string&) {} |
| 49 | void unexpectedExceptionWait(const std::string&) {} |
| 50 | }; |
| 51 | |
| 52 | class Fixture { |
| 53 | private: |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 54 | struct ListenEventHandler : public TServerEventHandler { |
| 55 | public: |
| 56 | ListenEventHandler(Mutex* mutex) : listenMonitor_(mutex), ready_(false) {} |
| 57 | |
| 58 | void preServe() /* override */ { |
| 59 | Guard g(listenMonitor_.mutex()); |
| 60 | ready_ = true; |
| 61 | listenMonitor_.notify(); |
| 62 | } |
| 63 | |
| 64 | Monitor listenMonitor_; |
| 65 | bool ready_; |
| 66 | }; |
| 67 | |
Ben Craig | 7207c22 | 2015-07-06 08:40:35 -0500 | [diff] [blame] | 68 | struct Runner : public apache::thrift::concurrency::Runnable { |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 69 | int port; |
| 70 | boost::shared_ptr<event_base> userEventBase; |
| 71 | boost::shared_ptr<TProcessor> processor; |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 72 | boost::shared_ptr<server::TNonblockingServer> server; |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 73 | boost::shared_ptr<ListenEventHandler> listenHandler; |
| 74 | Mutex mutex_; |
| 75 | |
| 76 | Runner() { |
| 77 | listenHandler.reset(new ListenEventHandler(&mutex_)); |
| 78 | } |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 79 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 80 | virtual void run() { |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 81 | // When binding to explicit port, allow retrying to workaround bind failures on ports in use |
| 82 | int retryCount = port ? 10 : 0; |
| 83 | startServer(retryCount); |
| 84 | } |
| 85 | |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 86 | void readyBarrier() { |
| 87 | // block until server is listening and ready to accept connections |
| 88 | Guard g(mutex_); |
| 89 | while (!listenHandler->ready_) { |
| 90 | listenHandler->listenMonitor_.wait(); |
| 91 | } |
| 92 | } |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 93 | private: |
| 94 | void startServer(int retry_count) { |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 95 | try { |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 96 | server.reset(new server::TNonblockingServer(processor, port)); |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 97 | server->setServerEventHandler(listenHandler); |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 98 | if (userEventBase) { |
| 99 | server->registerEvents(userEventBase.get()); |
| 100 | } |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 101 | server->serve(); |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 102 | } catch (const transport::TTransportException&) { |
| 103 | if (retry_count > 0) { |
| 104 | ++port; |
| 105 | startServer(retry_count - 1); |
| 106 | } else { |
| 107 | throw; |
| 108 | } |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | }; |
| 112 | |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 113 | struct EventDeleter { |
| 114 | void operator()(event_base* p) { event_base_free(p); } |
| 115 | }; |
| 116 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 117 | protected: |
| 118 | Fixture() : processor(new test::ParentServiceProcessor(boost::make_shared<Handler>())) {} |
| 119 | |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 120 | ~Fixture() { |
| 121 | if (server) { |
| 122 | server->stop(); |
| 123 | } |
| 124 | if (thread) { |
| 125 | thread->join(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | void setEventBase(event_base* user_event_base) { |
| 130 | userEventBase_.reset(user_event_base, EventDeleter()); |
| 131 | } |
| 132 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 133 | int startServer(int port) { |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 134 | boost::shared_ptr<Runner> runner(new Runner); |
| 135 | runner->port = port; |
| 136 | runner->processor = processor; |
| 137 | runner->userEventBase = userEventBase_; |
| 138 | |
Ben Craig | 7207c22 | 2015-07-06 08:40:35 -0500 | [diff] [blame] | 139 | boost::scoped_ptr<apache::thrift::concurrency::ThreadFactory> threadFactory( |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 140 | new apache::thrift::concurrency::PlatformThreadFactory( |
Nobuaki Sukegawa | 2825664 | 2014-12-16 03:24:37 +0900 | [diff] [blame] | 141 | #if !USE_BOOST_THREAD && !USE_STD_THREAD |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 142 | concurrency::PlatformThreadFactory::OTHER, concurrency::PlatformThreadFactory::NORMAL, |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 143 | 1, |
| 144 | #endif |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 145 | false)); |
| 146 | thread = threadFactory->newThread(runner); |
| 147 | thread->start(); |
Buğra Gedik | 36d1b0d | 2016-09-04 17:18:15 +0900 | [diff] [blame^] | 148 | runner->readyBarrier(); |
| 149 | |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 150 | server = runner->server; |
| 151 | return runner->port; |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool canCommunicate(int serverPort) { |
| 155 | boost::shared_ptr<transport::TSocket> socket(new transport::TSocket("localhost", serverPort)); |
| 156 | socket->open(); |
| 157 | test::ParentServiceClient client(boost::make_shared<protocol::TBinaryProtocol>( |
| 158 | boost::make_shared<transport::TFramedTransport>(socket))); |
| 159 | client.addString("foo"); |
| 160 | std::vector<std::string> strings; |
| 161 | client.getStrings(strings); |
| 162 | return strings.size() == 1 && !(strings[0].compare("foo")); |
| 163 | } |
| 164 | |
| 165 | private: |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 166 | boost::shared_ptr<event_base> userEventBase_; |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 167 | boost::shared_ptr<test::ParentServiceProcessor> processor; |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 168 | protected: |
| 169 | boost::shared_ptr<server::TNonblockingServer> server; |
Nobuaki Sukegawa | 8cc9175 | 2016-05-15 00:24:41 +0900 | [diff] [blame] | 170 | private: |
| 171 | boost::shared_ptr<apache::thrift::concurrency::Thread> thread; |
| 172 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | BOOST_AUTO_TEST_SUITE(TNonblockingServerTest) |
| 176 | |
| 177 | BOOST_FIXTURE_TEST_CASE(get_specified_port, Fixture) { |
| 178 | int specified_port = startServer(12345); |
| 179 | BOOST_REQUIRE_GE(specified_port, 12345); |
| 180 | BOOST_REQUIRE_EQUAL(server->getListenPort(), specified_port); |
| 181 | BOOST_CHECK(canCommunicate(specified_port)); |
| 182 | |
| 183 | server->stop(); |
| 184 | BOOST_CHECK_EQUAL(server->getListenPort(), specified_port); |
| 185 | } |
| 186 | |
| 187 | BOOST_FIXTURE_TEST_CASE(get_assigned_port, Fixture) { |
| 188 | int specified_port = startServer(0); |
| 189 | BOOST_REQUIRE_EQUAL(specified_port, 0); |
| 190 | int assigned_port = server->getListenPort(); |
| 191 | BOOST_REQUIRE_NE(assigned_port, 0); |
| 192 | BOOST_CHECK(canCommunicate(assigned_port)); |
| 193 | |
| 194 | server->stop(); |
| 195 | BOOST_CHECK_EQUAL(server->getListenPort(), 0); |
| 196 | } |
| 197 | |
Nobuaki Sukegawa | 8016af8 | 2015-01-02 23:14:22 +0900 | [diff] [blame] | 198 | BOOST_FIXTURE_TEST_CASE(provide_event_base, Fixture) { |
| 199 | event_base* eb = event_base_new(); |
| 200 | setEventBase(eb); |
| 201 | startServer(0); |
| 202 | |
| 203 | // assert that the server works |
| 204 | BOOST_CHECK(canCommunicate(server->getListenPort())); |
| 205 | #if LIBEVENT_VERSION_NUMBER > 0x02010400 |
| 206 | // also assert that the event_base is actually used when it's easy |
| 207 | BOOST_CHECK_GT(event_base_get_num_events(eb, EVENT_BASE_COUNT_ADDED), 0); |
| 208 | #endif |
| 209 | } |
| 210 | |
Nobuaki Sukegawa | d0d7a65 | 2014-12-07 21:36:51 +0900 | [diff] [blame] | 211 | BOOST_AUTO_TEST_SUITE_END() |