def test_ssl_with_invalid_ca(self): pool = ConnectionPool() config = copy.copy(self.ssl_config) config.ca_certs = "invalid" with self.assertRaises(Exception): pool.init(self.addresses, self.configs, config)
def test_wrong_hostname(self): pool = ConnectionPool() try: pool.init([('wrong_host', 9669)], Config()) assert False except InValidHostname: assert True
def test_init_failed(self): # init succeeded pool1 = ConnectionPool() addresses = list() addresses.append(('127.0.0.1', 9669)) addresses.append(('127.0.0.1', 9670)) assert pool1.init(addresses, Config()) # init failed, connected failed pool2 = ConnectionPool() addresses = list() addresses.append(('127.0.0.1', 3800)) try: pool2.init(addresses, Config()) assert False except Exception: assert True # init failed, hostname not existed try: pool3 = ConnectionPool() addresses = list() addresses.append(('not_exist_hostname', 3800)) assert not pool3.init(addresses, Config()) except InValidHostname: assert True, "We expected get the exception"
def test_multi_thread(): # Test multi thread addresses = [('127.0.0.1', 9669), ('127.0.0.1', 9670)] configs = Config() configs.max_connection_pool_size = 4 pool = ConnectionPool() assert pool.init(addresses, configs) global success_flag success_flag = True def main_test(): session = None global success_flag try: session = pool.get_session('root', 'nebula') if session is None: success_flag = False return space_name = 'space_' + threading.current_thread().getName() session.execute('DROP SPACE %s' % space_name) resp = session.execute( 'CREATE SPACE IF NOT EXISTS %s(vid_type=FIXED_STRING(8))' % space_name ) if not resp.is_succeeded(): raise RuntimeError('CREATE SPACE failed: {}'.format(resp.error_msg())) time.sleep(3) resp = session.execute('USE %s' % space_name) if not resp.is_succeeded(): raise RuntimeError('USE SPACE failed:{}'.format(resp.error_msg())) except Exception as x: print(x) success_flag = False return finally: if session is not None: session.release() thread1 = threading.Thread(target=main_test, name='thread1') thread2 = threading.Thread(target=main_test, name='thread2') thread3 = threading.Thread(target=main_test, name='thread3') thread4 = threading.Thread(target=main_test, name='thread4') thread1.start() thread2.start() thread3.start() thread4.start() thread1.join() thread2.join() thread3.join() thread4.join() pool.close() assert success_flag
def get_conn_pool(host: str, port: int, ssl_config: SSL_config): config = Config() config.max_connection_pool_size = 20 config.timeout = 180000 # init connection pool pool = ConnectionPool() if not pool.init([(host, port)], config, ssl_config): raise Exception("Fail to init connection pool.") return pool
def start_standalone(self): os.chdir(self.work_dir) start_time = time.time() for p in self.all_processes: print('start stand alone process') p.start() config = Config() config.max_connection_pool_size = 20 config.timeout = 60000 # init connection pool client_pool = ConnectionPool() # assert client_pool.init([("127.0.0.1", int(self.graphd_port))], config) ssl_config = get_ssl_config(self.is_graph_ssl, self.ca_signed) print("begin to add hosts") ok = False # wait graph is ready, and then add hosts for _ in range(20): try: ok = client_pool.init( [("127.0.0.1", self.graphd_processes[0].tcp_port)], config, ssl_config, ) if ok: break except: pass time.sleep(1) assert ok, "graph is not ready" # get session from the pool client = client_pool.get_session('root', 'nebula') hosts = ",".join([ "127.0.0.1:{}".format(str(storaged.storage_port)) for storaged in self.graphd_processes ]) cmd = "ADD HOSTS {}".format(hosts) print("add hosts cmd is {}".format(cmd)) resp = client.execute(cmd) assert resp.is_succeeded(), resp.error_msg() client.release() # wait nebula start server_ports = [p.tcp_port for p in self.all_processes] if not self._check_servers_status(server_ports): self._collect_pids() self.kill_all(signal.SIGKILL) elapse = time.time() - start_time raise Exception(f'nebula servers not ready in {elapse}s') self._collect_pids() return [p.tcp_port for p in self.graphd_processes]
def setup_class(self): self.user_name = 'root' self.password = '******' self.configs = Config() self.configs.max_connection_pool_size = 6 self.pool = ConnectionPool() assert self.pool.init( [('127.0.0.1', 9669), ('127.0.0.1', 9670), ('127.0.0.1', 9671)], self.configs, ) assert self.pool.connects() == 0 assert self.pool.in_used_connects() == 0
def setup_class(self): self.addresses = list() self.addresses.append(('127.0.0.1', 9669)) self.addresses.append(('127.0.0.1', 9670)) self.configs = Config() self.configs.min_connection_pool_size = 2 self.configs.max_connection_pool_size = 4 self.configs.idle_time = 2000 self.configs.interval_check = 2 self.pool = ConnectionPool() assert self.pool.init(self.addresses, self.configs) assert self.pool.connects() == 2
def setUp(self) -> None: super().setUpClass() self.user_name = 'root' self.password = '******' self.configs = Config() self.configs.max_connection_pool_size = 6 self.pool = ConnectionPool() self.pool.init([('127.0.0.1', 9671)], self.configs) # get session from the pool client = self.pool.get_session('root', 'nebula') assert client is not None # prepare space and insert data resp = client.execute( 'CREATE SPACE IF NOT EXISTS parameter_test(vid_type=FIXED_STRING(30));USE parameter_test' ) assert resp.is_succeeded(), resp.error_msg() resp = client.execute( 'CREATE TAG IF NOT EXISTS person(name string, age int);' 'CREATE EDGE like (likeness double);') time.sleep(6) # insert data need to sleep after create schema resp = client.execute( 'CREATE TAG INDEX person_age_index on person(age)') time.sleep(6) # insert vertex resp = client.execute( 'INSERT VERTEX person(name, age) VALUES "Bob":("Bob", 10), "Lily":("Lily", 9)' ) assert resp.is_succeeded(), resp.error_msg() # insert edges resp = client.execute( 'INSERT EDGE like(likeness) VALUES "Bob"->"Lily":(80.0);') assert resp.is_succeeded(), resp.error_msg() resp = client.execute('REBUILD TAG INDEX person_age_index') assert resp.is_succeeded(), resp.error_msg() # prepare parameters bval = ttypes.Value() bval.set_bVal(True) ival = ttypes.Value() ival.set_iVal(3) sval = ttypes.Value() sval.set_sVal("Bob") self.params = {"p1": ival, "p2": bval, "p3": sval} assert self.pool.connects() == 1 assert self.pool.in_used_connects() == 1
def test_4_timeout(self): try: configs = Config() configs.timeout = 100 configs.max_connection_pool_size = 1 pool = ConnectionPool() assert pool.init([('127.0.0.1', 9669)], configs) session = pool.get_session(self.user_name, self.password) ngql = '' for n in range(0, 500): ngql = ngql + 'show hosts;' session.execute(ngql) assert False, 'expect to get exception' except Exception as ex: assert str(ex).find('timed out') > 0 assert True, ex
def test_timeout(self): config = Config() config.timeout = 1000 config.max_connection_pool_size = 1 pool = ConnectionPool() assert pool.init([('127.0.0.1', 9669)], config) session = pool.get_session('root', 'nebula') try: resp = session.execute( 'USE nba;GO 1000 STEPS FROM \"Tim Duncan\" OVER like' ) assert False except IOErrorException as e: assert True assert str(e).find("Read timed out") session.release() try: session = pool.get_session('root', 'nebula') except IOErrorException as e: assert False
def prepare_data(): config = Config() config.max_connection_pool_size = 1 # init connection pool connection_pool = ConnectionPool() # the graphd server's address assert connection_pool.init([('127.0.0.1', 9671)], config) client = connection_pool.get_session('root', 'nebula') client.execute( 'CREATE SPACE IF NOT EXISTS ScanSpace(' 'PARTITION_NUM=10,' 'vid_type=FIXED_STRING(20));' 'USE ScanSpace;' 'CREATE TAG IF NOT EXISTS person(name string, age int);' 'CREATE EDGE IF NOT EXISTS friend(start int, end int);' ) time.sleep(5) for id in range(20): vid = 'person' + str(id) cmd = 'INSERT VERTEX person(name, age) ' 'VALUES \"{}\":(\"{}\", {})'.format( vid, vid, id ) client.execute(cmd) for id in range(20): src_id = 'person' + str(id) dst_id = 'person' + str(20 - id) start = random.randint(2000, 2010) end = random.randint(2010, 2020) cmd = 'INSERT EDGE friend(start, end) ' 'VALUES \"{}\"->\"{}\":({}, {})'.format( src_id, dst_id, start, end ) client.execute(cmd) client.release() connection_pool.close()
def setUpClass(cls) -> None: super().setUpClass() configs = Config() configs.max_connection_pool_size = 1 cls.pool = ConnectionPool() cls.pool.init([('127.0.0.1', 9671)], configs) cls.session = cls.pool.get_session('root', 'nebula') resp = cls.session.execute(''' CREATE SPACE IF NOT EXISTS test_data(vid_type=FIXED_STRING(8)); USE test_data; CREATE TAG IF NOT EXISTS person(name string, age int8, grade int16, friends int32, book_num int64, birthday datetime, start_school date, morning time, property double, is_girl bool, child_name fixed_string(10), expend float, first_out_city timestamp, hobby string); CREATE TAG IF NOT EXISTS student(name string, interval duration); CREATE EDGE IF NOT EXISTS like(likeness double); CREATE EDGE IF NOT EXISTS friend(start_year int, end_year int); CREATE TAG INDEX IF NOT EXISTS person_name_index ON person(name(8)); ''') assert resp.is_succeeded(), resp.error_msg() time.sleep(5) resp = cls.session.execute( "INSERT VERTEX person(name, age, grade,friends, book_num," "birthday, start_school, morning, property," "is_girl, child_name, expend, first_out_city) VALUES" "'Bob':('Bob', 10, 3, 10, 100, datetime('2010-09-10T10:08:02')," "date('2017-09-10'), time('07:10:00'), " "1000.0, false, 'Hello World!', 100.0, 1111)," "'Lily':('Lily', 9, 3, 10, 100, datetime('2010-09-10T10:08:02'), " "date('2017-09-10'), time('07:10:00'), " "1000.0, false, 'Hello World!', 100.0, 1111)," "'Tom':('Tom', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'), " "date('2017-09-10'), time('07:10:00'), " "1000.0, false, 'Hello World!', 100.0, 1111)," "'Jerry':('Jerry', 9, 3, 10, 100, datetime('2010-09-10T10:08:02')," "date('2017-09-10'), time('07:10:00'), " "1000.0, false, 'Hello World!', 100.0, 1111), " "'John':('John', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'), " "date('2017-09-10'), time('07:10:00'), " "1000.0, false, 'Hello World!', 100.0, 1111)") assert resp.is_succeeded(), resp.error_msg() resp = cls.session.execute( "INSERT VERTEX student(name, interval) VALUES " "'Bob':('Bob', duration({months:1, seconds:100, microseconds:20}))," "'Lily':('Lily', duration({years: 1, seconds: 0}))," "'Tom':('Tom', duration({years: 1, seconds: 0}))," "'Jerry':('Jerry', duration({years: 1, seconds: 0}))," "'John':('John', duration({years: 1, seconds: 0}))") assert resp.is_succeeded(), resp.error_msg() resp = cls.session.execute("INSERT EDGE like(likeness) VALUES " "'Bob'->'Lily':(80.0), " "'Bob'->'Tom':(70.0), " "'Jerry'->'Lily':(84.0)," "'Tom'->'Jerry':(68.3), " "'Bob'->'John':(97.2)") assert resp.is_succeeded(), resp.error_msg() resp = cls.session.execute( "INSERT EDGE friend(start_year, end_year) VALUES " "'Bob'->'Lily':(2018, 2020), " "'Bob'->'Tom':(2018, 2020), " "'Jerry'->'Lily':(2018, 2020)," "'Tom'->'Jerry':(2018, 2020), " "'Bob'->'John':(2018, 2020)") assert resp.is_succeeded(), resp.error_msg()
except Exception as x: print(x) import traceback print(traceback.format_exc()) finally: if client is not None: client.release() if __name__ == '__main__': config = Config() config.max_connection_pool_size = 4 # init connection pool connection_pool = ConnectionPool() assert connection_pool.init([('127.0.0.1', 9669), ('127.0.0.1', 9670)], config) # Use multi thread and reuse the session three times for count in range(0, 3): threads = list() for i in range(0, 4): threads.append( threading.Thread(target=main_test, name='thread{}'.format(i))) for thread in threads: thread.start() for thread in threads: thread.join()
class TestConnectionPool(TestCase): @classmethod def setup_class(self): self.addresses = list() self.addresses.append(('127.0.0.1', 9669)) self.addresses.append(('127.0.0.1', 9670)) self.configs = Config() self.configs.min_connection_pool_size = 2 self.configs.max_connection_pool_size = 4 self.configs.idle_time = 2000 self.configs.interval_check = 2 self.pool = ConnectionPool() assert self.pool.init(self.addresses, self.configs) assert self.pool.connects() == 2 def test_right_hostname(self): pool = ConnectionPool() assert pool.init([('localhost', 9669)], Config()) def test_wrong_hostname(self): pool = ConnectionPool() try: pool.init([('wrong_host', 9669)], Config()) assert False except InValidHostname: assert True def test_ping(self): assert self.pool.ping(('127.0.0.1', 9669)) assert self.pool.ping(('127.0.0.1', 5000)) is False def test_init_failed(self): # init succeeded pool1 = ConnectionPool() addresses = list() addresses.append(('127.0.0.1', 9669)) addresses.append(('127.0.0.1', 9670)) assert pool1.init(addresses, Config()) # init failed, connected failed pool2 = ConnectionPool() addresses = list() addresses.append(('127.0.0.1', 3800)) try: pool2.init(addresses, Config()) assert False except Exception: assert True # init failed, hostname not existed try: pool3 = ConnectionPool() addresses = list() addresses.append(('not_exist_hostname', 3800)) assert not pool3.init(addresses, Config()) except InValidHostname: assert True, "We expected get the exception" def test_get_session(self): # get session succeeded sessions = list() for num in range(0, self.configs.max_connection_pool_size): session = self.pool.get_session('root', 'nebula') resp = session.execute('SHOW SPACES') assert resp.is_succeeded() sessions.append(session) # get session failed try: self.pool.get_session('root', 'nebula') except NotValidConnectionException: assert True assert self.pool.in_used_connects() == 4 # release session for session in sessions: session.release() assert self.pool.in_used_connects() == 0 assert self.pool.connects() == 4 # test get session after release for num in range(0, self.configs.max_connection_pool_size - 1): session = self.pool.get_session('root', 'nebula') resp = session.execute('SHOW SPACES') assert resp.is_succeeded() sessions.append(session) assert self.pool.in_used_connects() == 3 assert self.pool.connects() == 4 # test the idle connection delete time.sleep(5) assert self.pool.connects() == 3 def test_stop_close(self): session = self.pool.get_session('root', 'nebula') assert session is not None resp = session.execute('SHOW SPACES') assert resp.is_succeeded() self.pool.close() try: new_session = self.pool.get_session('root', 'nebula') except NotValidConnectionException: assert True except Exception as e: assert False, "We don't expect reach here:{}".format(e) try: session.execute('SHOW SPACES') except IOErrorException: assert True except Exception as e: assert False, "We don't expect reach here:".format(e) @pytest.mark.skip(reason="the test data without nba") def test_timeout(self): config = Config() config.timeout = 1000 config.max_connection_pool_size = 1 pool = ConnectionPool() assert pool.init([('127.0.0.1', 9669)], config) session = pool.get_session('root', 'nebula') try: resp = session.execute( 'USE nba;GO 1000 STEPS FROM \"Tim Duncan\" OVER like' ) assert False except IOErrorException as e: assert True assert str(e).find("Read timed out") session.release() try: session = pool.get_session('root', 'nebula') except IOErrorException as e: assert False
def test_right_hostname(self): pool = ConnectionPool() assert pool.init([('localhost', 9669)], Config())
class TestSession(TestCase): @classmethod def setup_class(self): self.user_name = 'root' self.password = '******' self.configs = Config() self.configs.max_connection_pool_size = 6 self.pool = ConnectionPool() assert self.pool.init( [('127.0.0.1', 9669), ('127.0.0.1', 9670), ('127.0.0.1', 9671)], self.configs, ) assert self.pool.connects() == 0 assert self.pool.in_used_connects() == 0 def test_1_release_by_del(self): def get_local_session(pool): session = pool.get_session('root', 'nebula') assert pool.in_used_connects() == 1 get_local_session(self.pool) assert self.pool.in_used_connects() == 0 def test_2_reconnect(self): try: session = self.pool.get_session('root', 'nebula') session.execute( 'CREATE SPACE IF NOT EXISTS test_session(vid_type=FIXED_STRING(8)); USE test_session;' ) time.sleep(3) for i in range(0, 5): if i == 3: os.system('docker stop tests_graphd0_1') os.system('docker stop tests_graphd1_1') time.sleep(3) # the session update later, the expect test # resp = session.execute('SHOW TAGS') resp = session.execute('SHOW HOSTS') assert resp.is_succeeded(), resp.error_msg() assert resp.space_name() == 'test_session' time.sleep(2) session.release() new_session = self.pool.get_session('root', 'nebula') new_session.execute('SHOW SPACES') except Exception as e: assert False, e finally: os.system('docker start tests_graphd0_1') os.system('docker start tests_graphd1_1') time.sleep(5) def test_3_session_context(self): in_used_connects = self.pool.in_used_connects() with self.pool.session_context('root', 'nebula') as session: assert self.pool.in_used_connects() == in_used_connects + 1 assert self.pool.in_used_connects() == in_used_connects def test_4_timeout(self): try: configs = Config() configs.timeout = 100 configs.max_connection_pool_size = 1 pool = ConnectionPool() assert pool.init([('127.0.0.1', 9669)], configs) session = pool.get_session(self.user_name, self.password) ngql = '' for n in range(0, 500): ngql = ngql + 'show hosts;' session.execute(ngql) assert False, 'expect to get exception' except Exception as ex: assert str(ex).find('timed out') > 0 assert True, ex
def test_ssl_with_ca(self): pool = ConnectionPool() assert pool.init(self.addresses, self.configs, self.ssl_config) session = pool.get_session("root", "nebula") resp = session.execute("SHOW HOSTS") assert resp.is_succeeded()
class TestParameter(TestCase): @classmethod def setUp(self) -> None: super().setUpClass() self.user_name = 'root' self.password = '******' self.configs = Config() self.configs.max_connection_pool_size = 6 self.pool = ConnectionPool() self.pool.init([('127.0.0.1', 9671)], self.configs) # get session from the pool client = self.pool.get_session('root', 'nebula') assert client is not None # prepare space and insert data resp = client.execute( 'CREATE SPACE IF NOT EXISTS parameter_test(vid_type=FIXED_STRING(30));USE parameter_test' ) assert resp.is_succeeded(), resp.error_msg() resp = client.execute( 'CREATE TAG IF NOT EXISTS person(name string, age int);' 'CREATE EDGE like (likeness double);') time.sleep(6) # insert data need to sleep after create schema resp = client.execute( 'CREATE TAG INDEX person_age_index on person(age)') time.sleep(6) # insert vertex resp = client.execute( 'INSERT VERTEX person(name, age) VALUES "Bob":("Bob", 10), "Lily":("Lily", 9)' ) assert resp.is_succeeded(), resp.error_msg() # insert edges resp = client.execute( 'INSERT EDGE like(likeness) VALUES "Bob"->"Lily":(80.0);') assert resp.is_succeeded(), resp.error_msg() resp = client.execute('REBUILD TAG INDEX person_age_index') assert resp.is_succeeded(), resp.error_msg() # prepare parameters bval = ttypes.Value() bval.set_bVal(True) ival = ttypes.Value() ival.set_iVal(3) sval = ttypes.Value() sval.set_sVal("Bob") self.params = {"p1": ival, "p2": bval, "p3": sval} assert self.pool.connects() == 1 assert self.pool.in_used_connects() == 1 def test_parameter(self): try: # get session from the pool client = self.pool.get_session('root', 'nebula') assert client is not None resp = client.execute_parameter( 'USE parameter_test', self.params, ) assert resp.is_succeeded() # test basic parameter resp = client.execute_parameter( 'RETURN abs($p1)+3 AS col1, (toBoolean($p2) and false) AS col2, toLower($p3)+1 AS col3', self.params, ) assert resp.is_succeeded(), resp.error_msg() assert 1 == resp.row_size() names = ['col1', 'col2', 'col3'] assert names == resp.keys() assert 6 == resp.row_values(0)[0].as_int() assert False == resp.row_values(0)[1].as_bool() assert 'bob1' == resp.row_values(0)[2].as_string() # test cypher parameter resp = client.execute_parameter( f'''MATCH (v:person)--() WHERE v.person.age>abs($p1)+3 RETURN v.person.name AS vname,v.person.age AS vage ORDER BY vage, $p3 LIMIT $p1+1''', self.params, ) assert resp.is_succeeded(), resp.error_msg() assert 2 == resp.row_size() names = ['vname', 'vage'] assert names == resp.keys() assert 'Lily' == resp.row_values(0)[0].as_string() assert 9 == resp.row_values(0)[1].as_int() assert 'Bob' == resp.row_values(1)[0].as_string() assert 10 == resp.row_values(1)[1].as_int() # test ngql parameter resp = client.execute_parameter( '$p1=go from "Bob" over like yield like._dst;', self.params, ) assert not resp.is_succeeded() resp = client.execute_parameter( 'go from $p3 over like yield like._dst;', self.params, ) assert not resp.is_succeeded() resp = client.execute_parameter( 'fetch prop on person $p3 yield vertex as v', self.params, ) assert not resp.is_succeeded() resp = client.execute_parameter( 'find all path from $p3 to "Yao Ming" over like yield path as p', self.params, ) assert not resp.is_succeeded() resp = client.execute_parameter( 'get subgraph from $p3 both like yield vertices as v', self.params, ) assert not resp.is_succeeded() resp = client.execute_parameter( 'go 3 steps from \"Bob\" over like yield like._dst limit [1,$p1,3]', self.params, ) assert not resp.is_succeeded() except Exception as e: assert False, e def tearDown(self) -> None: client = self.pool.get_session('root', 'nebula') assert client is not None resp = client.execute('DROP SPACE parameter_test') assert resp.is_succeeded(), resp.error_msg()
import time import json from nebula3.gclient.net import ConnectionPool from nebula3.Config import Config from nebula3.common import * from FormatResp import print_resp if __name__ == '__main__': client = None try: config = Config() config.max_connection_pool_size = 2 # init connection pool connection_pool = ConnectionPool() assert connection_pool.init([('127.0.0.1', 9669)], config) # get session from the pool client = connection_pool.get_session('root', 'nebula') assert client is not None # get the result in json format resp_json = client.execute_json("yield 1") json_obj = json.loads(resp_json) print(json.dumps(json_obj, indent=2, sort_keys=True)) client.execute( 'CREATE SPACE IF NOT EXISTS test(vid_type=FIXED_STRING(30)); USE test;' 'CREATE TAG IF NOT EXISTS person(name string, age int);' 'CREATE EDGE like (likeness double);'