123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import ctypes
- import ctypes.util
- import datetime
- import errno
- import re
- import unittest.mock
- import pytest
- import cc1101._gpio
- def test__load_libgpiod():
- with unittest.mock.patch(
- "ctypes.util.find_library", return_value=ctypes.util.find_library("c")
- ) as find_library_mock:
- assert isinstance(cc1101._gpio._load_libgpiod(), ctypes.CDLL)
- find_library_mock.assert_called_once_with("gpiod")
- def test_load_libgpiod_not_found():
- cc1101._gpio._load_libgpiod.cache_clear()
- with unittest.mock.patch(
- "ctypes.util.find_library", return_value=None
- ), pytest.raises(FileNotFoundError):
- cc1101._gpio._load_libgpiod()
- @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
- def test_line_find_permission_denied(libgpiod_mock, name):
- libgpiod_mock.gpiod_line_find.return_value = 0
- ctypes.set_errno(errno.EACCES)
- with pytest.raises(
- PermissionError, match=f"^Failed to access GPIO line {re.escape(name)!r}\\.\\n"
- ):
- cc1101._gpio.GPIOLine.find(name.encode())
- @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
- def test_line_find_non_existing(libgpiod_mock, name):
- libgpiod_mock.gpiod_line_find.return_value = 0
- ctypes.set_errno(errno.ENOENT)
- with pytest.raises(
- FileNotFoundError, match=f"^GPIO line {re.escape(name)!r} does not exist\\.\\n"
- ):
- cc1101._gpio.GPIOLine.find(name.encode())
- @pytest.mark.parametrize("name", ("GPIO24", "GPIO25"))
- def test_line_find_unknown_error(libgpiod_mock, name):
- libgpiod_mock.gpiod_line_find.return_value = 0
- ctypes.set_errno(errno.ENOANO)
- with pytest.raises(
- OSError, match=f"^Failed to open GPIO line {re.escape(name)!r}: ENOANO$"
- ):
- cc1101._gpio.GPIOLine.find(name.encode())
- def test_line_find(libgpiod_mock):
- libgpiod_mock.gpiod_line_find.return_value = 21
- line = cc1101._gpio.GPIOLine.find(b"GPIO24")
- libgpiod_mock.gpiod_line_find.assert_called_once_with(b"GPIO24")
- assert isinstance(line, cc1101._gpio.GPIOLine)
- assert line._pointer.value == 21
- def test_line_release(libgpiod_mock):
- line = cc1101._gpio.GPIOLine(42)
- del line
- libgpiod_mock.gpiod_line_close_chip.assert_called_once_with(42)
- @pytest.mark.parametrize("consumer", (b"CC1101 GDO0", b"test"))
- @pytest.mark.parametrize("timeout_seconds", (21.42, 0.1234))
- @pytest.mark.parametrize("reached_timeout", (False, True))
- def test_line_wait_for_rising_edge(
- libgpiod_mock, consumer: bytes, timeout_seconds: float, reached_timeout: bool
- ):
- pointer = ctypes.c_void_p(1234)
- line = cc1101._gpio.GPIOLine(pointer=pointer)
- libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
- libgpiod_mock.gpiod_line_event_wait.return_value = 0 if reached_timeout else 1
- event_occured = line.wait_for_rising_edge(
- consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
- )
- assert event_occured is not reached_timeout
- libgpiod_mock.gpiod_line_request_rising_edge_events.assert_called_once_with(
- pointer, consumer
- )
- libgpiod_mock.gpiod_line_event_wait.assert_called_once()
- wait_args, wait_kwargs = libgpiod_mock.gpiod_line_event_wait.call_args
- assert wait_args[0] == pointer
- assert (
- wait_args[1].contents.tv_sec + wait_args[1].contents.tv_nsec / 10**9
- ) == pytest.approx(timeout_seconds, abs=1e-10)
- assert not wait_args[2:]
- assert not wait_kwargs
- libgpiod_mock.gpiod_line_release.assert_called_once_with(pointer)
- @pytest.mark.parametrize("consumer", (b"CC1101 GDO0",))
- @pytest.mark.parametrize("timeout_seconds", (21.42,))
- def test_line_wait_for_rising_edge_busy(
- libgpiod_mock, consumer: bytes, timeout_seconds: float
- ):
- pointer = ctypes.c_void_p(1234)
- line = cc1101._gpio.GPIOLine(pointer=pointer)
- libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = -1
- ctypes.set_errno(errno.EBUSY)
- with pytest.raises(
- OSError,
- match=r"^Request for rising edge event notifications failed \(EBUSY\)."
- r"\nBlocked by another process\?$",
- ):
- line.wait_for_rising_edge(
- consumer=consumer, timeout=datetime.timedelta(seconds=timeout_seconds)
- )
- def test_line_wait_for_rising_edge_wait_failed(libgpiod_mock) -> None:
- line = cc1101._gpio.GPIOLine(pointer=ctypes.c_void_p(42 // 2))
- libgpiod_mock.gpiod_line_request_rising_edge_events.return_value = 0
-
- libgpiod_mock.gpiod_line_event_wait.return_value = -1
- with pytest.raises(
- OSError, match="^Failed to wait for rising edge event notification.$"
- ):
- line.wait_for_rising_edge(
- consumer=b"test", timeout=datetime.timedelta(seconds=1)
- )
|