-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Tests need psutil * Check thread count only in standalone test run * Ignore
- Loading branch information
Showing
11 changed files
with
206 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .state import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import tempfile | ||
import shutil | ||
|
||
from chdb import query_stateful | ||
|
||
|
||
class Session(): | ||
""" | ||
Session will keep the state of query. All DDL and DML state will be kept in a dir. | ||
Dir path could be passed in as an argument. If not, a temporary dir will be created. | ||
If path is not specified, the temporary dir will be deleted when the Session object is deleted. | ||
Otherwise path will be kept. | ||
Note: The default database is "_local" and the default engine is "Memory" which means all data | ||
will be stored in memory. If you want to store data in disk, you should create another database. | ||
""" | ||
|
||
def __init__(self, path=None): | ||
if path is None: | ||
self._cleanup = True | ||
self._path = tempfile.mkdtemp() | ||
else: | ||
self._cleanup = False | ||
self._path = path | ||
|
||
def __del__(self): | ||
if self._cleanup: | ||
self.cleanup() | ||
|
||
def cleanup(self): | ||
shutil.rmtree(self._path) | ||
|
||
def query(self, sql, fmt="CSV"): | ||
""" | ||
Execute a query. | ||
""" | ||
return query_stateful(sql, fmt, path=self._path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!python3 | ||
|
||
import unittest | ||
import psutil | ||
from chdb import session | ||
|
||
|
||
current_process = psutil.Process() | ||
check_thread_count = False | ||
|
||
|
||
class TestStateful(unittest.TestCase): | ||
def test_zfree_thread_count(self): | ||
sess2 = session.Session() | ||
ret = sess2.query("SELECT sleep(2)", "Debug") | ||
# self.assertEqual(str(ret), "") | ||
thread_count = current_process.num_threads() | ||
print("Number of threads using psutil library: ", thread_count) | ||
if check_thread_count: | ||
self.assertEqual(thread_count, 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
check_thread_count = True | ||
unittest.main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#!python3 | ||
|
||
import unittest | ||
import gc | ||
import chdb | ||
|
||
class TestGC(unittest.TestCase): | ||
def test_gc(self): | ||
print("query started") | ||
gc.set_debug(gc.DEBUG_STATS) | ||
|
||
ret = chdb.query("SELECT 123,'adbcd'", 'CSV') | ||
# print("ret:", ret) | ||
# print("ret type:", type(ret)) | ||
self.assertEqual(str(ret), '123,"adbcd"\n') | ||
gc.collect() | ||
|
||
mv = ret.get_memview() | ||
self.assertIsNotNone(mv) | ||
gc.collect() | ||
|
||
self.assertEqual(len(mv), 12) | ||
|
||
out = mv.tobytes() | ||
self.assertEqual(out, b'123,"adbcd"\n') | ||
|
||
ret2 = chdb.query("SELECT 123,'adbcdefg'", 'CSV').get_memview().tobytes() | ||
self.assertEqual(ret2, b'123,"adbcdefg"\n') | ||
|
||
mv2 = chdb.query("SELECT 123,'adbcdefg'", 'CSV').get_memview() | ||
gc.collect() | ||
|
||
self.assertEqual(mv2.tobytes(), b'123,"adbcdefg"\n') | ||
|
||
mv3 = mv2.view() | ||
gc.collect() | ||
self.assertEqual(mv3.tobytes(), b'123,"adbcdefg"\n') | ||
self.assertEqual(len(mv3), 15) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#!python3 | ||
|
||
import time | ||
import shutil | ||
import psutil | ||
import unittest | ||
from chdb import session | ||
|
||
|
||
test_state_dir = ".state_tmp_auxten_" | ||
current_process = psutil.Process() | ||
check_thread_count = False | ||
|
||
class TestStateful(unittest.TestCase): | ||
def setUp(self) -> None: | ||
shutil.rmtree(test_state_dir, ignore_errors=True) | ||
return super().setUp() | ||
|
||
def tearDown(self) -> None: | ||
shutil.rmtree(test_state_dir, ignore_errors=True) | ||
return super().tearDown() | ||
|
||
def test_path(self): | ||
sess = session.Session(test_state_dir) | ||
sess.query("CREATE FUNCTION chdb_xxx AS () -> '0.12.0'", "CSV") | ||
ret = sess.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), '"0.12.0"\n') | ||
|
||
sess.query("CREATE DATABASE IF NOT EXISTS db_xxx ENGINE = Atomic", "CSV") | ||
ret = sess.query("SHOW DATABASES", "CSV") | ||
self.assertIn("db_xxx", str(ret)) | ||
|
||
sess.query( | ||
"CREATE TABLE IF NOT EXISTS db_xxx.log_table_xxx (x UInt8) ENGINE = Log;" | ||
) | ||
sess.query("INSERT INTO db_xxx.log_table_xxx VALUES (1), (2), (3), (4);") | ||
|
||
sess.query( | ||
"CREATE VIEW db_xxx.view_xxx AS SELECT * FROM db_xxx.log_table_xxx LIMIT 2;" | ||
) | ||
ret = sess.query("SELECT * FROM db_xxx.view_xxx", "CSV") | ||
self.assertEqual(str(ret), "1\n2\n") | ||
|
||
del sess # name sess dir will not be deleted | ||
|
||
sess = session.Session(test_state_dir) | ||
ret = sess.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), '"0.12.0"\n') | ||
|
||
ret = sess.query("SHOW DATABASES", "CSV") | ||
self.assertIn("db_xxx", str(ret)) | ||
|
||
ret = sess.query("SELECT * FROM db_xxx.log_table_xxx", "CSV") | ||
self.assertEqual(str(ret), "1\n2\n3\n4\n") | ||
|
||
# reuse session | ||
sess2 = session.Session(test_state_dir) | ||
|
||
ret = sess2.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), '"0.12.0"\n') | ||
|
||
# remove session dir | ||
sess2.cleanup() | ||
ret = sess2.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), "") | ||
|
||
def test_tmp(self): | ||
sess = session.Session() | ||
sess.query("CREATE FUNCTION chdb_xxx AS () -> '0.12.0'", "CSV") | ||
ret = sess.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), '"0.12.0"\n') | ||
del sess | ||
|
||
# another session | ||
sess2 = session.Session() | ||
ret = sess2.query("SELECT chdb_xxx()", "CSV") | ||
self.assertEqual(str(ret), "") | ||
|
||
def test_zfree_thread_count(self): | ||
time.sleep(3) | ||
thread_count = current_process.num_threads() | ||
print("Number of threads using psutil library: ", thread_count) | ||
if check_thread_count: | ||
self.assertEqual(thread_count, 1) | ||
|
||
if __name__ == "__main__": | ||
shutil.rmtree(test_state_dir, ignore_errors=True) | ||
check_thread_count = True | ||
unittest.main() |