test_lock.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import contextlib
  18. import unittest.mock
  19. import pytest
  20. import cc1101
  21. import cc1101.addresses
  22. # pylint: disable=protected-access
  23. @pytest.fixture(scope="function")
  24. def spidev_mock(tmp_path):
  25. class _SpiDevMock:
  26. def __init__(self):
  27. self._file = None
  28. def open(self, bus, select):
  29. path = tmp_path.joinpath(f"spidev{bus}.{select}~")
  30. self._file = path.open("w+")
  31. def fileno(self):
  32. # mimic behaviour of spidev.SpiDev.fileno()
  33. return self._file.fileno() if self._file else -1
  34. def close(self):
  35. self._file.close()
  36. self._file = None # for fileno
  37. return _SpiDevMock
  38. # pylint: disable=redefined-outer-name; using fixture
  39. @contextlib.contextmanager
  40. def _mock_hardware_access():
  41. with unittest.mock.patch.object(
  42. cc1101.CC1101, "_reset"
  43. ), unittest.mock.patch.object(
  44. cc1101.CC1101, "_verify_chip"
  45. ), unittest.mock.patch.object(
  46. cc1101.CC1101, "_configure_defaults"
  47. ), unittest.mock.patch.object(
  48. cc1101.CC1101,
  49. "get_main_radio_control_state_machine_state",
  50. return_value=cc1101.MainRadioControlStateMachineState.IDLE,
  51. ):
  52. yield
  53. def test_context_lock(spidev_mock):
  54. with _mock_hardware_access():
  55. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  56. transceiver = cc1101.CC1101(lock_spi_device=True)
  57. with transceiver:
  58. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  59. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  60. with pytest.raises(BlockingIOError):
  61. with transceiver2:
  62. pass
  63. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  64. transceiver3 = cc1101.CC1101(lock_spi_device=False)
  65. with transceiver3:
  66. pass
  67. with transceiver2: # closing unlocks
  68. pass
  69. def test_context_no_lock(spidev_mock):
  70. with _mock_hardware_access():
  71. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  72. transceiver = cc1101.CC1101(lock_spi_device=False)
  73. with transceiver:
  74. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  75. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  76. with transceiver2:
  77. pass
  78. def test_unlock_spi_device(spidev_mock):
  79. with _mock_hardware_access():
  80. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  81. transceiver = cc1101.CC1101(lock_spi_device=True)
  82. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  83. with transceiver: # acquire lock
  84. with pytest.raises(BlockingIOError):
  85. with transceiver2:
  86. pass
  87. transceiver.unlock_spi_device()
  88. with transceiver2:
  89. pass
  90. def test_unlock_spi_device_double(spidev_mock):
  91. with _mock_hardware_access():
  92. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  93. transceiver = cc1101.CC1101(lock_spi_device=True)
  94. # verify no error occurs
  95. with transceiver: # acquire lock
  96. transceiver.unlock_spi_device()
  97. transceiver.unlock_spi_device()
  98. def test_unlock_spi_device_outside_context(spidev_mock):
  99. with _mock_hardware_access():
  100. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  101. transceiver = cc1101.CC1101(lock_spi_device=True)
  102. # verify no error occurs
  103. transceiver.unlock_spi_device()
  104. with transceiver: # acquire lock
  105. pass
  106. transceiver.unlock_spi_device()
  107. def test_unlock_spi_device_no_lock(spidev_mock):
  108. with _mock_hardware_access():
  109. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  110. transceiver = cc1101.CC1101(lock_spi_device=False)
  111. with transceiver: # no lock acquired
  112. # verify no error occurs
  113. transceiver.unlock_spi_device()
  114. def test_unlock_on_exception_within_context(spidev_mock):
  115. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  116. transceiver1 = cc1101.CC1101(lock_spi_device=True)
  117. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  118. with _mock_hardware_access():
  119. with pytest.raises(ValueError, match=r"^test$"):
  120. with transceiver1:
  121. raise ValueError("test")
  122. with transceiver2:
  123. pass # no BlockingIOError
  124. # does locking still work after exception?
  125. with transceiver1:
  126. with pytest.raises(BlockingIOError):
  127. with transceiver2:
  128. pass
  129. with transceiver2:
  130. with pytest.raises(BlockingIOError):
  131. with transceiver1:
  132. pass
  133. def test_unlock_on_exception_when_entering_context(spidev_mock):
  134. with unittest.mock.patch("spidev.SpiDev", spidev_mock):
  135. transceiver1 = cc1101.CC1101(lock_spi_device=True)
  136. transceiver2 = cc1101.CC1101(lock_spi_device=True)
  137. with _mock_hardware_access():
  138. with unittest.mock.patch.object(
  139. cc1101.CC1101, "_verify_chip", side_effect=ValueError("test")
  140. ), pytest.raises(ValueError, match=r"^test$"):
  141. with transceiver1:
  142. pass # never reached
  143. with transceiver2:
  144. pass # no BlockingIOError
  145. # does locking still work after exception?
  146. with transceiver1:
  147. with pytest.raises(BlockingIOError):
  148. with transceiver2:
  149. pass
  150. with transceiver2:
  151. with pytest.raises(BlockingIOError):
  152. with transceiver1:
  153. pass