Skip to content

Commit

Permalink
Readd run override (facebook#691)
Browse files Browse the repository at this point in the history
Summary:
in order to support python 3.7, which has no `unittest.TestCase._callSetUp()` or `unittest.TestCase._callTearDown()` method, we add and remove alarms in the override of `unittest.TestCase.run()`

Pull Request resolved: facebook#691

Test Plan:
I tested this out in buck, with both an artificial timeout and an error and it gave normal output
```
test_fetch_mock_adhoc_fetcher: NoDataError: No metrics have availability overlapping with date range (2020-07-02, 2020-07-05).

Failures:

  1) ax.fb.utils.deltoid.tests.test_deltoid_apis.Deltoid3APIsTest: test_fetch_mock_adhoc_fetcher
    1) NoDataError: No metrics have availability overlapping with date range (2020-07-02, 2020-07-05).
      File "ax/fb/utils/deltoid/tests/test_deltoid_apis.py", line 785, in test_fetch_mock_adhoc_fetcher
        fetcher = MockDeltoidAdhocFetcher(trial=self.trial, metrics=self.metrics)
      File "ax/fb/utils/deltoid/mock.py", line 54, in __init__
        super().__init__(
      File "ax/fb/utils/deltoid/deltoid_apis.py", line 259, in __init__
        raise NoDataError(
Imports took: 41.1s! Profile with --import-profiler.           --_ |""---__
Executed 1 example in 41.7s:                                |'.|  ||  .    """|
  Successful: 0                                             | ||  || /|\""-.  |
  Failed: 1                                                 | ||  ||  |    |  |
  Skipped: 0                                                | ||  ||  |   \|/ |
  Not executed: 38                                          |."|  ||  --"" '__|
https://testslide.readthedocs.io/                              --" |__---"""
Failure: process exited with rc=1. (43.0s)
```
```
  test_fetch_mock_adhoc_fetcher: Exception: Test timed out at 1 seconds

Failures:

  1) ax.fb.utils.deltoid.tests.test_deltoid_apis.Deltoid3APIsTest: test_fetch_mock_adhoc_fetcher
    1) Exception: Test timed out at 1 seconds
      File "ax/fb/utils/deltoid/tests/test_deltoid_apis.py", line 786, in test_fetch_mock_adhoc_fetcher
        sleep(2)
      File "ax/utils/common/testutils.py", line 225, in signal_handler
        raise Exception(f"Test timed out at {self.MAX_TEST_SECONDS} seconds")
Imports took: 41.2s! Profile with --import-profiler.           --_ |""---__
Executed 1 example in 42.8s:                                |'.|  ||  .    """|
  Successful: 0                                             | ||  || /|\""-.  |
  Failed: 1                                                 | ||  ||  |    |  |
  Skipped: 0                                                | ||  ||  |   \|/ |
  Not executed: 38                                          |."|  ||  --"" '__|
https://testslide.readthedocs.io/                              --" |__---"""
Failure: process exited with rc=1. (44.6s)
```

Also tested both failure varieties in pytest and they look normal in 3.7 and 3.8, but those outputs are much longer.

Reviewed By: lena-kashtelyan

Differential Revision: D30843594

Pulled By: danielcohenlive

fbshipit-source-id: 5b9fdfb06e74a146eda222d03a6cfe3647bcde68
  • Loading branch information
Daniel Cohen authored and facebook-github-bot committed Sep 14, 2021
1 parent 730ffd3 commit fb5a764
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions ax/utils/common/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,18 @@ def signal_handler(signum: int, frame: Optional[FrameType]) -> None:

super().__init__(methodName=methodName)
signal.signal(signal.SIGALRM, signal_handler)
self.setUp_called = False

def _callSetUp(self) -> None:
def run(
self, result: Optional[unittest.result.TestResult] = ...
) -> Optional[unittest.result.TestResult]:
# Arrange for a SIGALRM signal to be delivered to the calling process
# in specified number of seconds.
signal.alarm(self.MAX_TEST_SECONDS)
self.setUp_called = True
super()._callSetUp() # pyre-ignore[16]... it does

def _callTearDown(self) -> None:
# When seconds argument to alarm is zero, any pending alarm is canceled.
signal.alarm(0)
super()._callTearDown() # pyre-ignore[16]... it does
try:
result = super().run(result)
finally:
signal.alarm(0)
return result

def assertEqual(
self,
Expand Down

0 comments on commit fb5a764

Please sign in to comment.