Example #1
0
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
Example #2
0
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)