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 /**
21  * Various helpers used by more than a single test.
22  */
23 module test_utils;
24 
25 import std.parallelism : TaskPool;
26 import thrift.protocol.base;
27 import thrift.protocol.processor;
28 import thrift.server.base;
29 import thrift.server.nonblocking;
30 import thrift.server.simple;
31 import thrift.server.taskpool;
32 import thrift.server.threaded;
33 import thrift.server.transport.socket;
34 import thrift.transport.base;
35 import thrift.transport.buffered;
36 import thrift.transport.framed;
37 import thrift.transport.http;
38 import thrift.transport.zlib;
39 
40 // This is a likely victim of @@BUG4744@@ when used with command argument
41 // parsing.
42 enum ServerType {
43   simple,
44   nonblocking,
45   pooledNonblocking,
46   taskpool,
47   threaded
48 }
49 
createServer(ServerType type,size_t taskPoolSize,size_t numIOThreads,TProcessor processor,TServerSocket serverTransport,TTransportFactory transportFactory,TProtocolFactory protocolFactory)50 TServer createServer(ServerType type, size_t taskPoolSize, size_t numIOThreads,
51   TProcessor processor, TServerSocket serverTransport,
52   TTransportFactory transportFactory, TProtocolFactory protocolFactory)
53 {
54   final switch (type) {
55     case ServerType.simple:
56       return new TSimpleServer(processor, serverTransport,
57         transportFactory, protocolFactory);
58     case ServerType.nonblocking:
59       auto nb = new TNonblockingServer(processor, serverTransport.port,
60         transportFactory, protocolFactory);
61       nb.numIOThreads = numIOThreads;
62       return nb;
63     case ServerType.pooledNonblocking:
64       auto nb = new TNonblockingServer(processor, serverTransport.port,
65         transportFactory, protocolFactory, new TaskPool(taskPoolSize));
66       nb.numIOThreads = numIOThreads;
67       return nb;
68     case ServerType.taskpool:
69       auto tps = new TTaskPoolServer(processor, serverTransport,
70         transportFactory, protocolFactory);
71       tps.taskPool = new TaskPool(taskPoolSize);
72       return tps;
73     case ServerType.threaded:
74       return new TThreadedServer(processor, serverTransport,
75         transportFactory, protocolFactory);
76   }
77 }
78 
79 enum TransportType {
80   buffered,
81   framed,
82   http,
83   zlib,
84   raw
85 }
86 
createTransportFactory(TransportType type)87 TTransportFactory createTransportFactory(TransportType type) {
88   final switch (type) {
89     case TransportType.buffered:
90       return new TBufferedTransportFactory;
91     case TransportType.framed:
92       return new TFramedTransportFactory;
93     case TransportType.http:
94       return new TServerHttpTransportFactory;
95     case TransportType.zlib:
96       return new TZlibTransportFactory;
97     case TransportType.raw:
98       return new TTransportFactory;
99   }
100 }
101