summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/lib/cpp/test/processor/Handlers.h
blob: 05d19edd9673dd26758492ec610151f776a6c9d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
#ifndef _THRIFT_PROCESSOR_TEST_HANDLERS_H_
#define _THRIFT_PROCESSOR_TEST_HANDLERS_H_ 1

#include "EventLog.h"
#include "gen-cpp/ParentService.h"
#include "gen-cpp/ChildService.h"

namespace apache {
namespace thrift {
namespace test {

class ParentHandler : virtual public ParentServiceIf {
public:
  ParentHandler(const std::shared_ptr<EventLog>& log)
    : triggerMonitor(&mutex_), generation_(0), wait_(false), log_(log) {}

  int32_t incrementGeneration() override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_INCREMENT_GENERATION, 0, 0);
    return ++generation_;
  }

  int32_t getGeneration() override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_GET_GENERATION, 0, 0);
    return generation_;
  }

  void addString(const std::string& s) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_ADD_STRING, 0, 0);
    strings_.push_back(s);
  }

  void getStrings(std::vector<std::string>& _return) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_GET_STRINGS, 0, 0);
    _return = strings_;
  }

  void getDataWait(std::string& _return, const int32_t length) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_GET_DATA_WAIT, 0, 0);

    blockUntilTriggered();

    _return.append(length, 'a');
  }

  void onewayWait() override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_ONEWAY_WAIT, 0, 0);

    blockUntilTriggered();
  }

  void exceptionWait(const std::string& message) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_EXCEPTION_WAIT, 0, 0);

    blockUntilTriggered();

    MyError e;
    e.message = message;
    throw e;
  }

  void unexpectedExceptionWait(const std::string& message) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_UNEXPECTED_EXCEPTION_WAIT, 0, 0);

    blockUntilTriggered();

    MyError e;
    e.message = message;
    throw e;
  }

  /**
   * After prepareTriggeredCall() is invoked, calls to any of the *Wait()
   * functions won't return until triggerPendingCalls() is invoked
   *
   * This has to be a separate function invoked by the main test thread
   * in order to to avoid race conditions.
   */
  void prepareTriggeredCall() {
    concurrency::Guard g(mutex_);
    wait_ = true;
  }

  /**
   * Wake up all calls waiting in blockUntilTriggered()
   */
  void triggerPendingCalls() {
    concurrency::Guard g(mutex_);
    wait_ = false;
    triggerMonitor.notifyAll();
  }

protected:
  /**
   * blockUntilTriggered() won't return until triggerPendingCalls() is invoked
   * in another thread.
   *
   * This should only be called when already holding mutex_.
   */
  void blockUntilTriggered() {
    while (wait_) {
      triggerMonitor.waitForever();
    }

    // Log an event when we return
    log_->append(EventLog::ET_WAIT_RETURN, 0, 0);
  }

  concurrency::Mutex mutex_;
  concurrency::Monitor triggerMonitor;
  int32_t generation_;
  bool wait_;
  std::vector<std::string> strings_;
  std::shared_ptr<EventLog> log_;
};

#ifdef _WIN32
  #pragma warning( push )
  #pragma warning (disable : 4250 ) //inheriting methods via dominance
#endif

class ChildHandler : public ParentHandler, virtual public ChildServiceIf {
public:
  ChildHandler(const std::shared_ptr<EventLog>& log) : ParentHandler(log), value_(0) {}

  int32_t setValue(const int32_t value) override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_SET_VALUE, 0, 0);

    int32_t oldValue = value_;
    value_ = value;
    return oldValue;
  }

  int32_t getValue() override {
    concurrency::Guard g(mutex_);
    log_->append(EventLog::ET_CALL_GET_VALUE, 0, 0);

    return value_;
  }

protected:
  int32_t value_;
};

#ifdef _WIN32
  #pragma warning( pop )
#endif

struct ConnContext {
public:
  ConnContext(std::shared_ptr<protocol::TProtocol> in,
              std::shared_ptr<protocol::TProtocol> out,
              uint32_t id)
    : input(in), output(out), id(id) {}

  std::shared_ptr<protocol::TProtocol> input;
  std::shared_ptr<protocol::TProtocol> output;
  uint32_t id;
};

struct CallContext {
public:
  CallContext(ConnContext* context, uint32_t id, const std::string& name)
    : connContext(context), name(name), id(id) {}

