From 85bcec24ca19b023ed9ce741e672c8bbd9f49c0a Mon Sep 17 00:00:00 2001 From: chrisruk Date: Thu, 23 Jun 2022 11:21:59 +0100 Subject: [PATCH 1/2] Test uploading firmware --- test/firmware.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 test/firmware.py diff --git a/test/firmware.py b/test/firmware.py new file mode 100644 index 0000000..0da70a7 --- /dev/null +++ b/test/firmware.py @@ -0,0 +1,47 @@ +"""Test uploading firmware""" + +import time +import unittest +from multiprocessing import Process + +from gpiozero import DigitalOutputDevice + +RESET_GPIO_NUMBER = 4 +BOOT0_GPIO_NUMBER = 22 + + +def resethat(): + """Reset the HAT""" + reset = DigitalOutputDevice(RESET_GPIO_NUMBER) + boot0 = DigitalOutputDevice(BOOT0_GPIO_NUMBER) + boot0.off() + reset.off() + time.sleep(0.01) + reset.on() + time.sleep(0.01) + boot0.close() + reset.close() + time.sleep(0.5) + + +def reboot(): + """Reboot hat""" + from buildhat import Hat + resethat() + h = Hat(debug=True) + print(h.get()) + + +class TestFirmware(unittest.TestCase): + """Test firmware uploading functions""" + + def test_upload(self): + """Test uploading firmware""" + for _ in range(200): + p = Process(target=reboot) + p.start() + p.join() + + +if __name__ == '__main__': + unittest.main() From 0bd8fcf9901a2c97a47df4121a1c10345a4f5443 Mon Sep 17 00:00:00 2001 From: chrisruk Date: Fri, 24 Jun 2022 10:44:10 +0100 Subject: [PATCH 2/2] Catch exceptions in subprocess --- test/firmware.py | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/test/firmware.py b/test/firmware.py index 0da70a7..72723f8 100644 --- a/test/firmware.py +++ b/test/firmware.py @@ -2,7 +2,8 @@ import time import unittest -from multiprocessing import Process +from multiprocessing import Process, Queue +from queue import Empty from gpiozero import DigitalOutputDevice @@ -24,23 +25,37 @@ def resethat(): time.sleep(0.5) -def reboot(): - """Reboot hat""" - from buildhat import Hat - resethat() - h = Hat(debug=True) - print(h.get()) +def reboot(exc): + """Reboot hat and load firmware + + :param exc: Queue to pass exceptions + """ + try: + from buildhat import Hat + resethat() + h = Hat(debug=True) + print(h.get()) + except Exception as e: + exc.put(e) class TestFirmware(unittest.TestCase): """Test firmware uploading functions""" def test_upload(self): - """Test uploading firmware""" - for _ in range(200): - p = Process(target=reboot) + """Test upload firmware + + :raises exc.get: Raised if exception in subprocess + """ + for _ in range(500): + exc = Queue() + p = Process(target=reboot, args=(exc,)) p.start() p.join() + try: + raise exc.get(False) + except Empty: + pass if __name__ == '__main__':