test_lock.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 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("spidev{}.{}~".format(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()