OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TestDBServer.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
23 import org.apache.thrift.TException;
24 import org.apache.thrift.protocol.TBinaryProtocol;
25 import org.apache.thrift.protocol.TProtocol;
26 import org.apache.thrift.transport.TSocket;
27 import org.apache.thrift.transport.TTransport;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 
33 import java.util.Random;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 
37 import ai.heavy.thrift.server.Heavy;
38 import ai.heavy.thrift.server.TDBException;
39 import ai.heavy.thrift.server.TQueryResult;
40 
41 public class TestDBServer {
42  private final static Logger HEAVYDBLOGGER = LoggerFactory.getLogger(TestDBServer.class);
43  private final static int TEST_THREAD_COUNT = 2;
44  private volatile int threadsRun = 0;
45  private volatile boolean threadHadFailure = false;
46  private volatile AssertionError ae;
47  private static CalciteServerWrapper csw = null;
48 
49  @Ignore
50  public void testThreadedCall() {
51  final ExecutorService pool = Executors.newFixedThreadPool(TEST_THREAD_COUNT);
52 
53  Runnable r = new Runnable() {
54  @Override
55  public void run() {
56  try {
58  Random r = new Random();
59  int calCount = r.nextInt(9) + 1;
60  int calls = 0;
61  for (int i = 1; i <= 100; i++) {
62  // if (i%100 == 0){
63  System.out.println("i is " + i);
64  // }
65  if (calls > calCount) {
66  closeDBConnection(conn);
67  conn = createDBConnection();
68  calCount = r.nextInt(9) + 1;
69  calls = 0;
70  }
71  randomDBCall(conn);
72  calls++;
73  }
74  closeDBConnection(conn);
75  } catch (AssertionError x) {
76  HEAVYDBLOGGER.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  private void randomDBCall(ConnInfo conn) {
99  Random r = new Random();
100  int aliasID = r.nextInt(100000) + 1000000;
101  int limit = r.nextInt(20) + 1;
102  // executeQuery(conn, String.format("Select TABALIAS%d.dest_lon AS COLALIAS%d
103  // from
104  // flights TABALIAS%d LIMIT %d",
105  // aliasID, aliasID, aliasID, limit), limit);
106  executeQuery(conn,
107  "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",
108  2202);
109  }
110 
112  String session = null;
113  TTransport transport = null;
114  Heavy.Client client = null;
115  try {
116  transport = new TSocket("localhost", 6274);
117  transport.open();
118  TProtocol protocol = new TBinaryProtocol(transport);
119  client = new Heavy.Client(protocol);
120  session = client.connect("admin", "HyperInteractive", "omnisci");
121  } catch (TException x) {
122  fail("Exception on create occurred " + x.toString());
123  }
124  return new ConnInfo(session, transport, client);
125  }
126 
127  private void executeQuery(ConnInfo conn, String query, int resultCount) {
128  try {
129  TQueryResult res = conn.client.sql_execute(conn.session, query, true, null, -1, -1);
130  if (resultCount != res.row_set.columns.get(0).nulls.size()) {
131  fail("result doesn't match " + resultCount
132  + " != " + res.row_set.columns.get(0).nulls.size());
133  }
134  } catch (TDBException x) {
135  fail("Exception on EXECUTE " + x.getError_msg());
136  } catch (TException x) {
137  fail("Exception on EXECUTE " + x.toString());
138  }
139  }
140 
142  try {
143  conn.client.disconnect(conn.session);
144  conn.transport.close();
145  } catch (TException x) {
146  fail("Exception on close occurred " + x.toString());
147  }
148  }
149 
150  private static class ConnInfo {
151  public String session;
152  public TTransport transport;
153  public Heavy.Client client;
154 
155  private ConnInfo(String session, TTransport transport, Heavy.Client client) {
156  this.session = session;
157  this.transport = transport;
158  this.client = client;
159  }
160  }
161 }
void executeQuery(ConnInfo conn, String query, int resultCount)
static CalciteServerWrapper csw
ConnInfo(String session, TTransport transport, Heavy.Client client)
tuple conn
Definition: report.py:41
static bool run