test: Events and signals

This commit is contained in:
akira committed 2024-10-29 14:15:16 +01:00
1 parent 32d113540c
commit 1de230c1e2
4 files changed
+178

No files matched your search

+1
View File
@@ -0,0 +1 @@
{"App":{"name": "PiCtory", "version": "1.2.3","saveTS": "20170826120906","language": "en","layout": {"north":{"size":70,"initClosed":false,"initHidden":false},"south":{"size":480,"initClosed":false,"initHidden":false,"children":{"layout1":{"east":{"size":500,"initClosed":false,"initHidden":false}}}},"east":{"size":70,"initClosed":true,"initHidden":false,"children":{"layout1":{"north":{"size":100,"initClosed":false,"initHidden":false},"south":{"size":98,"initClosed":false,"initHidden":false}}}},"west":{"size":200,"initClosed":false,"initHidden":false,"children":{"layout1":{}}}}},"Summary":{"inpTotal": 338,"outTotal": 113},"Devices":[{"catalogNr": "RevPiCore","GUID": "4ebb85e8-92ea-415d-b191-cc58c2857c0a","id": "device_RevPiCore_20170404_1_2_001","type": "BASE","productType": "95","position": "0","name": "picore01","bmk": "RevPi Core V1.2","inpVariant": 0,"outVariant": 0,"comment": "This is a RevPiCore Device","offset": 0,"inp": {"0": ["RevPiStatus","0","8","0",true,"0000", "",""],"1": ["RevPiIOCycle","0","8","1",true,"0001", "",""],"2": ["RS485ErrorCnt","0","16","2",false,"0002", "",""],"3": ["Core_Temperatur","0","8","4",false,"0003", "",""],"4": ["Core_Frequency","0","8","5",false,"0004", "",""]},"out": {"0": ["RevPiLED","0","8","6",true,"0005", "",""],"1": ["RS485ErrorLimit1","10","16","7",false,"0006", "",""],"2": ["RS485ErrorLimit2","1000","16","9",false,"0007", "",""]},"mem": {},"extend": {}},{"catalogNr": "RevPiDI","GUID": "2e2c089e-6bef-14c0-852b-41b7cf48d846","id": "device_RevPiDI_20160818_1_0_001","type": "LEFT_RIGHT","productType": "97","position": "32","name": "di01","bmk": "RevPi DI","inpVariant": 0,"outVariant": 0,"comment": "","offset": 11,"inp": {"0": ["notaus_ok","0","1","0",true,"0000", "","0"],"1": ["motorschutz_ok","0","1","0",true,"0001", "","1"],"2": ["fu_ok","0","1","0",true,"0002", "","2"],"3": ["fu_motoran","0","1","0",true,"0003", "","3"],"4": ["I_5","0","1","0",true,"0004", "","4"],"5": ["I_6","0","1","0",true,"0005", "","5"],"6": ["I_7","0","1","0",true,"0006", "","6"],"7": ["I_8","0","1","0",true,"0007", "","7"],"8": ["t_automatik","0","1","0",true,"0008", "","8"],"9": ["t_start","0","1","0",true,"0009", "","9"],"10": ["t_stop","0","1","0",true,"0010", "","10"],"11": ["t_quit","0","1","0",true,"0011", "","11"],"12": ["t_notaus","0","1","0",true,"0012", "","12"],"13": ["I_14","0","1","0",true,"0013", "","13"],"14": ["I_15","0","1","0",true,"0014", "","14"],"15": ["I_16","0","1","0",true,"0015", "","15"],"16": ["Status","0","16","4",false,"0016", "",""],"17": ["Counter_1","0","32","6",false,"0017", "",""],"18": ["Counter_2","0","32","10",false,"0018", "",""],"19": ["Counter_3","0","32","14",false,"0019", "",""],"20": ["Counter_4","0","32","18",false,"0020", "",""],"21": ["Counter_5","0","32","22",false,"0021", "",""],"22": ["Counter_6","0","32","26",false,"0022", "",""],"23": ["Counter_7","0","32","30",false,"0023", "",""],"24": ["Counter_8","0","32","34",false,"0024", "",""],"25": ["Counter_9","0","32","38",false,"0025", "",""],"26": ["Counter_10","0","32","42",false,"0026", "",""],"27": ["Counter_11","0","32","46",false,"0027", "",""],"28": ["Counter_12","0","32","50",false,"0028", "",""],"29": ["Counter_13","0","32","54",false,"0029", "",""],"30": ["Counter_14","0","32","58",false,"0030", "",""],"31": ["Counter_15","0","32","62",false,"0031", "",""],"32": ["Counter_16","0","32","66",false,"0032", "",""],"33": ["Output_Status","0","16","2",false,"0050", "",""]},"out": {"0": ["Output","0","16","70",false,"0051", "",""],"1": ["PWM_1","0","8","72",false,"0052", "",""],"2": ["PWM_2","0","8","73",false,"0053", "",""],"3": ["PWM_3","0","8","74",false,"0054", "",""],"4": ["PWM_4","0","8","75",false,"0055", "",""],"5": ["PWM_5","0","8","76",false,"0056", "",""],"6": ["PWM_6","0","8","77",false,"0057", "",""],"7": ["PWM_7","0","8","78",false,"0058", "",""],"8": ["PWM_8","0","8","79",false,"0059", "",""],"9": ["PWM_9","0","8","80",false,"0060", "",""],"10": ["PWM_10","0","8","81",false,"0061", "",""],"11": ["PWM_11","0","8","82",false,"0062", "",""],"12": ["PWM_12","0","8","83",false,"0063", "",""],"13": ["PWM_13","0","8","84",false,"0064", "",""],"14": ["PWM_14","0","8","85",false,"0065", "",""],"15": ["PWM_15","0","8","86",false,"0066", "",""],"16": ["PWM_16","0","8","87",false,"0067", "",""]},"mem": {"0": ["InputMode_1","0","8","88",false,"0033", "",""],"1": ["InputMode_2","0","8","89",false,"0034", "",""],"2": ["InputMode_3","0","8","90",false,"0035", "",""],"3": ["InputMode_4","0","8","91",false,"0036", "",""],"4": ["InputMode_5","0","8","92",false,"0037", "",""],"5": ["InputMode_6","0","8","93",false,"0038", "",""],"6": ["InputMode_7","0","8","94",false,"0039", "",""],"7": ["InputMode_8","0","8","95",false,"0040", "",""],"8": ["InputMode_9","0","8","96",false,"0041", "",""],"9": ["InputMode_10","0","8","97",false,"0042", "",""],"10": ["InputMode_11","0","8","98",false,"0043", "",""],"11": ["InputMode_12","0","8","99",false,"0044", "",""],"12": ["InputMode_13","0","8","100",false,"00Line truncated
+135
View File
@@ -0,0 +1,135 @@
# -*- coding: utf-8 -*-
"""Test events."""
__author__ = "Sven Sager"
__copyright__ = "Copyright (C) 2024 Sven Sager"
__license__ = "GPLv2"
from os.path import dirname
from threading import Event
from time import sleep
from revpimodio2 import RISING, FALLING
from tests import TestRevPiModIO
from tests.helper import ChangeThread
event_data = (None, None)
def xxx(name, value):
"""Test event function."""
global event_data
event_data = (name, value)
class TestEvents(TestRevPiModIO):
data_dir = dirname(__file__)
def setUp(self):
global event_data
event_data = (None, None)
super().setUp()
def test_events(self):
"""Test event functions."""
rpi = self.modio()
with self.assertRaises(ValueError):
rpi.io.magazin1.reg_event(xxx, edge=RISING)
rpi.io.magazin1.reg_event(xxx)
with self.assertRaises(RuntimeError):
rpi.io.magazin1.reg_event(xxx)
self.assertTrue(rpi.io.magazin1 in rpi.device.virt01._dict_events)
rpi.io.magazin1.unreg_event()
self.assertFalse(rpi.io.magazin1 in rpi.device.virt01._dict_events)
with self.assertRaises(ValueError):
rpi.io.v_druck.reg_event(None)
with self.assertRaises(ValueError):
rpi.io.v_druck.reg_event(xxx, delay=-100)
rpi.io.v_druck.reg_event(xxx, delay=100, edge=RISING)
with self.assertRaises(RuntimeError):
rpi.io.v_druck.reg_event(xxx)
with self.assertRaises(RuntimeError):
rpi.io.v_druck.reg_event(xxx, edge=RISING)
rpi.io.v_druck.reg_event(xxx, edge=FALLING, as_thread=True)
rpi.io.v_druck.reg_event(lambda name, value: None, edge=FALLING, as_thread=True)
self.assertEqual(len(rpi.device.do01._dict_events[rpi.io.v_druck]), 3)
rpi.io.v_druck.unreg_event(xxx, RISING)
def test_dd_timer_events(self):
"""Testet timer events."""
rpi = self.modio()
with self.assertRaises(ValueError):
rpi.io.magazin1.reg_timerevent(xxx, 200, edge=RISING)
rpi.io.magazin1.reg_timerevent(xxx, 200)
with self.assertRaises(RuntimeError):
rpi.io.magazin1.reg_timerevent(xxx, 200)
rpi.io.magazin1.reg_timerevent(lambda name, value: None, 200)
with self.assertRaises(ValueError):
rpi.io.v_druck.reg_timerevent(None, 200)
with self.assertRaises(ValueError):
rpi.io.v_druck.reg_timerevent(xxx, -100)
rpi.io.v_druck.reg_timerevent(xxx, 100, edge=RISING)
with self.assertRaises(RuntimeError):
rpi.io.v_druck.reg_timerevent(xxx, 200)
with self.assertRaises(RuntimeError):
rpi.io.v_druck.reg_timerevent(xxx, 200, edge=RISING)
rpi.io.v_druck.reg_timerevent(xxx, 200, edge=FALLING, as_thread=True)
self.assertEqual(len(rpi.device.do01._dict_events[rpi.io.v_druck]), 2)
rpi.io.v_druck.unreg_event(xxx, RISING)
self.assertEqual(len(rpi.device.do01._dict_events[rpi.io.v_druck]), 1)
rpi.io.v_druck.unreg_event(xxx, FALLING)
self.assertFalse(rpi.io.v_druck in rpi.device.do01._dict_events)
rpi.io.magazin1.unreg_event()
def test_dh_wait(self):
"""Test .wait functions of IOs."""
rpi = self.modio()
with self.assertRaises(RuntimeError):
rpi.io.v_druck.wait()
rpi.autorefresh_all()
with self.assertRaises(ValueError):
rpi.io.v_druck.wait(edge=30)
with self.assertRaises(ValueError):
rpi.io.v_druck.wait(edge=34)
with self.assertRaises(TypeError):
rpi.io.v_druck.wait(exitevent=True)
with self.assertRaises(ValueError):
rpi.io.v_druck.wait(timeout=-1)
with self.assertRaises(ValueError):
rpi.io.meldung0_7.wait(edge=RISING)
self.assertEqual(rpi.io.v_druck.wait(okvalue=False), -1)
self.assertEqual(rpi.io.v_druck.wait(timeout=100), 2)
self.assertEqual(rpi.io.v_druck.wait(edge=RISING, timeout=100), 2)
# Exit event caught
x = Event()
x.set()
self.assertEqual(rpi.io.v_druck.wait(exitevent=x), 1)
# Successful waiting
th = ChangeThread(rpi, "fu_lahm", True, 0.5)
th.start()
self.assertEqual(rpi.io.fu_lahm.wait(), 0)
th = ChangeThread(rpi, "fu_lahm", False, 0.3)
th.start()
th = ChangeThread(rpi, "fu_lahm", True, 0.6)
th.start()
self.assertEqual(rpi.io.fu_lahm.wait(edge=RISING), 0)
# Error while refreshing IO data
rpi._imgwriter.stop()
self.assertEqual(rpi.io.v_druck.wait(timeout=100), 2)
rpi.io.fu_lahm.value = False
sleep(0.1)
rpi.exit()
+29
View File
@@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
"""Test signals."""
__author__ = "Sven Sager"
__copyright__ = "Copyright (C) 2024 Sven Sager"
__license__ = "GPLv2"
from os.path import dirname
from tests import TestRevPiModIO
from tests.helper import ExitSignal
class TestSignals(TestRevPiModIO):
data_dir = dirname(__file__)
def test_handle_signal_end(self):
rpi = self.modio(autorefresh=True)
rpi.io.v_druck.value = True
def ende():
rpi.io.v_druck.value = False
rpi.handlesignalend(ende)
th_ende = ExitSignal(1)
th_ende.start()
rpi.mainloop()
self.assertFalse(rpi.io.v_druck.value)
+13
View File
@@ -27,6 +27,19 @@ class ChangeThread(Thread):
self.revpi.io[self.ioname].value = self.iovalue self.revpi.io[self.ioname].value = self.iovalue
class ExitSignal(Thread):
"""Call SIGINT after given time."""
def __init__(self, time):
"""Signal SIGINT after given time."""
super().__init__()
self.time = time
def run(self):
sleep(self.time)
os.kill(os.getpid(), SIGINT)
class ExitThread(Thread): class ExitThread(Thread):
"""Call .exit() of ModIO after given time.""" """Call .exit() of ModIO after given time."""