Skip to content

Commit

Permalink
[FIX] Intermittent Tests Start Error With No Information (#157)
Browse files Browse the repository at this point in the history
* Fixing intermittent problem with Python's Base Manager

Creating the manager now only once in the SDK Container Init

* Refactoring and updating the unit tests with related mocks
  • Loading branch information
antonio-amjr authored Oct 23, 2024
1 parent 3cf1ace commit 4f87387
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from asyncio import sleep
from enum import IntEnum
from inspect import iscoroutinefunction
from multiprocessing.managers import BaseManager
from pathlib import Path
from socket import SocketIO
from typing import Any, Optional, Type, TypeVar
Expand All @@ -38,10 +37,7 @@
from ...sdk_container import SDKContainer
from ...utils import prompt_for_commissioning_mode
from .python_test_models import PythonTest, PythonTestType
from .python_testing_hooks_proxy import (
SDKPythonTestResultBase,
SDKPythonTestRunnerHooks,
)
from .python_testing_hooks_proxy import SDKPythonTestResultBase
from .utils import (
EXECUTABLE,
RUNNER_CLASS_PATH,
Expand Down Expand Up @@ -261,9 +257,7 @@ async def execute(self) -> None:
try:
logger.info("Running Python Test: " + self.python_test.name)

BaseManager.register("TestRunnerHooks", SDKPythonTestRunnerHooks)
manager = BaseManager(address=("0.0.0.0", 50000), authkey=b"abc")
manager.start()
manager = self.sdk_container.manager
test_runner_hooks = manager.TestRunnerHooks() # type: ignore

if not self.python_test.path:
Expand Down
13 changes: 13 additions & 0 deletions test_collections/matter/sdk_tests/support/sdk_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
from __future__ import annotations

from multiprocessing.managers import BaseManager
from pathlib import Path
from typing import Optional, Union

Expand All @@ -29,6 +30,7 @@

from .exec_run_in_container import ExecResultExtended, exec_run_in_container
from .pics import set_pics_command
from .python_testing.models.python_testing_hooks_proxy import SDKPythonTestRunnerHooks

# Trace mount
LOCAL_LOGS_PATH = Path("/var/tmp")
Expand Down Expand Up @@ -136,6 +138,7 @@ def __init__(

self.__pics_file_created = False
self.logger = logger
self.manager: BaseManager | None = None

@property
def pics_file_created(self) -> bool:
Expand All @@ -150,6 +153,13 @@ def __destroy_existing_container(self) -> None:
)
container_manager.destroy(existing_container)

def __create_manager(self) -> BaseManager:
BaseManager.register("TestRunnerHooks", SDKPythonTestRunnerHooks)
manager = BaseManager(address=("0.0.0.0", 50000), authkey=b"abc")
manager.start()

return manager

def is_running(self) -> bool:
if self.__container is None:
return False
Expand All @@ -176,6 +186,9 @@ async def start(self) -> None:
self.image_tag, self.run_parameters
)

# Create the BaseManager for multiprocess data share
self.manager = self.__create_manager()

self.logger.info(
f"{self.container_name} container started"
f" with configuration: {self.run_parameters}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ async def test_destroy_container_running() -> None:

with mock.patch.object(
target=sdk_container, attribute="is_running", return_value=False
), mock.patch.object(
target=sdk_container,
attribute="_SDKContainer__create_manager",
return_value=None,
), mock.patch.object(
target=container_manager, attribute="get_container", return_value=None
), mock.patch.object(
Expand Down Expand Up @@ -112,6 +116,10 @@ async def test_destroy_container_once() -> None:

with mock.patch.object(
target=sdk_container, attribute="is_running", return_value=False
), mock.patch.object(
target=sdk_container,
attribute="_SDKContainer__create_manager",
return_value=None,
), mock.patch.object(
target=container_manager, attribute="get_container", return_value=None
), mock.patch.object(
Expand Down Expand Up @@ -147,6 +155,10 @@ async def test_send_command_default_prefix() -> None:

with mock.patch.object(
target=sdk_container, attribute="is_running", return_value=False
), mock.patch.object(
target=sdk_container,
attribute="_SDKContainer__create_manager",
return_value=None,
), mock.patch.object(
target=container_manager, attribute="get_container", return_value=None
), mock.patch.object(
Expand Down Expand Up @@ -188,6 +200,10 @@ async def test_send_command_custom_prefix() -> None:

with mock.patch.object(
target=sdk_container, attribute="is_running", return_value=False
), mock.patch.object(
target=sdk_container,
attribute="_SDKContainer__create_manager",
return_value=None,
), mock.patch.object(
target=container_manager, attribute="get_container", return_value=None
), mock.patch.object(
Expand Down

0 comments on commit 4f87387

Please sign in to comment.