summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/lib/d/test/thrift_test_server.d
blob: ce820d69905f1bffb2d112901c722902cf59fac9 (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
/*
 * 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.
 */

module thrift_test_server;

import core.stdc.errno : errno;
import core.stdc.signal : signal, SIGINT, SIG_DFL, SIG_ERR;
import core.thread : dur, Thread;
import std.algorithm;
import std.exception : enforce;
import std.getopt;
import std.parallelism : totalCPUs;
import std.string;
import std.stdio;
import std.typetuple : TypeTuple, staticMap;
import thrift.base;
import thrift.codegen.processor;
import thrift.protocol.base;
import thrift.protocol.binary;
import thrift.protocol.compact;
import thrift.protocol.json;
import thrift.server.base;
import thrift.server.transport.socket;
import thrift.server.transport.ssl;
import thrift.transport.base;
import thrift.transport.buffered;
import thrift.transport.framed;
import thrift.transport.http;
import thrift.transport.ssl;
import thrift.util.cancellation;
import thrift.util.hashset;
import test_utils;

import thrift_test_common;
import thrift.test.ThriftTest_types;
import thrift.test.ThriftTest;

class TestHandler : ThriftTest {
  this(bool trace) {
    trace_ = trace;
  }

  override void testVoid() {
    if (trace_) writeln("testVoid()");
  }

  override string testString(string thing) {
    if (trace_) writefln("testString(\"%s\")", thing);
    return thing;
  }

  override byte testByte(byte thing) {
    if (trace_) writefln("testByte(%s)", thing);
    return thing;
  }

  override int testI32(int thing) {
    if (trace_) writefln("testI32(%s)", thing);
    return thing;
  }

  override long testI64(long thing) {
    if (trace_) writefln("testI64(%s)", thing);
    return thing;
  }

  override double testDouble(double thing) {
    if (trace_) writefln("testDouble(%s)", thing);
    return thing;
  }

  override string testBinary(string thing) {
    if (trace_) writefln("testBinary(\"%s\")", thing);
    return thing;
  }

  override bool testBool(bool thing) {
    if (trace_) writefln("testBool(\"%s\")", thing);
    return thing;
  }

  override Xtruct testStruct(ref const(Xtruct) thing) {
    if (trace_) writefln("testStruct({\"%s\", %s, %s, %s})",
      thing.string_thing, thing.byte_thing, thing.i32_thing, thing.i64_thing);
    return thing;
  }

  override Xtruct2 testNest(ref const(Xtruct2) nest) {
    auto thing = nest.struct_thing;
    if (trace_) writefln("testNest({%s, {\"%s\", %s, %s, %s}, %s})",
      nest.byte_thing, thing.string_thing, thing.byte_thing, thing.i32_thing,
      thing.i64_thing, nest.i32_thing);
    return nest;
  }

  override int[int] testMap(int[int] thing) {
    if (trace_) writefln("testMap({%s})", thing);
    return thing;
  }

  override HashSet!int testSet(HashSet!int thing) {
    if (trace_) writefln("testSet({%s})",
      join(map!`to!string(a)`(thing[]), ", "));
    return thing;
  }

  override int[] testList(int[] thing) {
    if (trace_) writefln("testList(%s)", thing);
    return thing;
  }

  override Numberz testEnum(Numberz thing) {
    if (trace_) writefln("testEnum(%s)", thing);
    return thing;
  }

  override UserId testTypedef(UserId thing) {
    if (trace_) writefln("testTypedef(%s)", thing);
    return thing;
  }

  override string[string] testStringMap(string[string] thing) {
    if (trace_) writefln("testStringMap(%s)", thing);
    return thing;
  }

  override int[int][int] testMapMap(int hello) {
    if (trace_) writefln("testMapMap(%s)", hello);
    return testMapMapReturn;
  }

  override Insanity[Numberz][UserId] testInsanity(ref const(Insanity) argument) {
    if (trace_) writeln("testInsanity()");
    Insanity[Numberz][UserId] ret;
    Insanity[Numberz] m1;
    Insanity[Numberz] m2;
    Insanity tmp;
    tmp = cast(Insanity)argument;
    m1[Numberz.TWO] = tmp;
    m1[Numberz.THREE] = tmp;
    m2[Numberz.SIX] = Insanity();
    ret[1] = m1;
    ret[2] = m2;
    return ret;
  }

  override Xtruct testMulti(byte arg0, int arg1, long arg2, string[short] arg3,
    Numberz arg4, UserId arg5)
  {
    if (trace_) writeln("testMulti()");
    return Xtruct("Hello2", arg0, arg1, arg2);
  }

  override void testException(string arg) {
    if (trace_) writefln("testException(%s)", arg);
    if (arg == "Xception") {
      auto e = new Xception();
      e.errorCode = 1001;
      e.message = arg;
      throw e;
    } else if (arg == "TException") {
      throw new TException();
    } else if (arg == "ApplicationException") {
      throw new TException();
    }
  }

  override Xtruct testMultiException(string arg0, string arg1) {
    if (trace_) writefln("testMultiException(%s, %s)", arg0, arg1);

    if (arg0 == "Xception") {
      auto e = new Xception();
      e.errorCode = 1001;
      e.message = "This is an Xception";
      throw e;
    } else if (arg0 == "Xception2") {
      auto e = new Xception2();
      e.errorCode = 2002;
      e.struct_thing.string_thing = "This is an Xception2";
      throw e;
    } else {
      return Xtruct(arg1);
    }
  }

  override void testOneway(int sleepFor) {
    if (trace_) writefln("testOneway(%s): Sleeping...", sleepFor);
    Thread.sleep(dur!"seconds"(sleepFor));
    if (trace_) writefln("testOneway(%s): done sleeping!", sleepFor);
  }

private:
  bool trace_;
}

shared(bool) gShutdown = false;

nothrow @nogc extern(C) void handleSignal(int sig) {
  gShutdown = true;
}

// Runs a thread that waits for shutdown to be
// signaled and then triggers cancellation,
// causing the server to stop.  While we could
// use a signalfd for this purpose, we are instead
// opting for a busy waiting scheme for maximum
// portability since signalfd is a linux thing.

class ShutdownThread : Thread {
  this(TCancellationOrigin cancellation) {
    cancellation_ = cancellation;
    super(&run);
  }
  
private:
  void run() {
    while (!gShutdown) {
      Thread.sleep(dur!("msecs")(25));
    }
    cancellation_.trigger();
  }
  
  TCancellationOrigin cancellation_;
}

void main(string[] args) {
  ushort port = 9090;
  ServerType serverType;
  ProtocolType protocolType;
  size_t numIOThreads = 1;
  TransportType transportType;
  bool ssl = false;
  bool trace = true;
  size_t taskPoolSize = totalCPUs;

  getopt(args, "port", &port, "protocol", &protocolType, "server-type",
    &serverType, "ssl", &ssl, "num-io-threads", &numIOThreads,
    "task-pool-size", &taskPoolSize, "trace", &trace,
    "transport", &transportType);

  if (serverType == ServerType.nonblocking ||
    serverType == ServerType.pooledNonblocking
  ) {
    enforce(transportType == TransportType.framed,
      "Need to use framed transport with non-blocking server.");
    enforce(!ssl, "The non-blocking server does not support SSL yet.");

    // Don't wrap the contents into another layer of framing.
    transportType = TransportType.raw;
  }

  version (ThriftTestTemplates) {
    // Only exercise the specialized template code paths if explicitly enabled
    // to reduce memory consumption on regular test suite runs – there should
    // not be much that can go wrong with that specifically anyway.
    alias TypeTuple!(TBufferedTransport, TFramedTransport, TServerHttpTransport)
      AvailableTransports;
    alias TypeTuple!(
      staticMap!(TBinaryProtocol, AvailableTransports),
      staticMap!(TCompactProtocol, AvailableTransports)
    ) AvailableProtocols;
  } else {
    alias TypeTuple!() AvailableTransports;
    alias TypeTuple!() AvailableProtocols;
  }

  TProtocolFactory protocolFactory;
  final switch (protocolType) {
    case ProtocolType.binary:
      protocolFactory = new TBinaryProtocolFactory!AvailableTransports;
      break;
    case ProtocolType.compact:
      protocolFactory = new TCompactProtocolFactory!AvailableTransports;
      break;
    case ProtocolType.json:
      protocolFactory = new TJsonProtocolFactory!AvailableTransports;
      break;
  }

  auto processor = new TServiceProcessor!(ThriftTest, AvailableProtocols)(
    new TestHandler(trace));

  TServerSocket serverSocket;
  if (ssl) {
    auto sslContext = new TSSLContext();
    sslContext.serverSide = true;
    sslContext.loadCertificate("../../../test/keys/server.crt");
    sslContext.loadPrivateKey("../../../test/keys/server.key");
    sslContext.ciphers = "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH";
    serverSocket = new TSSLServerSocket(port, sslContext);
  } else {
    serverSocket = new TServerSocket(port);
  }

  auto transportFactory = createTransportFactory(transportType);

  auto server = createServer(serverType, numIOThreads, taskPoolSize,
    processor, serverSocket, transportFactory, protocolFactory);

  // Set up SIGINT signal handling
  enforce(signal(SIGINT, &handleSignal) != SIG_ERR,
    "Could not replace the SIGINT signal handler: errno {0}".format(errno()));
  
  // Set up a server cancellation trigger
  auto cancel = new TCancellationOrigin();

  // Set up a listener for the shutdown condition - this will
  // wake up when the signal occurs and trigger cancellation.
  auto shutdown = new ShutdownThread(cancel);
  shutdown.start();
  
  // Serve from this thread; the signal will stop the server
  // and control will return here
  writefln("Starting %s/%s %s ThriftTest server %son port %s...", protocolType,
    transportType, serverType, ssl ? "(using SSL) ": "", port);
  server.serve(cancel);
  shutdown.join();
  signal(SIGINT, SIG_DFL);
    
  writeln("done.");
}