def test_job_wait_complete3_success_result():
    """Test wait_for_completion() with successful complete job with a
    result."""
    with requests_mock.mock() as m:
        mock_server_1(m)
        session = Session('fake-host', 'fake-user', 'fake-pw')
        op_method = 'POST'
        op_uri = '/api/foo'
        job = Job(session, JOB_URI, op_method, op_uri)
        exp_op_result = {
            'foo': 'bar',
        }
        m.get(JOB_URI, [
            {
                'text': result_running_callback
            },
            {
                'text': result_complete_callback
            },
        ])
        m.delete(JOB_URI, status_code=204)

        op_result = job.wait_for_completion()

        assert op_result == exp_op_result
def test_job_wait_complete3_timeout():
    """Test wait_for_completion() with timeout."""
    with requests_mock.mock() as m:
        mock_server_1(m)
        session = Session('fake-host', 'fake-user', 'fake-pw')
        op_method = 'POST'
        op_uri = '/api/foo'
        job = Job(session, JOB_URI, op_method, op_uri)
        m.get(JOB_URI, [
            {
                'text': result_running_callback
            },
            {
                'text': result_running_callback
            },
            {
                'text': result_complete_callback
            },
        ])
        m.delete(JOB_URI, status_code=204)

        # Here we provoke a timeout, by setting the timeout to less than
        # the time it would take to return the completed job status.
        # The time it would take is the sum of the following:
        # - 2 * 1 s (1 s is the sleep time in Job.wait_for_completion(),
        #   and this happens before each repeated status retrieval)
        # - 3 * 1 s (1 s is the sleep time in result_*_callback() in this
        #   module, and this happens in each mocked status retrieval)
        # Because status completion is given priority over achieving the
        # timeout duration, the timeout value needed to provoke the
        # timeout exception needs to be shorter by the last status
        # retrieval (the one that completes the job), so 3 s is the
        # boundary for the timeout value.
        operation_timeout = 2.9
        try:
            start_time = time.time()
            job.wait_for_completion(operation_timeout=operation_timeout)
            duration = time.time() - start_time
            raise AssertionError(
                "No OperationTimeout raised. Actual duration: %s s, "
                "timeout: %s s" % (duration, operation_timeout))
        except OperationTimeout as exc:
            msg = exc.args[0]
            assert msg.startswith("Waiting for completion of job")
    def test_wait_complete1_success_result(self):
        """Test wait_for_completion() with successful complete job with a
        result."""
        with requests_mock.mock() as m:
            self.mock_server_1(m)
            session = Session('fake-host', 'fake-user', 'fake-pw')
            op_method = 'POST'
            op_uri = '/api/foo'
            job = Job(session, self.job_uri, op_method, op_uri)
            exp_op_result = {
                'foo': 'bar',
            }
            query_job_status_result = {
                'status': 'complete',
                'job-status-code': 200,
                # 'job-reason-code' omitted because HTTP status good
                'job-results': exp_op_result,
            }
            m.get(self.job_uri, json=query_job_status_result)
            m.delete(self.job_uri, status_code=204)

            op_result = job.wait_for_completion()

            assert op_result == exp_op_result