Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [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 | #ifndef _THRIFT_PROCESSOR_TEST_HANDLERS_H_ |
| 20 | #define _THRIFT_PROCESSOR_TEST_HANDLERS_H_ 1 |
| 21 | |
Roger Meier | 2b1a528 | 2012-05-11 10:12:39 +0000 | [diff] [blame] | 22 | #include "EventLog.h" |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 23 | #include "gen-cpp/ParentService.h" |
| 24 | #include "gen-cpp/ChildService.h" |
| 25 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 26 | namespace apache { |
| 27 | namespace thrift { |
| 28 | namespace test { |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 29 | |
| 30 | class ParentHandler : virtual public ParentServiceIf { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | public: |
| 32 | ParentHandler(const boost::shared_ptr<EventLog>& log) |
| 33 | : triggerMonitor(&mutex_), generation_(0), wait_(false), log_(log) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 34 | |
| 35 | int32_t incrementGeneration() { |
| 36 | concurrency::Guard g(mutex_); |
| 37 | log_->append(EventLog::ET_CALL_INCREMENT_GENERATION, 0, 0); |
| 38 | return ++generation_; |
| 39 | } |
| 40 | |
| 41 | int32_t getGeneration() { |
| 42 | concurrency::Guard g(mutex_); |
| 43 | log_->append(EventLog::ET_CALL_GET_GENERATION, 0, 0); |
| 44 | return generation_; |
| 45 | } |
| 46 | |
| 47 | void addString(const std::string& s) { |
| 48 | concurrency::Guard g(mutex_); |
| 49 | log_->append(EventLog::ET_CALL_ADD_STRING, 0, 0); |
| 50 | strings_.push_back(s); |
| 51 | } |
| 52 | |
| 53 | void getStrings(std::vector<std::string>& _return) { |
| 54 | concurrency::Guard g(mutex_); |
| 55 | log_->append(EventLog::ET_CALL_GET_STRINGS, 0, 0); |
| 56 | _return = strings_; |
| 57 | } |
| 58 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame^] | 59 | void getDataWait(std::string& _return, const int32_t length) { |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 60 | concurrency::Guard g(mutex_); |
| 61 | log_->append(EventLog::ET_CALL_GET_DATA_WAIT, 0, 0); |
| 62 | |
| 63 | blockUntilTriggered(); |
| 64 | |
| 65 | _return.append(length, 'a'); |
| 66 | } |
| 67 | |
| 68 | void onewayWait() { |
| 69 | concurrency::Guard g(mutex_); |
| 70 | log_->append(EventLog::ET_CALL_ONEWAY_WAIT, 0, 0); |
| 71 | |
| 72 | blockUntilTriggered(); |
| 73 | } |
| 74 | |
| 75 | void exceptionWait(const std::string& message) { |
| 76 | concurrency::Guard g(mutex_); |
| 77 | log_->append(EventLog::ET_CALL_EXCEPTION_WAIT, 0, 0); |
| 78 | |
| 79 | blockUntilTriggered(); |
| 80 | |
| 81 | MyError e; |
| 82 | e.message = message; |
| 83 | throw e; |
| 84 | } |
| 85 | |
| 86 | void unexpectedExceptionWait(const std::string& message) { |
| 87 | concurrency::Guard g(mutex_); |
| 88 | log_->append(EventLog::ET_CALL_UNEXPECTED_EXCEPTION_WAIT, 0, 0); |
| 89 | |
| 90 | blockUntilTriggered(); |
| 91 | |
| 92 | MyError e; |
| 93 | e.message = message; |
| 94 | throw e; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * After prepareTriggeredCall() is invoked, calls to any of the *Wait() |
| 99 | * functions won't return until triggerPendingCalls() is invoked |
| 100 | * |
| 101 | * This has to be a separate function invoked by the main test thread |
| 102 | * in order to to avoid race conditions. |
| 103 | */ |
| 104 | void prepareTriggeredCall() { |
| 105 | concurrency::Guard g(mutex_); |
| 106 | wait_ = true; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Wake up all calls waiting in blockUntilTriggered() |
| 111 | */ |
| 112 | void triggerPendingCalls() { |
| 113 | concurrency::Guard g(mutex_); |
| 114 | wait_ = false; |
| 115 | triggerMonitor.notifyAll(); |
| 116 | } |
| 117 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 118 | protected: |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 119 | /** |
| 120 | * blockUntilTriggered() won't return until triggerPendingCalls() is invoked |
| 121 | * in another thread. |
| 122 | * |
| 123 | * This should only be called when already holding mutex_. |
| 124 | */ |
| 125 | void blockUntilTriggered() { |
| 126 | while (wait_) { |
| 127 | triggerMonitor.waitForever(); |
| 128 | } |
| 129 | |
| 130 | // Log an event when we return |
| 131 | log_->append(EventLog::ET_WAIT_RETURN, 0, 0); |
| 132 | } |
| 133 | |
| 134 | concurrency::Mutex mutex_; |
| 135 | concurrency::Monitor triggerMonitor; |
| 136 | int32_t generation_; |
| 137 | bool wait_; |
| 138 | std::vector<std::string> strings_; |
| 139 | boost::shared_ptr<EventLog> log_; |
| 140 | }; |
| 141 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame^] | 142 | #ifdef _WIN32 |
| 143 | #pragma warning( push ) |
| 144 | #pragma warning (disable : 4250 ) //inheriting methods via dominance |
| 145 | #endif; |
| 146 | |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 147 | class ChildHandler : public ParentHandler, virtual public ChildServiceIf { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 148 | public: |
| 149 | ChildHandler(const boost::shared_ptr<EventLog>& log) : ParentHandler(log), value_(0) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 150 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame^] | 151 | int32_t setValue(const int32_t value) { |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 152 | concurrency::Guard g(mutex_); |
| 153 | log_->append(EventLog::ET_CALL_SET_VALUE, 0, 0); |
| 154 | |
| 155 | int32_t oldValue = value_; |
| 156 | value_ = value; |
| 157 | return oldValue; |
| 158 | } |
| 159 | |
| 160 | int32_t getValue() { |
| 161 | concurrency::Guard g(mutex_); |
| 162 | log_->append(EventLog::ET_CALL_GET_VALUE, 0, 0); |
| 163 | |
| 164 | return value_; |
| 165 | } |
| 166 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 167 | protected: |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 168 | int32_t value_; |
| 169 | }; |
| 170 | |
ben-craig | fae08e7 | 2015-07-15 11:34:47 -0500 | [diff] [blame^] | 171 | #ifdef _WIN32 |
| 172 | #pragma warning( pop ) |
| 173 | #endif |
| 174 | |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 175 | struct ConnContext { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 176 | public: |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 177 | ConnContext(boost::shared_ptr<protocol::TProtocol> in, |
| 178 | boost::shared_ptr<protocol::TProtocol> out, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 179 | uint32_t id) |
| 180 | : input(in), output(out), id(id) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 181 | |
| 182 | boost::shared_ptr<protocol::TProtocol> input; |
| 183 | boost::shared_ptr<protocol::TProtocol> output; |
| 184 | uint32_t id; |
| 185 | }; |
| 186 | |
| 187 | struct CallContext { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 188 | public: |
| 189 | CallContext(ConnContext* context, uint32_t id, const std::string& name) |
| 190 | : connContext(context), name(name), id(id) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 191 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 192 | ConnContext* connContext; |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 193 | std::string name; |
| 194 | uint32_t id; |
| 195 | }; |
| 196 | |
| 197 | class ServerEventHandler : public server::TServerEventHandler { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 198 | public: |
| 199 | ServerEventHandler(const boost::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 200 | |
| 201 | virtual void preServe() {} |
| 202 | |
| 203 | virtual void* createContext(boost::shared_ptr<protocol::TProtocol> input, |
| 204 | boost::shared_ptr<protocol::TProtocol> output) { |
| 205 | ConnContext* context = new ConnContext(input, output, nextId_); |
| 206 | ++nextId_; |
| 207 | log_->append(EventLog::ET_CONN_CREATED, context->id, 0); |
| 208 | return context; |
| 209 | } |
| 210 | |
| 211 | virtual void deleteContext(void* serverContext, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 212 | boost::shared_ptr<protocol::TProtocol> input, |
| 213 | boost::shared_ptr<protocol::TProtocol> output) { |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 214 | ConnContext* context = reinterpret_cast<ConnContext*>(serverContext); |
| 215 | |
| 216 | if (input != context->input) { |
| 217 | abort(); |
| 218 | } |
| 219 | if (output != context->output) { |
| 220 | abort(); |
| 221 | } |
| 222 | |
| 223 | log_->append(EventLog::ET_CONN_DESTROYED, context->id, 0); |
| 224 | |
| 225 | delete context; |
| 226 | } |
| 227 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 228 | virtual void processContext(void* serverContext, |
| 229 | boost::shared_ptr<transport::TTransport> transport) { |
| 230 | // TODO: We currently don't test the behavior of the processContext() |
| 231 | // calls. The various server implementations call processContext() at |
| 232 | // slightly different times, and it is too annoying to try and account for |
| 233 | // their various differences. |
| 234 | // |
| 235 | // TThreadedServer, TThreadPoolServer, and TSimpleServer usually wait until |
| 236 | // they see the first byte of a request before calling processContext(). |
| 237 | // However, they don't wait for the first byte of the very first request, |
| 238 | // and instead immediately call processContext() before any data is |
| 239 | // received. |
| 240 | // |
| 241 | // TNonblockingServer always waits until receiving the full request before |
| 242 | // calling processContext(). |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 243 | #if 0 |
| 244 | ConnContext* context = reinterpret_cast<ConnContext*>(serverContext); |
| 245 | log_->append(EventLog::ET_PROCESS, context->id, 0); |
Konrad Grochowski | b3f5ffc | 2014-11-06 19:32:59 +0100 | [diff] [blame] | 246 | #else |
| 247 | THRIFT_UNUSED_VARIABLE(serverContext); |
| 248 | THRIFT_UNUSED_VARIABLE(transport); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 249 | #endif |
| 250 | } |
| 251 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 252 | protected: |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 253 | uint32_t nextId_; |
| 254 | boost::shared_ptr<EventLog> log_; |
| 255 | }; |
| 256 | |
| 257 | class ProcessorEventHandler : public TProcessorEventHandler { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 258 | public: |
| 259 | ProcessorEventHandler(const boost::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {} |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 260 | |
| 261 | void* getContext(const char* fnName, void* serverContext) { |
| 262 | ConnContext* connContext = reinterpret_cast<ConnContext*>(serverContext); |
| 263 | |
| 264 | CallContext* context = new CallContext(connContext, nextId_, fnName); |
| 265 | ++nextId_; |
| 266 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 267 | log_->append(EventLog::ET_CALL_STARTED, connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 268 | return context; |
| 269 | } |
| 270 | |
| 271 | void freeContext(void* ctx, const char* fnName) { |
| 272 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 273 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 274 | log_->append(EventLog::ET_CALL_FINISHED, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 275 | delete context; |
| 276 | } |
| 277 | |
| 278 | void preRead(void* ctx, const char* fnName) { |
| 279 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 280 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 281 | log_->append(EventLog::ET_PRE_READ, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void postRead(void* ctx, const char* fnName, uint32_t bytes) { |
Konrad Grochowski | b3f5ffc | 2014-11-06 19:32:59 +0100 | [diff] [blame] | 285 | THRIFT_UNUSED_VARIABLE(bytes); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 286 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 287 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 288 | log_->append(EventLog::ET_POST_READ, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | void preWrite(void* ctx, const char* fnName) { |
| 292 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 293 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 294 | log_->append(EventLog::ET_PRE_WRITE, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void postWrite(void* ctx, const char* fnName, uint32_t bytes) { |
Konrad Grochowski | b3f5ffc | 2014-11-06 19:32:59 +0100 | [diff] [blame] | 298 | THRIFT_UNUSED_VARIABLE(bytes); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 299 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 300 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 301 | log_->append(EventLog::ET_POST_WRITE, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void asyncComplete(void* ctx, const char* fnName) { |
| 305 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 306 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 307 | log_->append(EventLog::ET_ASYNC_COMPLETE, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void handlerError(void* ctx, const char* fnName) { |
| 311 | CallContext* context = reinterpret_cast<CallContext*>(ctx); |
| 312 | checkName(context, fnName); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 313 | log_->append(EventLog::ET_HANDLER_ERROR, context->connContext->id, context->id, fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 316 | protected: |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 317 | void checkName(const CallContext* context, const char* fnName) { |
| 318 | // Note: we can't use BOOST_CHECK_EQUAL here, since the handler runs in a |
| 319 | // different thread from the test functions. Just abort if the names are |
| 320 | // different |
| 321 | if (context->name != fnName) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 322 | fprintf(stderr, |
| 323 | "call context name mismatch: \"%s\" != \"%s\"\n", |
| 324 | context->name.c_str(), |
| 325 | fnName); |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 326 | fflush(stderr); |
| 327 | abort(); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | uint32_t nextId_; |
| 332 | boost::shared_ptr<EventLog> log_; |
| 333 | }; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } // apache::thrift::test |
Roger Meier | 2be7f24 | 2012-05-10 09:01:45 +0000 | [diff] [blame] | 337 | |
| 338 | #endif // _THRIFT_PROCESSOR_TEST_HANDLERS_H_ |