Skip to content

Commit

Permalink
Fix _compat tests (quantumlib#4926)
Browse files Browse the repository at this point in the history
Fixes some of the out of order test breakage described in quantumlib#4921 

These tests are certainly broken, they are supposed to pass in the function not the result of the function.

Not sure I fully see why this interacts badly with test_deprecated_module tests, but once it hits one of these failing the following parameterized tests fail.
  • Loading branch information
dabacon authored and rht committed May 1, 2023
1 parent 41ddc5c commit 1b493ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ jobs:
- name: Install npm
run: check/npm ci
- name: Lint
run: check/ts-lint
run: check/ts-lint
ts-test:
name: Typescript tests
runs-on: ubuntu-20.04
Expand All @@ -325,7 +325,7 @@ jobs:
- name: Install npm dependencies
run: check/npm ci
- name: Unit tests
run: check/ts-test
run: check/ts-test
- name: End to end tests
run: check/ts-test-e2e
ts-coverage:
Expand Down
19 changes: 13 additions & 6 deletions cirq-core/cirq/_compat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ def _trace_unhandled_exceptions(*args, queue: 'multiprocessing.Queue', func: Cal

def subprocess_context(test_func):
"""Ensures that sys.modules changes in subprocesses won't impact the parent process."""
assert callable(test_func), (
"subprocess_context expects a function. Did you call the function instead of passing "
"it to this method?"
)

import os

ctx = multiprocessing.get_context('spawn' if os.name == 'nt' else 'fork')
Expand All @@ -609,8 +614,8 @@ def isolated_func(*args, **kwargs):
kwargs['func'] = test_func
p = ctx.Process(target=_trace_unhandled_exceptions, args=args, kwargs=kwargs)
p.start()
result = exception.get()
p.join()
result = exception.get()
if result:
# coverage: ignore
ex_type, msg, ex_trace = result
Expand Down Expand Up @@ -667,8 +672,6 @@ def _test_deprecated_module_inner(outdated_method, deprecation_messages):
deadline='v0.20',
count=len(deprecation_messages),
):
import warnings

warnings.simplefilter('always')
outdated_method()

Expand Down Expand Up @@ -785,6 +788,7 @@ def _test_broken_module_1_inner():


def _test_broken_module_2_inner():
warnings.simplefilter('always')
with cirq.testing.assert_deprecated(deadline="v0.20", count=None):
with pytest.raises(
DeprecatedModuleImportError,
Expand All @@ -798,6 +802,9 @@ def _test_broken_module_2_inner():


def _test_broken_module_3_inner():
import cirq.testing._compat_test_data

warnings.simplefilter('always')
with cirq.testing.assert_deprecated(deadline="v0.20", count=None):
with pytest.raises(
DeprecatedModuleImportError,
Expand All @@ -807,15 +814,15 @@ def _test_broken_module_3_inner():


def test_deprecated_module_error_handling_1():
subprocess_context(_test_broken_module_1_inner())
subprocess_context(_test_broken_module_1_inner)()


def test_deprecated_module_error_handling_2():
subprocess_context(_test_broken_module_2_inner())
subprocess_context(_test_broken_module_2_inner)()


def test_deprecated_module_error_handling_3():
subprocess_context(_test_broken_module_3_inner())
subprocess_context(_test_broken_module_3_inner)()


def test_new_module_is_top_level():
Expand Down

0 comments on commit 1b493ad

Please sign in to comment.