def test_cancel_plugin(daq_robot): """Test that cancelling a request actually stops it.""" def run_forever(request, store_helper: DataAcquisitionStoreHelper): while True: store_helper.cancel_check() time.sleep(0.01) service = DataAcquisitionPluginService(daq_robot, single_capability, run_forever) context = None response = service.AcquirePluginData(make_single_request('action 1'), context) assert response.request_id > 0 feedback_request = data_acquisition_pb2.GetStatusRequest( request_id=response.request_id) feedback = service.GetStatus(feedback_request, context) assert feedback.status == feedback.STATUS_ACQUIRING cancel_response = service.CancelAcquisition( data_acquisition_pb2.CancelAcquisitionRequest( request_id=response.request_id), context) assert cancel_response.status == cancel_response.STATUS_OK feedback = service.GetStatus(feedback_request, context) assert feedback.status in (feedback.STATUS_CANCEL_IN_PROGRESS, feedback.STATUS_ACQUISITION_CANCELLED) service.executor.shutdown() feedback = service.GetStatus(feedback_request, context) assert feedback.status == feedback.STATUS_ACQUISITION_CANCELLED
def cancel_acquisition_async(self, request_id, **kwargs): """Async version of the cancel_acquisition() RPC.""" request = data_acquisition.CancelAcquisitionRequest( request_id=request_id) return self.call_async(self._stub.CancelAcquisition, request, error_from_response=_cancel_acquisition_error, **kwargs)
def test_plugin_stages(daq_robot): """Test that we can transition through the stages acquiring->saving->complete""" # Used to coordinate between the service and client rather than relying on sleeps. go = threading.Event() done = threading.Event() def collect_data(request, store_helper: DataAcquisitionStoreHelper): while not go.wait(timeout=1): print('Waiting on go 1') store_helper.cancel_check() print('Go 1 Finished.') go.clear() store_helper.state.set_status( data_acquisition_pb2.GetStatusResponse.STATUS_SAVING) done.set() while not go.wait(timeout=1): print('Waiting on go 2') store_helper.cancel_check() print('Go 2 Finished.') store_helper.wait_for_stores_complete() service = DataAcquisitionPluginService(daq_robot, single_capability, collect_data) context = None response = service.AcquirePluginData(make_single_request('action 1'), context) assert response.request_id > 0 try: feedback_request = data_acquisition_pb2.GetStatusRequest( request_id=response.request_id) feedback = service.GetStatus(feedback_request, context) assert feedback.status == feedback.STATUS_ACQUIRING go.set() assert done.wait(timeout=1) done.clear() feedback = service.GetStatus(feedback_request, context) assert feedback.status == feedback.STATUS_SAVING go.set() service.executor.shutdown(wait=True) feedback = service.GetStatus(feedback_request, context) assert feedback.status == feedback.STATUS_COMPLETE finally: # If anything fails, make sure that we kill the background thread. service.CancelAcquisition( data_acquisition_pb2.CancelAcquisitionRequest( request_id=response.request_id), context) service.executor.shutdown(wait=False)
def cancel_acquisition(self, request_id, **kwargs): """Cancel a data acquisition based on the request id. Args: request_id (int): The request id associated with an AcquireData request. Raises: RpcError: Problem communicating with the robot. CancellationFailedError: The data acquisitions associated with the request id were unable to be cancelled. RequestIdDoesNotExistError: The request id provided is incorrect. Returns: If the RPC is successful, then it will return the full status response, which includes the status as well as other information about any possible errors. """ request = data_acquisition.CancelAcquisitionRequest(request_id=request_id) return self.call(self._stub.CancelAcquisition, request, error_from_response=_cancel_acquisition_error, **kwargs)