OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TestServer.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.mapd.parser.server.test;
18 
19 import static org.junit.Assert.*;
20 
23 import com.mapd.thrift.calciteserver.CalciteServer;
24 import com.mapd.thrift.calciteserver.TPlanResult;
25 
26 import org.apache.thrift.TException;
27 import org.apache.thrift.protocol.TBinaryProtocol;
28 import org.apache.thrift.protocol.TProtocol;
29 import org.apache.thrift.transport.TSocket;
30 import org.apache.thrift.transport.TTransport;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 
38 import java.util.ArrayList;
39 import java.util.Random;
40 import java.util.concurrent.ExecutorService;
41 import java.util.concurrent.Executors;
42 
43 public class TestServer {
44  private final static Logger MAPDLOGGER = LoggerFactory.getLogger(TestServer.class);
45  private final static int TEST_THREAD_COUNT = 3;
46  private volatile int threadsRun = 0;
47  private volatile boolean threadHadFailure = false;
48  private volatile AssertionError ae;
49  private static CalciteServerWrapper csw = null;
50  private static SockTransportProperties client_skT = null;
51  private static SockTransportProperties server_skT = null;
52 
53  @BeforeClass
54  public static void startServer() {
55  csw = new CalciteServerWrapper(11000, 11001, "/data", null, client_skT, server_skT);
56  new Thread(csw).start();
57  }
58 
59  @AfterClass
60  public static void stopServer() {
61  csw.stopServer();
62  }
63 
64  @Ignore
65  public void testThreadedCall() {
66  final ExecutorService pool = Executors.newFixedThreadPool(TEST_THREAD_COUNT);
67 
68  Runnable r = new Runnable() {
69  @Override
70  public void run() {
71  try {
72  for (int i = 1; i <= 5; i++) {
74  }
75  } catch (AssertionError x) {
76  MAPDLOGGER.error("error during Runnable");
77  threadHadFailure = true;
78  ae = x;
79  }
80  threadsRun++;
82  pool.shutdown();
83  }
84  }
85  };
86 
87  for (int i = 0; i < TEST_THREAD_COUNT; i++) {
88  pool.submit(r);
89  }
90  while (!pool.isShutdown()) {
91  // stay alive
92  }
93  if (threadHadFailure) {
94  throw ae;
95  }
96  }
97 
98  @Ignore
99  public void testSimpleCall() {
100  callCalciteCheck("Select ENAME from EMP",
101  "{\n"
102  + " \"rels\": [\n"
103  + " {\n"
104  + " \"id\": \"0\",\n"
105  + " \"relOp\": \"LogicalTableScan\",\n"
106  + " \"fieldNames\": [\n"
107  + " \"EMPNO\",\n"
108  + " \"ENAME\",\n"
109  + " \"JOB\",\n"
110  + " \"MGR\",\n"
111  + " \"HIREDATE\",\n"
112  + " \"SAL\",\n"
113  + " \"COMM\",\n"
114  + " \"DEPTNO\",\n"
115  + " \"SLACKER\",\n"
116  + " \"SLACKARR1\",\n"
117  + " \"SLACKARR2\"\n"
118  + " ],\n"
119  + " \"table\": [\n"
120  + " \"CATALOG\",\n"
121  + " \"SALES\",\n"
122  + " \"EMP\"\n"
123  + " ],\n"
124  + " \"inputs\": []\n"
125  + " },\n"
126  + " {\n"
127  + " \"id\": \"1\",\n"
128  + " \"relOp\": \"LogicalProject\",\n"
129  + " \"fields\": [\n"
130  + " \"ENAME\"\n"
131  + " ],\n"
132  + " \"exprs\": [\n"
133  + " {\n"
134  + " \"input\": 1\n"
135  + " }\n"
136  + " ]\n"
137  + " }\n"
138  + " ]\n"
139  + "}");
140  }
141 
142  @Ignore
143  public void testRandomCall() {
145  }
146 
147  private void randomCalciteCall() {
148  Random r = new Random();
149  int aliasID = r.nextInt(100000) + 1000000;
151  String.format(
152  "Select TABALIAS%d.ENAME AS COLALIAS%d from EMP TABALIAS%d LIMIT %d",
153  aliasID,
154  aliasID,
155  aliasID,
156  aliasID),
157  String.format("{\n"
158  + " \"rels\": [\n"
159  + " {\n"
160  + " \"id\": \"0\",\n"
161  + " \"relOp\": \"LogicalTableScan\",\n"
162  + " \"fieldNames\": [\n"
163  + " \"EMPNO\",\n"
164  + " \"ENAME\",\n"
165  + " \"JOB\",\n"
166  + " \"MGR\",\n"
167  + " \"HIREDATE\",\n"
168  + " \"SAL\",\n"
169  + " \"COMM\",\n"
170  + " \"DEPTNO\",\n"
171  + " \"SLACKER\",\n"
172  + " \"SLACKARR1\",\n"
173  + " \"SLACKARR2\"\n"
174  + " ],\n"
175  + " \"table\": [\n"
176  + " \"CATALOG\",\n"
177  + " \"SALES\",\n"
178  + " \"EMP\"\n"
179  + " ],\n"
180  + " \"inputs\": []\n"
181  + " },\n"
182  + " {\n"
183  + " \"id\": \"1\",\n"
184  + " \"relOp\": \"LogicalProject\",\n"
185  + " \"fields\": [\n"
186  + " \"COLALIAS%d\"\n"
187  + " ],\n"
188  + " \"exprs\": [\n"
189  + " {\n"
190  + " \"input\": 1\n"
191  + " }\n"
192  + " ]\n"
193  + " },\n"
194  + " {\n"
195  + " \"id\": \"2\",\n"
196  + " \"relOp\": \"LogicalSort\",\n"
197  + " \"collation\": [],\n"
198  + " \"fetch\": {\n"
199  + " \"literal\": %d,\n"
200  + " \"type\": \"DECIMAL\",\n"
201  + " \"scale\": 0,\n"
202  + " \"precision\": 7,\n"
203  + " \"type_scale\": 0,\n"
204  + " \"type_precision\": 10\n"
205  + " }\n"
206  + " },\n"
207  + " {\n"
208  + " \"id\": \"3\",\n"
209  + " \"relOp\": \"LogicalProject\",\n"
210  + " \"fields\": [\n"
211  + " \"COLALIAS%d\"\n"
212  + " ],\n"
213  + " \"exprs\": [\n"
214  + " {\n"
215  + " \"input\": 0\n"
216  + " }\n"
217  + " ]\n"
218  + " },\n"
219  + " {\n"
220  + " \"id\": \"4\",\n"
221  + " \"relOp\": \"LogicalSort\",\n"
222  + " \"collation\": [],\n"
223  + " \"fetch\": {\n"
224  + " \"literal\": %d,\n"
225  + " \"type\": \"DECIMAL\",\n"
226  + " \"scale\": 0,\n"
227  + " \"precision\": 7,\n"
228  + " \"type_scale\": 0,\n"
229  + " \"type_precision\": 10\n"
230  + " }\n"
231  + " }\n"
232  + " ]\n"
233  + "}",
234  aliasID,
235  aliasID,
236  aliasID,
237  aliasID));
238  }
239 
240  private void callCalciteCheck(String query, String result) {
241  try {
242  TTransport transport;
243  transport = new TSocket("localhost", 11000);
244  transport.open();
245  TProtocol protocol = new TBinaryProtocol(transport);
246  CalciteServer.Client client = new CalciteServer.Client(protocol);
247  TPlanResult algebra = client.process(
248  "user", "passwd", "SALES", query, new ArrayList<>(), false, false, false);
249  transport.close();
250  try {
251  assertEquals(algebra.plan_result, result);
252  } catch (AssertionError s) {
253  MAPDLOGGER.error("error during callCalciteCheck");
254  throw s;
255  }
256  } catch (TException x) {
257  fail("Exception occurred " + x.toString());
258  }
259  }
260 }
static CalciteServerWrapper csw
Definition: TestServer.java:49
void callCalciteCheck(String query, String result)
static unsigned fail
Definition: testing.h:30
static SockTransportProperties client_skT
Definition: TestServer.java:50
static bool run
static SockTransportProperties server_skT
Definition: TestServer.java:51