def test_disconnect_bad_handle(self): """Do not attempt to disconnect if the device handle is bad.""" spec = AvsFiberSpectrograph() spec.handle = AvsReturnCode.invalidHandle.value spec.disconnect() self.patch.return_value.AVS_Deactivate.assert_not_called() self.patch.return_value.AVS_Done.assert_called_once_with()
async def test_disconnect_stop_exposure_exception(self): """Test that `disconnect` does not raise if `stop_exposure` raises, but does log an error message, and continues with deactivating the device. """ duration = 5 # seconds spec = AvsFiberSpectrograph() self.patch.return_value.AVS_StopMeasure.return_value = ( AvsReturnCode.ERR_INVALID_PARAMETER.value) t0 = time.monotonic() task = asyncio.create_task(spec.expose(duration)) await asyncio.sleep(0.1) # give the event loop time to start try: with self.assertLogs(spec.log, "ERROR"): spec.disconnect() except AvsReturnError: self.fail( "disconnect() should not raise an exception, even if `stop_exposure` does." ) with pytest.raises(asyncio.CancelledError): await task t1 = time.monotonic() # cancelling the task should make it end much sooner than the duration assert t1 - t0 < 1 self.patch.return_value.AVS_StopMeasure.assert_called_with(self.handle) self.patch.return_value.AVS_Deactivate.assert_called_once_with( self.handle) self.patch.return_value.AVS_Done.assert_called_once_with() assert spec.handle is None
def test_disconnect(self): """Test a successful USB disconnect command.""" spec = AvsFiberSpectrograph() spec.disconnect() self.patch.return_value.AVS_Deactivate.assert_called_once_with( self.handle) self.patch.return_value.AVS_Done.assert_called_once_with() assert spec.handle is None
def test_disconnect_fails_logged(self): """Test that a "failed" Deactivate emits an error.""" self.patch.return_value.AVS_Deactivate.return_value = False spec = AvsFiberSpectrograph() with self.assertLogs(spec.log, "ERROR"): spec.disconnect() self.patch.return_value.AVS_Deactivate.assert_called_once_with( self.handle) self.patch.return_value.AVS_Done.assert_called_once_with()
def test_disconnect_no_handle(self): """Test that we do not attempt to disconnect if there is no device handle. """ spec = AvsFiberSpectrograph() spec.handle = None spec.disconnect() self.patch.return_value.AVS_Deactivate.assert_not_called() self.patch.return_value.AVS_Done.assert_called_once_with()
def test_disconnect_other_exception(self): """Test that disconnect continues if there some other exception raised during disconnect. """ self.patch.return_value.AVS_Deactivate.side_effect = RuntimeError spec = AvsFiberSpectrograph() with self.assertLogs(spec.log, "ERROR"): spec.disconnect() self.patch.return_value.AVS_Deactivate.assert_called_once_with( self.handle) self.patch.return_value.AVS_Done.assert_called_once_with()
async def test_disconnect_active_exposure(self): """Test that disconnecting cancels an active exposure.""" duration = 5 # seconds spec = AvsFiberSpectrograph() t0 = time.monotonic() task = asyncio.create_task(spec.expose(duration)) await asyncio.sleep(0.1) # give the event loop time to start spec.disconnect() with pytest.raises(asyncio.CancelledError): await task t1 = time.monotonic() # cancelling the task should make it end much sooner than the duration assert t1 - t0 < 1 self.patch.return_value.AVS_StopMeasure.assert_called_with(self.handle) self.patch.return_value.AVS_Deactivate.assert_called_once_with( self.handle) self.patch.return_value.AVS_Done.assert_called_once_with() assert spec.handle is None