Skip to content

Commit 50cf7a1

Browse files
authored
Merge pull request #63 from kontsaki/drop_py27_more
Drop py27 more
2 parents 6394960 + 2d30b87 commit 50cf7a1

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ nosetests.xml
4343
coverage.xml
4444
*,cover
4545

46+
# virtual environment
47+
venv
48+
4649
# Translations
4750
*.mo
4851
*.pot

pysipp/agent.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
SocketAddr = namedtuple('SocketAddr', 'ip port')
2020

21+
DEFAULT_RUNNER_TIMEOUT = 180
2122

2223
def tuple_property(attrs):
2324
def getter(self):
@@ -27,7 +28,6 @@ def getter(self):
2728
return None
2829

2930
def setter(self, pair):
30-
3131
if not isinstance(pair, tuple):
3232
if pair is None:
3333
pair = (None, None)
@@ -269,6 +269,7 @@ def __init__(
269269

270270
# agents iterable in launch-order
271271
self._agents = agents
272+
self._prepared_agents = None
272273
ua_attrs = UserAgent.keys()
273274

274275
# default settings
@@ -444,13 +445,27 @@ def from_agents(self, agents=None, autolocalsocks=True, **scenkwargs):
444445

445446
async def arun(
446447
self,
447-
timeout=180,
448+
timeout=DEFAULT_RUNNER_TIMEOUT,
448449
runner=None,
450+
block=True,
449451
):
450-
agents = self.prepare()
451-
runner = runner or launch.TrioRunner()
452+
self._prepared_agents = agents = self.prepare()
453+
self._runner = runner = runner or launch.TrioRunner()
452454

453-
return await launch.run_all_agents(runner, agents, timeout=timeout)
455+
return await launch.run_all_agents(runner, agents, timeout=timeout, block=block)
456+
457+
def finalize(self, *, timeout=DEFAULT_RUNNER_TIMEOUT):
458+
assert (
459+
self._prepared_agents and self._runner
460+
), "Must run scenario before finalizing."
461+
return trio.run(
462+
partial(
463+
launch.finalize,
464+
self._runner,
465+
self._prepared_agents,
466+
timeout=timeout,
467+
)
468+
)
454469

455470
def run(
456471
self,

pysipp/launch.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ async def run(
5656
for cmd in cmds:
5757
log.debug(
5858
"launching cmd:\n\"{}\"\n".format(cmd))
59-
# proc = await trio.open_process(
60-
proc = trio.Process(
59+
proc = await trio.open_process(
6160
shlex.split(cmd),
6261
stdout=subprocess.DEVNULL,
6362
stderr=subprocess.PIPE
@@ -157,32 +156,32 @@ def clear(self):
157156
self._procs.clear()
158157

159158

160-
async def run_all_agents(runner, agents, timeout=180):
161-
"""Run a sequencec of agents using a ``TrioRunner``.
162-
"""
163-
async def finalize():
164-
# this might raise TimeoutError
165-
cmds2procs = await runner.get(timeout=timeout)
166-
agents2procs = list(zip(agents, cmds2procs.values()))
167-
msg = report.err_summary(agents2procs)
168-
if msg:
169-
# report logs and stderr
170-
await report.emit_logfiles(agents2procs)
171-
raise SIPpFailure(msg)
172-
173-
return cmds2procs
159+
async def run_all_agents(runner, agents, timeout=180, block=True):
160+
"""Run a sequencec of agents using a ``TrioRunner``."""
174161

175162
try:
176-
await runner.run(
177-
(ua.render() for ua in agents),
178-
timeout=timeout
179-
)
180-
await finalize()
163+
await runner.run((ua.render() for ua in agents), timeout=timeout)
164+
if block:
165+
await finalize(runner, agents, timeout)
181166
return runner
182167
except TimeoutError as terr:
183168
# print error logs even when we timeout
184169
try:
185-
await finalize()
170+
await finalize(runner, agents, timeout)
186171
except SIPpFailure as err:
187-
assert 'exit code -9' in str(err)
172+
assert "exit code -9" in str(err)
188173
raise terr
174+
175+
176+
async def finalize(runner, agents, timeout):
177+
"""Block up to `timeout` seconds for all agents to complete."""
178+
# this might raise TimeoutError
179+
cmds2procs = await runner.get(timeout=timeout)
180+
agents2procs = list(zip(agents, cmds2procs.values()))
181+
msg = report.err_summary(agents2procs)
182+
if msg:
183+
# report logs and stderr
184+
await report.emit_logfiles(agents2procs)
185+
raise SIPpFailure(msg)
186+
187+
return cmds2procs

tests/test_agent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ def test_pass_bad_socket_addr():
199199
with pytest.raises(ValueError):
200200
pysipp.client(proxyaddr='10.10.8.88')
201201

202-
203202
def test_authentication_arguments():
204203
client = agent.client(auth_username='username', auth_password='passw0rd')
205204

0 commit comments

Comments
 (0)