Skip to content

Commit 4384dff

Browse files
committed
Add test for issue #31
1 parent b6cdcd6 commit 4384dff

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

tests/run_all.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
test_loader = unittest.TestLoader()
66
test_suite = test_loader.discover('./')
77

8-
test_runner = unittest.TextTestRunner()
8+
test_runner = unittest.TextTestRunner(verbosity=2)
99
ret = test_runner.run(test_suite)
1010

1111
# if any test fails, exit with non-zero code

tests/test_issue31.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import time
5+
import hashlib
56
import unittest
67
import chdb
78
import zipfile
@@ -24,18 +25,50 @@ def download_and_extract(url, save_path):
2425
print("Done!")
2526

2627

27-
@timeout(20, use_signals=False)
28+
# @timeout(60, use_signals=False)
29+
30+
import signal
31+
32+
2833
def payload():
2934
now = time.time()
3035
res = chdb.query(
3136
'select Name, count(*) cnt from file("organizations-2000000.csv", CSVWithNames) group by Name order by cnt desc',
3237
"CSV",
3338
)
34-
print(res.get_memview().tobytes().decode("utf-8"))
39+
# calculate md5 of the result
40+
hash_out = hashlib.md5(res.get_memview().tobytes()).hexdigest()
41+
print("output length: ", len(res.get_memview().tobytes()))
42+
if hash_out != "60833f6ba30f2892f1fda976b2088570":
43+
print(res.get_memview().tobytes().decode("utf-8"))
44+
raise Exception(f"md5 not match {hash_out}")
3545
used_time = time.time() - now
3646
print("used time: ", used_time)
3747

3848

49+
class TimeoutTestRunner(unittest.TextTestRunner):
50+
def __init__(self, timeout=60, *args, **kwargs):
51+
super().__init__(*args, **kwargs)
52+
self.timeout = timeout
53+
54+
def run(self, test):
55+
class TimeoutException(Exception):
56+
pass
57+
58+
def handler(signum, frame):
59+
print("Timeout after {} seconds".format(self.timeout))
60+
raise TimeoutException("Timeout after {} seconds".format(self.timeout))
61+
62+
old_handler = signal.signal(signal.SIGALRM, handler)
63+
signal.alarm(self.timeout)
64+
65+
result = super().run(test)
66+
67+
signal.alarm(0)
68+
signal.signal(signal.SIGALRM, old_handler)
69+
return result
70+
71+
3972
class TestAggOnCSVSpeed(unittest.TestCase):
4073
def setUp(self):
4174
download_and_extract(csv_url, "organizations-2000000.zip")
@@ -44,9 +77,13 @@ def tearDown(self):
4477
os.remove("organizations-2000000.csv")
4578
os.remove("organizations-2000000.zip")
4679

47-
def test_agg(self):
80+
def _test_agg(self, arg=None):
4881
payload()
4982

83+
def test_agg(self):
84+
result = TimeoutTestRunner(timeout=20).run(self._test_agg)
85+
self.assertTrue(result.wasSuccessful(), "Test failed: took too long to execute")
86+
5087

5188
if __name__ == "__main__":
5289
unittest.main()

0 commit comments

Comments
 (0)