Skip to content

Commit 6f524e4

Browse files
authored
Merge pull request #88 from linuxmaniac/vseva/trio
trio subprocs: small fixes for #87
2 parents 57ffd9e + bb22d6f commit 6f524e4

File tree

3 files changed

+27
-49
lines changed

3 files changed

+27
-49
lines changed

pysipp/agent.py

+7-20
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
import tempfile
77
from collections import namedtuple
88
from collections import OrderedDict
9-
from functools import partial
109
from copy import deepcopy
10+
from functools import partial
1111
from os import path
12-
from distutils import spawn
1312

1413
import trio
14+
from distutils import spawn
1515

1616
from . import command
17+
from . import launch
1718
from . import plugin
1819
from . import utils
19-
from . import launch
2020

2121
log = utils.get_logger()
2222

@@ -72,11 +72,7 @@ def name(self):
7272
def __call__(self, *args, **kwargs):
7373
return self.run(*args, **kwargs)
7474

75-
def run(
76-
self,
77-
timeout=180,
78-
**kwargs
79-
):
75+
def run(self, timeout=180, **kwargs):
8076

8177
# create and configure a temp scenario
8278
scen = plugin.mng.hook.pysipp_conf_scen_protocol(
@@ -472,20 +468,11 @@ async def arun(
472468
timeout=timeout,
473469
)
474470

475-
def run(
476-
self,
477-
timeout=180,
478-
**kwargs
479-
):
471+
def run(self, timeout=180, **kwargs):
480472
"""Run scenario blocking to completion."""
481-
return trio.run(
482-
partial(
483-
self.arun,
484-
timeout=timeout,
485-
**kwargs
486-
)
487-
)
473+
return trio.run(partial(self.arun, timeout=timeout, **kwargs))
488474

489475
def __call__(self, *args, **kwargs):
490476
# TODO: deprecation warning here
477+
kwargs.pop("block", None)
491478
return self.run(*args, **kwargs)

pysipp/launch.py

+19-28
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
from functools import partial
1111
from pprint import pformat
1212

13-
from . import utils
14-
1513
import trio
1614

1715
from . import report
16+
from . import utils
1817

1918
log = utils.get_logger()
2019

@@ -26,8 +25,7 @@ class TimeoutError(Exception):
2625

2726

2827
class SIPpFailure(RuntimeError):
29-
"""SIPp commands failed
30-
"""
28+
"""SIPp commands failed"""
3129

3230

3331
class TrioRunner:
@@ -42,13 +40,7 @@ def __init__(
4240
# store proc results
4341
self._procs = OrderedDict()
4442

45-
async def run(
46-
self,
47-
nursery,
48-
cmds,
49-
rate=300,
50-
**kwargs
51-
):
43+
async def run(self, nursery, cmds, rate=300, **kwargs):
5244
if self.is_alive():
5345
raise RuntimeError(
5446
"Not all processes from a prior run have completed"
@@ -59,16 +51,15 @@ async def run(
5951
)
6052
# run agent commands in sequence
6153
for cmd in cmds:
62-
log.debug(
63-
"launching cmd:\n\"{}\"\n".format(cmd)
64-
)
54+
log.debug('launching cmd:\n"{}"\n'.format(cmd))
6555

6656
proc = await nursery.start(
6757
partial(
6858
trio.run_process,
6959
shlex.split(cmd),
7060
stdout=subprocess.DEVNULL,
71-
stderr=subprocess.PIPE
61+
stderr=subprocess.PIPE,
62+
check=False,
7263
)
7364
)
7465
self._procs[cmd] = proc
@@ -125,15 +116,19 @@ async def wait_on_proc(proc):
125116
# all procs were killed by SIGUSR1
126117
raise TimeoutError(
127118
"pids '{}' failed to complete after '{}' seconds".format(
128-
pformat([p.pid for p in signalled.values()]), timeout)
119+
pformat([p.pid for p in signalled.values()]), timeout
120+
)
129121
)
130122

131123
def iterprocs(self):
132124
"""Iterate all processes which are still alive yielding
133125
(cmd, proc) pairs
134126
"""
135-
return ((cmd, proc) for cmd, proc in self._procs.items()
136-
if proc and proc.poll() is None)
127+
return (
128+
(cmd, proc)
129+
for cmd, proc in self._procs.items()
130+
if proc and proc.poll() is None
131+
)
137132

138133
def stop(self):
139134
"""Stop all agents with SIGUSR1 as per SIPp's signal handling"""
@@ -160,8 +155,7 @@ def is_alive(self):
160155
return any(self.iterprocs())
161156

162157
def clear(self):
163-
"""Clear all processes from the last run
164-
"""
158+
"""Clear all processes from the last run"""
165159
assert not self.is_alive(), "Not all processes have completed"
166160
self._procs.clear()
167161

@@ -170,28 +164,25 @@ async def run_all_agents(
170164
runner,
171165
agents,
172166
timeout=180,
173-
174167
) -> TrioRunner:
175-
"""Run a sequencec of agents using a ``TrioRunner``.
176-
"""
168+
"""Run a sequencec of agents using a ``TrioRunner``."""
169+
177170
async def finalize():
178171
# this might raise TimeoutError
179172
cmds2procs = await runner.get(timeout=timeout)
180173
agents2procs = list(zip(agents, cmds2procs.values()))
181174
msg = report.err_summary(agents2procs)
182175
if msg:
183176
# report logs and stderr
184-
await report.emit_logfiles(agents2procs)
177+
report.emit_logfiles(agents2procs)
185178
raise SIPpFailure(msg)
186179

187180
return cmds2procs
188181

189182
try:
190183
async with trio.open_nursery() as nurse:
191184
await runner.run(
192-
nurse,
193-
(ua.render() for ua in agents),
194-
timeout=timeout
185+
nurse, (ua.render() for ua in agents), timeout=timeout
195186
)
196187
await finalize()
197188
return runner
@@ -201,5 +192,5 @@ async def finalize():
201192
try:
202193
await finalize()
203194
except SIPpFailure as err:
204-
assert 'exit code -9' in str(err)
195+
assert "exit code -9" in str(err)
205196
raise terr

tests/test_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def test_server():
118118
# test client failure on bad remote destination
119119
(agent.client(destaddr=("99.99.99.99", 5060)), 1, {}, RuntimeError),
120120
# test if server times out it is signalled
121-
(agent.server(), 0, {"timeout": 1}, launch.TimeoutError),
121+
(agent.server(), -9, {"timeout": 1}, launch.TimeoutError),
122122
],
123123
ids=["ua", "uac", "uas"],
124124
)

0 commit comments

Comments
 (0)