Skip to content
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
38 changes: 36 additions & 2 deletions pyfrc/mains/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ def __init__(self, parser=None):
default=-1,
help="Maximum isolated robot processes (default: max CPUs - 1)",
)
parser.add_argument(
"--init-timeout",
type=float,
default=None,
help="Seconds to wait for robot to start (can be set in `tool.robotpy.pyfrc.init_timeout` also)",
)

def run(
self,
Expand All @@ -87,6 +93,7 @@ def run(
verbose: bool,
pytest_args: typing.List[str],
jobs: int,
init_timeout: typing.Optional[float],
):
if isolated is None:
pyproject_path = project_path / "pyproject.toml"
Expand All @@ -106,6 +113,26 @@ def run(

isolated = v

try:
v = d["tool"]["robotpy"]["pyfrc"]["init_timeout"]
except KeyError:
pass
else:
if not isinstance(v, (int, float)):
raise ValueError(
f"tool.robotpy.pyfrc.init_timeout must be a number (got {v})"
)
elif not (v > 0):
raise ValueError(
f"tool.robotpy.pyfrc.init_timeout must be a positive number (got {v})"
)

if init_timeout is None:
init_timeout = float(v)

if init_timeout is None:
init_timeout = 2.0

if isolated is None:
isolated = True

Expand All @@ -120,6 +147,7 @@ def run(
verbose,
pytest_args,
jobs,
init_timeout,
)
except _TryAgain:
return self._run_test(
Expand All @@ -132,6 +160,7 @@ def run(
verbose,
pytest_args,
jobs,
init_timeout,
)

def _run_test(
Expand All @@ -145,6 +174,7 @@ def _run_test(
verbose: bool,
pytest_args: typing.List[str],
jobs: int,
init_timeout: float,
):
# find test directory, change current directory so pytest can find the tests
# -> assume that tests reside in tests or ../tests
Expand Down Expand Up @@ -180,14 +210,18 @@ def _run_test(
pytest_args,
plugins=[
pytest_isolated_tests_plugin.IsolatedTestsPlugin(
robot_class, main_file, builtin, verbose, jobs
robot_class, main_file, builtin, verbose, jobs, init_timeout
)
],
)
else:
retv = pytest.main(
pytest_args,
plugins=[pytest_plugin.PyFrcPlugin(robot_class, main_file, False)],
plugins=[
pytest_plugin.PyFrcPlugin(
robot_class, main_file, False, init_timeout
)
],
)
finally:
os.chdir(curdir)
Expand Down
10 changes: 8 additions & 2 deletions pyfrc/test_support/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ class TestController:
Use this object to control the robot's state during tests
"""

def __init__(self, reraise, robot: wpilib.RobotBase):
def __init__(self, reraise, robot: wpilib.RobotBase, init_timeout: float = 2.0):
self._reraise = reraise

self._thread: typing.Optional[threading.Thread] = None
self._robot = robot
self._init_timeout = init_timeout

self._cond = threading.Condition()
self._robot_started = False
Expand Down Expand Up @@ -72,7 +73,12 @@ def run_robot(self):

# If your robotInit is taking more than 2 seconds in simulation, you're
# probably doing something wrong... but if not, please report a bug!
assert self._cond.wait_for(lambda: self._robot_initialized, timeout=2)
#
# To avoid this altogether, `--init-timeout` can be passed to `robotpy test`
# or you can add `tools.robotpy.pyfrc.init_timeout` instead
assert self._cond.wait_for(
lambda: self._robot_initialized, timeout=self._init_timeout
)

try:
# in this block you should tell the sim to do sim things
Expand Down
14 changes: 12 additions & 2 deletions pyfrc/test_support/pytest_isolated_tests_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ def pytest_runtest_logreport(self, report: pytest.TestReport):


def _run_test(
item_nodeid, config_args, robot_class, robot_file, verbose, pipe, root_path
item_nodeid,
config_args,
robot_class,
robot_file,
verbose,
pipe,
root_path,
init_timeout,
):
"""This function runs in a subprocess"""
logging.root.addHandler(logging.NullHandler())
Expand All @@ -143,7 +150,7 @@ def _run_test(

# keep the plugins around because it has a reference to the robot
# and we don't want it to die and deadlock
plugin = PyFrcPlugin(robot_class, robot_file, True)
plugin = PyFrcPlugin(robot_class, robot_file, True, init_timeout)
worker_plugin = WorkerPlugin(pipe)

ec = pytest.main(
Expand Down Expand Up @@ -195,11 +202,13 @@ def __init__(
builtin_tests: bool,
verbose: bool,
parallelism: int,
init_timeout: float,
):
self._robot_class = robot_class
self._robot_file = robot_file
self._builtin_tests = builtin_tests
self._verbose = verbose
self._init_timeout = init_timeout

if parallelism < 1:
try:
Expand Down Expand Up @@ -287,6 +296,7 @@ def _start_isolated_test(self, item: pytest.Function) -> IsolatedTestJob:
self._verbose,
cconn,
self._config.rootpath,
self._init_timeout,
),
)
process.start()
Expand Down
5 changes: 4 additions & 1 deletion pyfrc/test_support/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
robot_class: Type[wpilib.RobotBase],
robot_file: pathlib.Path,
isolated: bool,
init_timeout: float,
):
self.isolated = isolated

Expand All @@ -61,6 +62,8 @@ def robotInit(self):
self._robot_file = robot_file
self._robot_class = TestRobot

self._init_timeout = init_timeout

self._physics = physics

if physics:
Expand Down Expand Up @@ -164,7 +167,7 @@ def control(self, reraise, robot: wpilib.RobotBase) -> TestController:
"""
A pytest fixture that provides control over your robot
"""
return TestController(reraise, robot)
return TestController(reraise, robot, self._init_timeout)

@pytest.fixture()
def robot_file(self) -> pathlib.Path:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_pytest_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _configure_pyfrc_plugin(pytester, robot_class="DummyRobot"):

def pytest_configure(config):
robot_file = pathlib.Path(__file__).resolve()
config.pluginmanager.register(PyFrcPlugin({robot_class}, robot_file, False))
config.pluginmanager.register(PyFrcPlugin({robot_class}, robot_file, False, 2.0))
""")


Expand All @@ -92,7 +92,7 @@ def pytest_configure(config):
return
robot_file = pathlib.Path(__file__).resolve()
config.pluginmanager.register(
IsolatedTestsPlugin({robot_class}, robot_file, False, False, {parallelism})
IsolatedTestsPlugin({robot_class}, robot_file, False, False, {parallelism}, 2.0)
)
""")

Expand Down