Skip to content

Commit 0bd8fcf

Browse files
committed
Catch exceptions in subprocess
1 parent 85bcec2 commit 0bd8fcf

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

test/firmware.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import time
44
import unittest
5-
from multiprocessing import Process
5+
from multiprocessing import Process, Queue
6+
from queue import Empty
67

78
from gpiozero import DigitalOutputDevice
89

@@ -24,23 +25,37 @@ def resethat():
2425
time.sleep(0.5)
2526

2627

27-
def reboot():
28-
"""Reboot hat"""
29-
from buildhat import Hat
30-
resethat()
31-
h = Hat(debug=True)
32-
print(h.get())
28+
def reboot(exc):
29+
"""Reboot hat and load firmware
30+
31+
:param exc: Queue to pass exceptions
32+
"""
33+
try:
34+
from buildhat import Hat
35+
resethat()
36+
h = Hat(debug=True)
37+
print(h.get())
38+
except Exception as e:
39+
exc.put(e)
3340

3441

3542
class TestFirmware(unittest.TestCase):
3643
"""Test firmware uploading functions"""
3744

3845
def test_upload(self):
39-
"""Test uploading firmware"""
40-
for _ in range(200):
41-
p = Process(target=reboot)
46+
"""Test upload firmware
47+
48+
:raises exc.get: Raised if exception in subprocess
49+
"""
50+
for _ in range(500):
51+
exc = Queue()
52+
p = Process(target=reboot, args=(exc,))
4253
p.start()
4354
p.join()
55+
try:
56+
raise exc.get(False)
57+
except Empty:
58+
pass
4459

4560

4661
if __name__ == '__main__':

0 commit comments

Comments
 (0)