Skip to content

Commit ab14f5a

Browse files
committed
ci: rework interrupt tests to use a barrier and longer waits
1 parent 296a76e commit ab14f5a

2 files changed

Lines changed: 36 additions & 30 deletions

File tree

tests/fast/api/test_connection_interrupt.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import platform
22
import threading
33
import time
4+
from concurrent.futures import ThreadPoolExecutor
45

56
import duckdb
67
import pytest
@@ -11,20 +12,28 @@ class TestConnectionInterrupt(object):
1112
condition=platform.system() == "Emscripten",
1213
reason="threads not allowed on Emscripten",
1314
)
15+
@pytest.mark.timeout(10)
1416
def test_connection_interrupt(self):
1517
conn = duckdb.connect()
18+
barrier = threading.Barrier(2)
1619

17-
def interrupt():
18-
# Wait for query to start running before interrupting
19-
time.sleep(1)
20+
def execute_query():
21+
barrier.wait()
22+
return conn.execute('select * from range(1000000) t1, range(1000000) t2').fetchall()
23+
24+
def interrupt_query():
25+
barrier.wait()
26+
time.sleep(2)
2027
conn.interrupt()
2128

22-
thread = threading.Thread(target=interrupt)
23-
thread.start()
24-
with pytest.raises(duckdb.InterruptException):
25-
conn.execute('select * from range(100000) t1,range(100000) t2').fetchall()
29+
with ThreadPoolExecutor() as executor:
30+
query_future = executor.submit(execute_query)
31+
interrupt_future = executor.submit(interrupt_query)
32+
33+
interrupt_future.result()
2634

27-
thread.join()
35+
with pytest.raises((duckdb.InterruptException, duckdb.InvalidInputException)):
36+
query_future.result()
2837

2938
def test_interrupt_closed_connection(self):
3039
conn = duckdb.connect()
Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
import duckdb
2-
import time
3-
import pytest
4-
51
import platform
62
import threading
3+
import time
74
import _thread as thread
85

9-
10-
def send_keyboard_interrupt():
11-
# Wait a little, so we're sure the 'execute' has started
12-
time.sleep(1)
13-
# Send an interrupt to the main thread
14-
thread.interrupt_main()
6+
import duckdb
7+
import pytest
158

169

1710
class TestQueryInterruption(object):
1811
@pytest.mark.xfail(
1912
condition=platform.system() == "Emscripten",
2013
reason="Emscripten builds cannot use threads",
2114
)
15+
@pytest.mark.timeout(10)
2216
def test_query_interruption(self):
2317
con = duckdb.connect()
24-
thread = threading.Thread(target=send_keyboard_interrupt)
25-
# Start the thread
26-
thread.start()
27-
try:
28-
con.execute('select * from range(100000) t1,range(100000) t2').fetchall()
29-
except RuntimeError:
30-
# If this is not reached, we could not cancel the query before it completed
31-
# indicating that the query interruption functionality is broken
32-
assert True
33-
except KeyboardInterrupt:
34-
pytest.fail()
35-
thread.join()
18+
barrier = threading.Barrier(2)
19+
20+
def send_keyboard_interrupt():
21+
barrier.wait()
22+
time.sleep(2)
23+
thread.interrupt_main()
24+
25+
interrupt_thread = threading.Thread(target=send_keyboard_interrupt)
26+
interrupt_thread.start()
27+
28+
with pytest.raises((KeyboardInterrupt, RuntimeError)):
29+
barrier.wait()
30+
con.execute('select * from range(1000000) t1,range(1000000) t2').fetchall()
31+
32+
interrupt_thread.join()

0 commit comments

Comments
 (0)