22
33import os
44import time
5+ import hashlib
56import unittest
67import chdb
78import 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+
2833def 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+
3972class 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
5188if __name__ == "__main__" :
5289 unittest .main ()
0 commit comments