Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation for the default repeat limit, add constant for retrying "forever". #1180

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions openhtf/core/phase_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import logging
import os.path
import pdb
import sys
from typing import Any, Callable, Dict, Iterator, List, Optional, Sequence, Set, Text, TYPE_CHECKING, Type, Union

import attr
Expand All @@ -43,6 +44,10 @@
if TYPE_CHECKING:
from openhtf.core import test_state # pylint: disable=g-import-not-at-top


DEFAULT_REPEAT_LIMIT = 3
MAX_REPEAT_LIMIT = sys.maxsize

_LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -111,8 +116,9 @@ class PhaseOptions(object):
repeat_on_measurement_fail: If true, force phase with failed
measurements to repeat up to repeat_limit times.
repeat_on_timeout: If consider repeat on phase timeout, default is No.
repeat_limit: Maximum number of repeats. None indicates a phase will be
repeated infinitely as long as PhaseResult.REPEAT is returned.
repeat_limit: Maximum number of repeats. DEFAULT_REPEAT_LIMIT applies if
this is set to None. MAX_REPEAT_LIMIT can be used to repeat the phase
virtually forever, as long as PhaseResult.REPEAT is returned.
run_under_pdb: If True, run the phase under the Python Debugger (pdb). When
setting this option, increase the phase timeout as well because the
timeout will still apply when under the debugger.
Expand Down
5 changes: 3 additions & 2 deletions openhtf/core/phase_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
from openhtf.core import test_state as htf_test_state # pylint: disable=g-import-not-at-top

DEFAULT_PHASE_TIMEOUT_S = 3 * 60
DEFAULT_RETRIES = 3
_JOIN_TRY_INTERVAL_SECONDS = 3

ARG_PARSER = argv.module_parser()
Expand Down Expand Up @@ -279,7 +278,9 @@ def execute_phase(
requested and successfully ran for this phase execution.
"""
repeat_count = 1
repeat_limit = (phase.options.repeat_limit or DEFAULT_RETRIES)
repeat_limit = (
phase.options.repeat_limit or phase_descriptor.DEFAULT_REPEAT_LIMIT
)
while not self._stopping.is_set():
is_last_repeat = repeat_count >= repeat_limit
phase_execution_outcome, profile_stats = self._execute_phase_once(
Expand Down
56 changes: 56 additions & 0 deletions test/core/phase_executor_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Unit tests for phase_executor module."""

import unittest

import openhtf
from openhtf.core import phase_descriptor


class PhaseExecutorTest(unittest.TestCase):

def _run_repeating_phase(self, phase_options, expected_call_count):
call_count = 0

@phase_options
def repeating_phase():
nonlocal call_count
call_count += 1
if call_count < phase_descriptor.DEFAULT_REPEAT_LIMIT + 1:
return phase_descriptor.PhaseResult.REPEAT
return phase_descriptor.PhaseResult.STOP

test = openhtf.Test(repeating_phase)
test.execute()
self.assertEqual(call_count, expected_call_count)

def test_execute_phase_with_repeat_limit_unspecified_uses_default_limit(self):
self._run_repeating_phase(
openhtf.PhaseOptions(),
expected_call_count=phase_descriptor.DEFAULT_REPEAT_LIMIT,
)

def test_execute_phase_with_repeat_limit_none_uses_default_limit(self):
self._run_repeating_phase(
openhtf.PhaseOptions(repeat_limit=None),
expected_call_count=phase_descriptor.DEFAULT_REPEAT_LIMIT,
)

def test_execute_phase_with_repeat_limit_max_exceeds_default_limit(self):
self._run_repeating_phase(
openhtf.PhaseOptions(repeat_limit=phase_descriptor.MAX_REPEAT_LIMIT),
expected_call_count=phase_descriptor.DEFAULT_REPEAT_LIMIT + 1,
)