File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -58,6 +58,11 @@ def open(self):
5858
5959
6060 def close (self ):
61+ # release the claimed interface/endpoints, otherwise a later open() (in this
62+ # process or another) can fail to claim the interface ("Resource busy")
63+ usb .util .dispose_resources (self ._device )
64+ self ._out_endpoint = None
65+ self ._in_endpoint = None
6166 self ._is_open = False
6267
6368
Original file line number Diff line number Diff line change 1+ """Tests for UsbConnection"""
2+ # pylint: disable=missing-function-docstring,protected-access
3+ # test names are self-explanatory, docstrings would only restate them
4+
5+ from unittest .mock import MagicMock , patch
6+
7+ from science_mode_4 .utils .usb_connection import UsbConnection
8+
9+
10+ def test_close_disposes_usb_resources_and_resets_state ():
11+ # regression test: close() used to only flip _is_open to False without releasing
12+ # the claimed USB interface/endpoints, so a later open() (in this process or
13+ # another) could fail to claim the interface ("Resource busy")
14+ device = MagicMock ()
15+ connection = UsbConnection (device )
16+ connection ._out_endpoint = MagicMock ()
17+ connection ._in_endpoint = MagicMock ()
18+ connection ._is_open = True
19+
20+ with patch ("science_mode_4.utils.usb_connection.usb.util.dispose_resources" ) as mock_dispose :
21+ connection .close ()
22+
23+ mock_dispose .assert_called_once_with (device )
24+ assert connection ._out_endpoint is None
25+ assert connection ._in_endpoint is None
26+ assert connection .is_open () is False
You can’t perform that action at this time.
0 commit comments