Skip to content

Commit

Permalink
Enable test_websocket_integration (#310)
Browse files Browse the repository at this point in the history
* enable test_websocket_integration

* update makefile

Co-authored-by: Daniel Kaulen <dkaulen@de.ibm.com>
  • Loading branch information
kt474 and daka1510 committed Apr 5, 2022
1 parent a3e8311 commit 3e4a6b7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ integration-test:
python -m unittest -v test/integration/test_backend.py test/integration/test_account_client.py test/integration/test_filter_backends.py \
test/integration/test_serialization.py test/integration/test_ibm_job_attributes.py test/integration/test_basic_server_paths.py \
test/integration/test_ibm_integration.py test/integration/test_ibm_job.py test/integration/test_ibm_qasm_simulator.py \
test/integration/test_proxies.py test/integration/test_composite_job.py
test/integration/test_proxies.py test/integration/test_websocket_integration.py test/integration/test_composite_job.py

e2e-test:
python -m unittest discover --verbose --top-level-directory . --start-directory test/e2e
Expand Down
65 changes: 37 additions & 28 deletions test/integration/test_websocket_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# pylint: disable-all
# type: ignore
# TODO: Reenable and fix integration tests.

# This code is part of Qiskit.
#
# (C) Copyright IBM 2021.
Expand All @@ -25,9 +21,13 @@
from qiskit.providers.jobstatus import JobStatus
from qiskit.test import slow_test
from qiskit.test.reference_circuits import ReferenceCircuits

from qiskit_ibm_provider import IBMBackend
from qiskit_ibm_provider.hub_group_project import from_instance_format
from qiskit_ibm_provider.api.clients import AccountClient, websocket
from ..decorators import requires_provider, requires_device
from ..decorators import (
IntegrationTestDependencies,
integration_test_setup_with_backend,
)
from ..ibm_test_case import IBMTestCase
from ..proxy_server import MockProxyServer, use_proxies
from ..utils import most_busy_backend, cancel_job
Expand All @@ -37,19 +37,20 @@ class TestWebsocketIntegration(IBMTestCase):
"""Websocket integration tests."""

@classmethod
@requires_provider
def setUpClass(cls, provider, hub, group, project):
@integration_test_setup_with_backend(simulator=False)
def setUpClass(
cls, backend: IBMBackend, dependencies: IntegrationTestDependencies
) -> None:
"""Initial class level setup."""
# pylint: disable=arguments-differ
super().setUpClass()
cls.provider = provider
cls.hub = hub
cls.group = group
cls.project = project
cls.sim_backend = provider.get_backend(
"ibmq_qasm_simulator", hub=cls.hub, group=cls.group, project=cls.project
cls.dependencies = dependencies
hub, group, project = from_instance_format(dependencies.instance)
cls.sim_backend = dependencies.provider.get_backend(
"ibmq_qasm_simulator", hub=hub, group=group, project=project
)
cls.bell = transpile(ReferenceCircuits.bell(), cls.sim_backend)
cls.real_device_backend = backend

def setUp(self):
"""Initial test setup."""
Expand All @@ -75,16 +76,19 @@ def test_websockets_simulator(self):
job = self.sim_backend.run(self.bell, shots=1)

# Manually disable the non-websocket polling.
job._api_client._job_final_status_polling = self._job_final_status_polling
job._api_client.account_api._job_final_status_polling = (
self._job_final_status_polling
)
result = job.result()

self.assertEqual(result.status, "COMPLETED")

@slow_test
@requires_device
def test_websockets_device(self, backend):
def test_websockets_device(self):
"""Test checking status of a job via websockets for a device."""
job = backend.run(transpile(ReferenceCircuits.bell(), backend), shots=1)
job = self.real_device_backend.run(
transpile(ReferenceCircuits.bell(), self.real_device_backend), shots=1
)

# Manually disable the non-websocket polling.
job._api_client._job_final_status_polling = self._job_final_status_polling
Expand All @@ -100,7 +104,9 @@ def test_websockets_job_final_state(self):
job._wait_for_completion()

# Manually disable the non-websocket polling.
job._api_client._job_final_status_polling = self._job_final_status_polling
job._api_client.account_api._job_final_status_polling = (
self._job_final_status_polling
)

# Pretend we haven't seen the final status
job._status = JobStatus.RUNNING
Expand All @@ -112,17 +118,17 @@ def test_websockets_retry_bad_url(self):
"""Test http retry after websocket error due to an invalid URL."""

job = self.sim_backend.run(self.bell)
saved_websocket_url = job._api_client._credentials.websockets_url
saved_websocket_url = job._api_client._params.url

try:
# Use fake websocket address.
job._api_client._credentials.websockets_url = "wss://wss.localhost"
job._api_client._params.url = "wss://wss.localhost"

# _wait_for_completion() should retry with http successfully
# after getting websockets error.
job._wait_for_completion()
finally:
job._api_client._credentials.websockets_url = saved_websocket_url
job._api_client._params.url = saved_websocket_url

self.assertIs(job._status, JobStatus.DONE)

Expand Down Expand Up @@ -171,9 +177,7 @@ def _job_status_side_effect(*args, **kwargs):

def test_websockets_timeout(self):
"""Test timeout checking status of a job via websockets."""
backend = most_busy_backend(
self.provider, hub=self.hub, group=self.group, project=self.project
)
backend = most_busy_backend(self.dependencies.provider)
job = backend.run(
transpile(ReferenceCircuits.bell(), backend),
shots=backend.configuration().max_shots,
Expand Down Expand Up @@ -228,9 +232,12 @@ def test_websocket_proxy(self):
job = self.sim_backend.run(self.bell, shots=1)

# Manually disable the non-websocket polling.
job._api_client._job_final_status_polling = self._job_final_status_polling
job._api_client.account_api._job_final_status_polling = (
self._job_final_status_polling
)
with use_proxies(
self.provider.backend._default_hgp, MockProxyServer.VALID_PROXIES
self.dependencies.provider.backend._default_hgp,
MockProxyServer.VALID_PROXIES,
):
result = job.result()

Expand All @@ -246,7 +253,9 @@ def test_websocket_proxy_invalid_port(self):
MockProxyServer.PROXY_IP_ADDRESS, MockProxyServer.INVALID_PROXY_PORT
)
}
with use_proxies(self.provider.backend._default_hgp, invalid_proxy):
with use_proxies(
self.dependencies.provider.backend._default_hgp, invalid_proxy
):
with self.assertLogs("qiskit_ibm_provider", "INFO") as log_cm:
job.wait_for_final_state()

Expand Down
5 changes: 3 additions & 2 deletions test/proxy_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def stop(self):
@contextmanager
def use_proxies(hgp, proxies):
"""Context manager to set and restore proxies setting."""

try:
hgp.credentials.proxies = {"urls": proxies}
hgp._api_client.proxies = {"urls": proxies}
yield
finally:
hgp.credentials.proxies = None
hgp._api_client.proxies = None

0 comments on commit 3e4a6b7

Please sign in to comment.