async def test_stop_exposure_no_expose_running(self): """Test that stop_exposure does nothing if there is no active `expose` command. """ spec = AvsFiberSpectrograph() spec.stop_exposure() self.patch.return_value.AVS_StopMeasure.assert_not_called()
async def test_stop_exposure(self): """Test that `stop_exposure` ends the active `expose`.""" 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.stop_exposure() 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)
async def test_stop_exposure_during_poll_loop(self): """Test that `stop_exposure` ends the active `expose` when called during the `PollData` loop. """ duration = 0.2 # seconds # repeat "no data" forever, so that `stop` will trigger during polling self.patch.return_value.AVS_PollScan.side_effect = itertools.repeat(0) spec = AvsFiberSpectrograph() task = asyncio.create_task(spec.expose(duration)) await asyncio.sleep(duration + 0.1 ) # wait until we are in the poll loop spec.stop_exposure() with pytest.raises(asyncio.CancelledError): await task self.patch.return_value.AVS_StopMeasure.assert_called_with(self.handle) self.patch.return_value.AVS_PollScan.assert_called_with(self.handle) self.patch.return_value.AVS_GetScopeData.assert_not_called()
async def test_stop_exposure_fails(self): """Test `AVS_StopMeasure` returning an error: the existing exposure task should be cancelled, but `stop_exposure` should also raise.""" duration = 5 # seconds self.patch.return_value.AVS_StopMeasure.return_value = ( AvsReturnCode.ERR_TIMEOUT.value) spec = AvsFiberSpectrograph() t0 = time.monotonic() task = asyncio.create_task(spec.expose(duration)) await asyncio.sleep(0.1) # give the event loop time to start with pytest.raises(AvsReturnError, match="StopMeasure"): spec.stop_exposure() 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)