Skip to content

Commit

Permalink
Explicitly throw TimeoutError when job times out (#4514)
Browse files Browse the repository at this point in the history
Fixes #4507
  • Loading branch information
dabacon committed Sep 22, 2021
1 parent 9f6c359 commit f1964e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cirq-ionq/cirq_ionq/job.py
Expand Up @@ -199,8 +199,10 @@ def results(
if 'failure' in self._job and 'error' in self._job['failure']:
error = self._job['failure']['error']
raise RuntimeError(f'Job failed. Error message: {error}')
if time_waited_seconds >= timeout_seconds:
raise TimeoutError(f'Job timed out after waiting {time_waited_seconds} seconds.')
raise RuntimeError(
f'Job was not completed successful. Instead had status: {self.status()}'
f'Job was not completed successfully. Instead had status: {self.status()}'
)
# IonQ returns results in little endian, Cirq prefers to use big endian, so we convert.
if self.target() == 'qpu':
Expand Down
10 changes: 9 additions & 1 deletion cirq-ionq/cirq_ionq/job_test.py
Expand Up @@ -79,6 +79,14 @@ def test_job_results_failed():
assert job.status() == 'failed'


def test_job_results_failed_no_error_message():
job_dict = {'id': 'my_id', 'status': 'failed', 'failure': {}}
job = ionq.Job(None, job_dict)
with pytest.raises(RuntimeError, match='failed'):
_ = job.results()
assert job.status() == 'failed'


def test_job_results_qpu_endianness():
job_dict = {
'id': 'my_id',
Expand Down Expand Up @@ -124,7 +132,7 @@ def test_job_results_poll_timeout(mock_sleep):
mock_client = mock.MagicMock()
mock_client.get_job.return_value = ready_job
job = ionq.Job(mock_client, ready_job)
with pytest.raises(RuntimeError, match='ready'):
with pytest.raises(TimeoutError, match='seconds'):
_ = job.results(timeout_seconds=1, polling_seconds=0.1)
assert mock_sleep.call_count == 11

Expand Down

0 comments on commit f1964e5

Please sign in to comment.