Commit cb81e1a2 authored by Wout De Nolf's avatar Wout De Nolf
Browse files

writer tests: std capturing optional

parent 6f8176c0
......@@ -80,6 +80,21 @@ def nexus_writer_config(nexus_writer_session_policy, scan_tmpdir):
yield info
@pytest.fixture
def nexus_writer_config_capture(nexus_writer_session_policy, scan_tmpdir):
"""Writer session with a Nexus writer
"""
with nexus_writer(
nexus_writer_session_policy,
scan_tmpdir,
config=True,
alt=False,
policy=True,
capture=True,
) as info:
yield info
@pytest.fixture
def nexus_writer_limited_disk_space(nexus_writer_session_policy, scan_tmpdir):
"""Like nexus_writer_config but require more disk space
......@@ -120,7 +135,13 @@ def nexus_writer_config_alt(nexus_writer_session_policy, scan_tmpdir):
@contextmanager
def nexus_writer(
session, tmpdir, config=True, alt=False, policy=True, required_disk_space=None
session,
tmpdir,
config=True,
alt=False,
policy=True,
capture=False,
required_disk_space=None,
):
"""Nexus writer for this session
......@@ -129,6 +150,7 @@ def nexus_writer(
:param bool policy:
:param bool config:
:param bool alt:
:param bool capture:
:param num required_disk_space:
:returns dict:
"""
......@@ -142,7 +164,7 @@ def nexus_writer(
}
prepare_objects(**info)
prepare_scan_saving(**info)
with writer_tango(**info) as writer:
with writer_tango(capture=capture, **info) as writer:
info["writer"] = writer
yield info
......@@ -206,6 +228,7 @@ def writer_tango(
tmpdir=None,
config=True,
alt=False,
capture=False,
required_disk_space=None,
**kwargs,
):
......@@ -217,6 +240,7 @@ def writer_tango(
:param callable wait_for_fixture:
:param bool config:
:param bool alt:
:param bool capture:
:param num required_disk_space:
:param kwargs: ignored
:returns PopenGreenlet:
......@@ -238,9 +262,7 @@ def writer_tango(
db.put_device_property(device_name, properties)
exception = None
for i in range(3):
with nxw_test_utils.popencontext(
cliargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env
) as greenlet:
with nxw_test_utils.popencontext(cliargs, env=env, capture=capture) as greenlet:
try:
dev_proxy = wait_tango_device(
device_fqdn=device_fqdn, state=DevState.ON
......@@ -266,7 +288,9 @@ def writer_tango(
@contextmanager
def writer_process(session=None, tmpdir=None, config=True, alt=False, **kwargs):
def writer_process(
session=None, tmpdir=None, config=True, alt=False, capture=False, **kwargs
):
"""
Run external writer as a python process
......@@ -274,6 +298,7 @@ def writer_process(session=None, tmpdir=None, config=True, alt=False, **kwargs):
:param tmpdir:
:param bool config:
:param bool alt:
:param bool capture:
:param kwargs: ignored
:returns PopenGreenlet:
"""
......@@ -283,9 +308,7 @@ def writer_process(session=None, tmpdir=None, config=True, alt=False, **kwargs):
+ writer_cli_logargs(tmpdir)
+ writer_options(tango=False, config=config, alt=alt)
)
with nxw_test_utils.popencontext(
cliargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env
) as greenlet:
with nxw_test_utils.popencontext(cliargs, env=env, capture=capture) as greenlet:
with gevent.Timeout(10, RuntimeError("Nexus Writer not running")):
while not greenlet.stdout_contains("Start listening"):
gevent.sleep(0.1)
......@@ -334,10 +357,10 @@ def writer_options(tango=True, config=True, alt=False, required_disk_space=None)
def writer_cli_logargs(tmpdir):
return (
"--log=warning", # applies to log_tango as well (abbreviations allowed)
"--redirectstdout",
"--redirectstderr",
"--logfileout={}".format(tmpdir.join("writer.stdout.log")),
"--logfileerr={}".format(tmpdir.join("writer.stderr.log")),
# "--redirectstdout",
# "--redirectstderr",
# "--logfileout={}".format(tmpdir.join("writer.stdout.log")),
# "--logfileerr={}".format(tmpdir.join("writer.stderr.log")),
)
......
......@@ -6,6 +6,7 @@
# Distributed under the GNU LGPLv3. See LICENSE for more info.
import gevent
import gevent.event
from gevent import subprocess
import sys
import functools
......@@ -186,6 +187,9 @@ class PopenGreenlet(gevent.Greenlet):
self.stderr = b""
self.popenargs = popenargs
self.popenkw = popenkw
if popenkw.pop("capture", False):
popenkw.setdefault("stdout", subprocess.PIPE)
popenkw.setdefault("stderr", subprocess.PIPE)
def __str__(self):
if self.process is None:
......@@ -196,12 +200,17 @@ class PopenGreenlet(gevent.Greenlet):
def _run(self):
self.process = process = subprocess.Popen(*self.popenargs, **self.popenkw)
try:
while True:
if process.stdout is not None:
self.stdout += process.stdout.read1()
if process.stderr is not None:
self.stderr += process.stderr.read1()
gevent.sleep()
capture_stdout = process.stdout is not None
capture_stderr = process.stderr is not None
if capture_stdout or capture_stderr:
while True:
if capture_stdout:
self.stdout += process.stdout.read1()
if capture_stderr:
self.stderr += process.stderr.read1()
gevent.sleep(0.1)
else:
gevent.event.Event().wait()
except gevent.GreenletExit:
pass
finally:
......
......@@ -16,8 +16,8 @@ from tests.nexus_writer.helpers import nxw_test_utils
from tests.nexus_writer.helpers import nxw_test_data
def test_nxw_tango_logging(nexus_writer_config):
_test_nxw_tango_logging(**nexus_writer_config)
def test_nxw_tango_logging(nexus_writer_config_capture):
_test_nxw_tango_logging(**nexus_writer_config_capture)
def test_nxw_tango_api(nexus_writer_config):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment