OmniSciDB  04ee39c94c
TestMapDServer.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 
22 import com.mapd.thrift.server.MapD;
23 import com.mapd.thrift.server.TMapDException;
24 import com.mapd.thrift.server.TQueryResult;
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.Ignore;
32 import org.junit.Test;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 
36 import java.util.Random;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 
40 public class TestMapDServer {
41  private final static Logger MAPDLOGGER = LoggerFactory.getLogger(TestMapDServer.class);
42  private final static int TEST_THREAD_COUNT = 2;
43  private volatile int threadsRun = 0;
44  private volatile boolean threadHadFailure = false;
45  private volatile AssertionError ae;
46  private static CalciteServerWrapper csw = null;
47 
48  @Ignore
49  public void testThreadedCall() {
50  final ExecutorService pool = Executors.newFixedThreadPool(TEST_THREAD_COUNT);
51 
52  Runnable r = new Runnable() {
53  @Override
54  public void run() {
55  try {
57  Random r = new Random();
58  int calCount = r.nextInt(9) + 1;
59  int calls = 0;
60  for (int i = 1; i <= 100; i++) {
61  // if (i%100 == 0){
62  System.out.println("i is " + i);
63  // }
64  if (calls > calCount) {
65  closeMapDConnection(conn);
66  conn = createMapDConnection();
67  calCount = r.nextInt(9) + 1;
68  calls = 0;
69  }
70  randomMapDCall(conn);
71  calls++;
72  }
73  closeMapDConnection(conn);
74  } catch (AssertionError x) {
75  MAPDLOGGER.error("error during Runnable");
76  threadHadFailure = true;
77  ae = x;
78  }
79  threadsRun++;
80  if (threadsRun >= TEST_THREAD_COUNT) {
81  pool.shutdown();
82  }
83  }
84  };
85 
86  for (int i = 0; i < TEST_THREAD_COUNT; i++) {
87  pool.submit(r);
88  }
89  while (!pool.isShutdown()) {
90  // stay alive
91  }
92  if (threadHadFailure) {
93  throw ae;
94  }
95  }
96 
97  private void randomMapDCall(ConnInfo conn) {
98  Random r = new Random();
99  int aliasID = r.nextInt(100000) + 1000000;
100  int limit = r.nextInt(20) + 1;
101  // executeQuery(conn, String.format("Select TABALIAS%d.dest_lon AS COLALIAS%d
102  // from
103  // flights TABALIAS%d LIMIT %d",
104  // aliasID, aliasID, aliasID, limit), limit);
105  executeQuery(conn,
106  "SELECT date_trunc(day, arr_timestamp) as key0,CASE when carrier_name IN ('Southwest Airlines','American Airlines','Skywest Airlines','American Eagle Airlines','US Airways') then carrier_name ELSE 'other' END as key1,COUNT(*) AS val FROM flights_2008_7m WHERE (arr_timestamp >= TIMESTAMP(0) '2008-01-01 00:57:00' AND arr_timestamp < TIMESTAMP(0) '2009-01-01 18:27:00') GROUP BY key0, key1 ORDER BY key0,key1",
107  2202);
108  }
109 
111  String session = null;
112  TTransport transport = null;
113  MapD.Client client = null;
114  try {
115  transport = new TSocket("localhost", 6274);
116  transport.open();
117  TProtocol protocol = new TBinaryProtocol(transport);
118  client = new MapD.Client(protocol);
119  session = client.connect("admin", "HyperInteractive", "omnisci");
120  } catch (TException x) {
121  fail("Exception on create occurred " + x.toString());
122  }
123  return new ConnInfo(session, transport, client);
124  }
125 
126  private void executeQuery(ConnInfo conn, String query, int resultCount) {
127  try {
128  TQueryResult res = conn.client.sql_execute(conn.session, query, true, null, -1, -1);
129  if (resultCount != res.row_set.columns.get(0).nulls.size()) {
130  fail("result doesn't match " + resultCount
131  + " != " + res.row_set.columns.get(0).nulls.size());
132  }
133  } catch (TMapDException x) {
134  fail("Exception on EXECUTE " + x.toString());
135  } catch (TException x) {
136  fail("Exception on EXECUTE " + x.toString());
137  }
138  }
139 
140  private void closeMapDConnection(ConnInfo conn) {
141  try {
142  conn.client.disconnect(conn.session);
143  conn.transport.close();
144  } catch (TException x) {
145  fail("Exception on close occurred " + x.toString());
146  }
147  }
148 
149  private static class ConnInfo {
150  public String session;
151  public TTransport transport;
152  public MapD.Client client;
153 
154  private ConnInfo(String session, TTransport transport, MapD.Client client) {
155  this.session = session;
156  this.transport = transport;
157  this.client = client;
158  }
159  }
160 }
mapd::shared_ptr< MapDClient > client
static unsigned fail
Definition: testing.h:30
void executeQuery(ConnInfo conn, String query, int resultCount)
ConnInfo(String session, TTransport transport, MapD.Client client)
static bool run
TSessionId session