Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [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 | |
zeshuai007 | 6b1cb30 | 2020-05-27 12:08:01 +0800 | [diff] [blame] | 20 | #include <boost/test/unit_test.hpp> |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 21 | #include <boost/thread.hpp> |
| 22 | #include <iostream> |
| 23 | #include <climits> |
| 24 | #include <vector> |
| 25 | #include <thrift/concurrency/Monitor.h> |
| 26 | #include <thrift/protocol/TBinaryProtocol.h> |
| 27 | #include <thrift/protocol/TJSONProtocol.h> |
| 28 | #include <thrift/server/TThreadedServer.h> |
| 29 | #include <thrift/transport/THttpServer.h> |
| 30 | #include <thrift/transport/THttpClient.h> |
| 31 | #include <thrift/transport/TServerSocket.h> |
| 32 | #include <thrift/transport/TSocket.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 33 | #include <memory> |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 34 | #include <thrift/transport/TBufferTransports.h> |
| 35 | #include "gen-cpp/OneWayService.h" |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(OneWayHTTPTest) |
| 38 | |
| 39 | using namespace apache::thrift; |
| 40 | using apache::thrift::protocol::TProtocol; |
| 41 | using apache::thrift::protocol::TBinaryProtocol; |
| 42 | using apache::thrift::protocol::TBinaryProtocolFactory; |
| 43 | using apache::thrift::protocol::TJSONProtocol; |
| 44 | using apache::thrift::protocol::TJSONProtocolFactory; |
| 45 | using apache::thrift::server::TThreadedServer; |
| 46 | using apache::thrift::server::TServerEventHandler; |
| 47 | using apache::thrift::transport::TTransport; |
| 48 | using apache::thrift::transport::THttpServer; |
| 49 | using apache::thrift::transport::THttpServerTransportFactory; |
| 50 | using apache::thrift::transport::THttpClient; |
| 51 | using apache::thrift::transport::TBufferedTransport; |
| 52 | using apache::thrift::transport::TBufferedTransportFactory; |
| 53 | using apache::thrift::transport::TMemoryBuffer; |
| 54 | using apache::thrift::transport::TServerSocket; |
| 55 | using apache::thrift::transport::TSocket; |
| 56 | using apache::thrift::transport::TTransportException; |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 57 | using std::shared_ptr; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 58 | using std::string; |
| 59 | namespace utf = boost::unit_test; |
| 60 | |
| 61 | // Define this env var to enable some logging (in case you need to debug) |
| 62 | #undef ENABLE_STDERR_LOGGING |
| 63 | |
| 64 | class OneWayServiceHandler : public onewaytest::OneWayServiceIf { |
| 65 | public: |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 66 | OneWayServiceHandler() = default; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 67 | |
| 68 | void roundTripRPC() override { |
| 69 | #ifdef ENABLE_STDERR_LOGGING |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 70 | cerr << "roundTripRPC()" << '\n'; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 71 | #endif |
| 72 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 73 | void oneWayRPC() override { |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 74 | #ifdef ENABLE_STDERR_LOGGING |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 75 | cerr << "oneWayRPC()" << '\n'; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 76 | #endif |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | class OneWayServiceCloneFactory : virtual public onewaytest::OneWayServiceIfFactory { |
| 81 | public: |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 82 | ~OneWayServiceCloneFactory() override = default; |
| 83 | onewaytest::OneWayServiceIf* getHandler(const ::apache::thrift::TConnectionInfo& connInfo) override |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 84 | { |
| 85 | (void)connInfo ; |
| 86 | return new OneWayServiceHandler; |
| 87 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 88 | void releaseHandler( onewaytest::OneWayServiceIf* handler) override { |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 89 | delete handler; |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | class RPC0ThreadClass { |
| 94 | public: |
| 95 | RPC0ThreadClass(TThreadedServer& server) : server_(server) { } // Constructor |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 96 | ~RPC0ThreadClass() = default; // Destructor |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 97 | |
| 98 | void Run() { |
| 99 | server_.serve() ; |
| 100 | } |
| 101 | TThreadedServer& server_ ; |
| 102 | } ; |
| 103 | |
| 104 | using apache::thrift::concurrency::Monitor; |
| 105 | using apache::thrift::concurrency::Mutex; |
| 106 | using apache::thrift::concurrency::Synchronized; |
| 107 | |
| 108 | // copied from IntegrationTest |
| 109 | class TServerReadyEventHandler : public TServerEventHandler, public Monitor { |
| 110 | public: |
| 111 | TServerReadyEventHandler() : isListening_(false), accepted_(0) {} |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 112 | ~TServerReadyEventHandler() override = default; |
| 113 | void preServe() override { |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 114 | Synchronized sync(*this); |
| 115 | isListening_ = true; |
| 116 | notify(); |
| 117 | } |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 118 | void* createContext(shared_ptr<TProtocol> input, |
| 119 | shared_ptr<TProtocol> output) override { |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 120 | Synchronized sync(*this); |
| 121 | ++accepted_; |
| 122 | notify(); |
| 123 | |
| 124 | (void)input; |
| 125 | (void)output; |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 126 | return nullptr; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 127 | } |
| 128 | bool isListening() const { return isListening_; } |
| 129 | uint64_t acceptedCount() const { return accepted_; } |
| 130 | |
| 131 | private: |
| 132 | bool isListening_; |
| 133 | uint64_t accepted_; |
| 134 | }; |
| 135 | |
| 136 | class TBlockableBufferedTransport : public TBufferedTransport { |
| 137 | public: |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 138 | TBlockableBufferedTransport(std::shared_ptr<TTransport> transport) |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 139 | : TBufferedTransport(transport, 10240), |
| 140 | blocked_(false) { |
| 141 | } |
| 142 | |
| 143 | uint32_t write_buffer_length() { |
Sebastian Zenker | 042580f | 2019-01-29 15:48:12 +0100 | [diff] [blame] | 144 | auto have_bytes = static_cast<uint32_t>(wBase_ - wBuf_.get()); |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 145 | return have_bytes ; |
| 146 | } |
| 147 | |
| 148 | void block() { |
| 149 | blocked_ = true ; |
| 150 | #ifdef ENABLE_STDERR_LOGGING |
| 151 | cerr << "block flushing\n" ; |
| 152 | #endif |
| 153 | } |
| 154 | void unblock() { |
| 155 | blocked_ = false ; |
| 156 | #ifdef ENABLE_STDERR_LOGGING |
| 157 | cerr << "unblock flushing, buffer is\n<<" << std::string((char *)wBuf_.get(), write_buffer_length()) << ">>\n" ; |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void flush() override { |
| 162 | if (blocked_) { |
| 163 | #ifdef ENABLE_STDERR_LOGGING |
| 164 | cerr << "flush was blocked\n" ; |
| 165 | #endif |
| 166 | return ; |
| 167 | } |
| 168 | TBufferedTransport::flush() ; |
| 169 | } |
| 170 | |
| 171 | bool blocked_ ; |
| 172 | } ; |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE( JSON_BufferedHTTP ) |
| 175 | { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 176 | std::shared_ptr<TServerSocket> ss = std::make_shared<TServerSocket>(0) ; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 177 | TThreadedServer server( |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 178 | std::make_shared<onewaytest::OneWayServiceProcessorFactory>(std::make_shared<OneWayServiceCloneFactory>()), |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 179 | ss, //port |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 180 | std::make_shared<THttpServerTransportFactory>(), |
| 181 | std::make_shared<TJSONProtocolFactory>()); |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 182 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 183 | std::shared_ptr<TServerReadyEventHandler> pEventHandler(new TServerReadyEventHandler) ; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 184 | server.setServerEventHandler(pEventHandler); |
| 185 | |
| 186 | #ifdef ENABLE_STDERR_LOGGING |
| 187 | cerr << "Starting the server...\n"; |
| 188 | #endif |
| 189 | RPC0ThreadClass t(server) ; |
| 190 | boost::thread thread(&RPC0ThreadClass::Run, &t); |
| 191 | |
| 192 | { |
| 193 | Synchronized sync(*(pEventHandler.get())); |
| 194 | while (!pEventHandler->isListening()) { |
| 195 | pEventHandler->wait(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | int port = ss->getPort() ; |
| 200 | #ifdef ENABLE_STDERR_LOGGING |
CJCombrink | 4a280d5 | 2024-03-14 19:57:41 +0100 | [diff] [blame] | 201 | cerr << "port " << port << '\n'; |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | { |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 205 | std::shared_ptr<TSocket> socket(new TSocket("localhost", port)); |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 206 | socket->setRecvTimeout(10000) ; // 1000msec should be enough |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 207 | std::shared_ptr<TBlockableBufferedTransport> blockable_transport(new TBlockableBufferedTransport(socket)); |
| 208 | std::shared_ptr<TTransport> transport(new THttpClient(blockable_transport, "localhost", "/service")); |
| 209 | std::shared_ptr<TProtocol> protocol(new TJSONProtocol(transport)); |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 210 | onewaytest::OneWayServiceClient client(protocol); |
| 211 | |
| 212 | |
| 213 | transport->open(); |
| 214 | client.roundTripRPC(); |
| 215 | blockable_transport->block() ; |
| 216 | uint32_t size0 = blockable_transport->write_buffer_length() ; |
| 217 | client.send_oneWayRPC() ; |
| 218 | uint32_t size1 = blockable_transport->write_buffer_length() ; |
| 219 | client.send_oneWayRPC() ; |
| 220 | uint32_t size2 = blockable_transport->write_buffer_length() ; |
| 221 | BOOST_CHECK((size1 - size0) == (size2 - size1)) ; |
| 222 | blockable_transport->unblock() ; |
| 223 | client.send_roundTripRPC() ; |
| 224 | blockable_transport->flush() ; |
| 225 | try { |
| 226 | client.recv_roundTripRPC() ; |
cyy | 9fed901 | 2019-01-16 14:43:51 +0800 | [diff] [blame] | 227 | } catch (const TTransportException &e) { |
Chet Murthy | ad08a8b | 2017-12-19 15:55:56 -0800 | [diff] [blame] | 228 | BOOST_ERROR( "we should not get a transport exception -- this means we failed: " + std::string(e.what()) ) ; |
| 229 | } |
| 230 | transport->close(); |
| 231 | } |
| 232 | server.stop(); |
| 233 | thread.join() ; |
| 234 | #ifdef ENABLE_STDERR_LOGGING |
| 235 | cerr << "finished.\n"; |
| 236 | #endif |
| 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() |