Skip to content

Commit

Permalink
Fixed feature request eclipse-volttron#49
Browse files Browse the repository at this point in the history
  • Loading branch information
craig8 committed Oct 24, 2024
1 parent 9a00b0b commit 8db0712
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
36 changes: 25 additions & 11 deletions src/volttrontesting/platformwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,21 @@ def create(**kwargs) -> InstallAgentOptions:
DefaultAgentInstallOptions = InstallAgentOptions()

class PlatformWrapper:
def __init__(self, options: ServerOptions, project_toml_file: Path | str, start_platform: bool = True,
skip_cleanup: bool = False, environment_updates: dict[str, str] = None):
def __init__(self, options: ServerOptions, project_toml_file: Path | str, start_platform: bool = True,
skip_cleanup: bool = False, environment_updates: dict[str, str] = None, enable_sys_queue: bool = False):
"""
Initializes a new VOLTTRON instance
Creates a temporary VOLTTRON_HOME directory with a packaged directory for agents that are built.
:options: ServerOptions - The environment that the platform will run under.
:project_toml_file: Path - The pyproject.toml file to use as a base for this platform wrapper and it's environment.
:start_platform: bool - Should the platform be started before returning from this constructor
:options: The environment that the platform will run under.
:project_toml_file: The pyproject.toml file to use as a base for this platform wrapper and it's environment.
:start_platform: Should the platform be started before returning from this constructor
:skip_cleanup: Should the environment not be cleaned up (even when cleanup method is called)
:environment_updates: A dictionary of environmental variables to use during execution. Will be merged with
existing variables so these will overwrite if there is a collision.
:enable_sys_queue: Should stdout be intercepted to be analysed by calls to pop_stdout_queue method
"""

# We need to use the toml file as a template for executing the proper environment of the
Expand Down Expand Up @@ -291,6 +296,7 @@ def __init__(self, options: ServerOptions, project_toml_file: Path | str, start
self._dynamic_agent: Agent | None = None
self._dynamic_agent_task: Greenlet | None = None

self._enable_sys_queue = enable_sys_queue
self._stdout_queue = Queue()
self._stdout_thread: threading.Thread | None = None

Expand Down Expand Up @@ -319,12 +325,18 @@ def dynamic_agent(self) -> Agent:
return self._dynamic_agent

def pop_stdout_queue(self) -> str:
if not self._enable_sys_queue:
raise ValueError(f"SysQueue not enabled, pass True to PlatformWrapper constructor for enable_sys_queue "
f"argument.")
try:
yield self._stdout_queue.get_nowait()
except queue.Empty:
raise StopIteration()

def clear_stdout_queue(self):
if not self._enable_sys_queue:
raise ValueError(f"SysQueue not enabled, pass True to PlatformWrapper constructor for enable_sys_queue "
f"argument.")
try:
while True:
self._stdout_queue.get_nowait()
Expand Down Expand Up @@ -698,12 +710,13 @@ def capture_stdout(queue: Queue, process):
text=True)
time.sleep(1)

# Set up a background thread to gather queue
self._stdout_thread = threading.Thread(target=capture_stdout,
args=(self._stdout_queue, self._platform_process),
daemon=True)
self._stdout_thread.start()
gevent.sleep(0.1)
if self._enable_sys_queue:
# Set up a background thread to gather queue
self._stdout_thread = threading.Thread(target=capture_stdout,
args=(self._stdout_queue, self._platform_process),
daemon=True)
self._stdout_thread.start()
gevent.sleep(0.1)

# A None value means that the process is still running.
# A negative means that the process exited with an error.
Expand Down Expand Up @@ -978,6 +991,7 @@ def install_agent(self, agent_wheel: PathStr = None,
This function will return with a uuid of the installed agent.
:param start:
:param agent_wheel:
:param agent_dir:
:param install_options: The options available for installing an agent on the platform.
Expand Down
2 changes: 1 addition & 1 deletion src/volttrontesting/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from random import random

import gevent
import mock
from unittest import mock
import pytest

from volttron.utils import format_timestamp
Expand Down
30 changes: 30 additions & 0 deletions tests/test_platformwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@
from pathlib import Path


def test_can_enable_sys_queue(get_pyproject_toml):
options = create_server_options()

p = PlatformWrapper(options=options, project_toml_file=get_pyproject_toml, enable_sys_queue=True)

for data in p.pop_stdout_queue():
assert data
p.shutdown_platform()

p.cleanup()


def test_value_error_raised_sys_queue(get_pyproject_toml):
options = create_server_options()

p = PlatformWrapper(options=options, project_toml_file=get_pyproject_toml)

with pytest.raises(ValueError):
for data in p.pop_stdout_queue():
pass

with pytest.raises(ValueError):
for data in p.clear_stdout_queue():
pass

p.shutdown_platform()

p.cleanup()


def test_install_library(get_pyproject_toml):
options = create_server_options()

Expand Down

0 comments on commit 8db0712

Please sign in to comment.