  ConnContext* connContext;
  std::string name;
  uint32_t id;
};

class ServerEventHandler : public server::TServerEventHandler {
public:
  ServerEventHandler(const std::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {}

  void preServe() override {}

  void* createContext(std::shared_ptr<protocol::TProtocol> input,
                              std::shared_ptr<protocol::TProtocol> output) override {
    ConnContext* context = new ConnContext(input, output, nextId_);
    ++nextId_;
    log_->append(EventLog::ET_CONN_CREATED, context->id, 0);
    return context;
  }

  void deleteContext(void* serverContext,
                             std::shared_ptr<protocol::TProtocol> input,
                             std::shared_ptr<protocol::TProtocol> output) override {
    auto* context = reinterpret_cast<ConnContext*>(serverContext);

    if (input != context->input) {
      abort();
    }
    if (output != context->output) {
      abort();
    }

    log_->append(EventLog::ET_CONN_DESTROYED, context->id, 0);

    delete context;
  }

  void processContext(void* serverContext,
                              std::shared_ptr<transport::TTransport> transport) override {
// TODO: We currently don't test the behavior of the processContext()
// calls.  The various server implementations call processContext() at
// slightly different times, and it is too annoying to try and account for
// their various differences.
//
// TThreadedServer, TThreadPoolServer, and TSimpleServer usually wait until
// they see the first byte of a request before calling processContext().
// However, they don't wait for the first byte of the very first request,
// and instead immediately call processContext() before any data is
// received.
//
// TNonblockingServer always waits until receiving the full request before
// calling processContext().
#if 0
    ConnContext* context = reinterpret_cast<ConnContext*>(serverContext);
    log_->append(EventLog::ET_PROCESS, context->id, 0);
#else
    THRIFT_UNUSED_VARIABLE(serverContext);
    THRIFT_UNUSED_VARIABLE(transport);
#endif
  }

protected:
  uint32_t nextId_;
  std::shared_ptr<EventLog> log_;
};

class ProcessorEventHandler : public TProcessorEventHandler {
public:
  ProcessorEventHandler(const std::shared_ptr<EventLog>& log) : nextId_(1), log_(log) {}

  void* getContext(const char* fnName, void* serverContext) override {
    auto* connContext = reinterpret_cast<ConnContext*>(serverContext);

    CallContext* context = new CallContext(connContext, nextId_, fnName);
    ++nextId_;

    log_->append(EventLog::ET_CALL_STARTED, connContext->id, context->id, fnName);
    return context;
  }

  void freeContext(void* ctx, const char* fnName) override {
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_CALL_FINISHED, context->connContext->id, context->id, fnName);
    delete context;
  }

  void preRead(void* ctx, const char* fnName) override {
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_PRE_READ, context->connContext->id, context->id, fnName);
  }

  void postRead(void* ctx, const char* fnName, uint32_t bytes) override {
    THRIFT_UNUSED_VARIABLE(bytes);
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_POST_READ, context->connContext->id, context->id, fnName);
  }

  void preWrite(void* ctx, const char* fnName) override {
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_PRE_WRITE, context->connContext->id, context->id, fnName);
  }

  void postWrite(void* ctx, const char* fnName, uint32_t bytes) override {
    THRIFT_UNUSED_VARIABLE(bytes);
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_POST_WRITE, context->connContext->id, context->id, fnName);
  }

  void asyncComplete(void* ctx, const char* fnName) override {
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_ASYNC_COMPLETE, context->connContext->id, context->id, fnName);
  }

  void handlerError(void* ctx, const char* fnName) override {
    auto* context = reinterpret_cast<CallContext*>(ctx);
    checkName(context, fnName);
    log_->append(EventLog::ET_HANDLER_ERROR, context->connContext->id, context->id, fnName);
  }

protected:
  void checkName(const CallContext* context, const char* fnName) {
    // Note: we can't use BOOST_CHECK_EQUAL here, since the handler runs in a
    // different thread from the test functions.  Just abort if the names are
    // different
    if (context->name != fnName) {
      fprintf(stderr,
              "call context name mismatch: \"%s\" != \"%s\"\n",
              context->name.c_str(),
              fnName);
      fflush(stderr);
      abort();
    }
  }

  uint32_t nextId_;
  std::shared_ptr<EventLog> log_;
};
}
}
} // apache::thrift::test

#endif // _THRIFT_PROCESSOR_TEST_HANDLERS_H_