Do p2pd.close if not all of them succeed

This commit is contained in:
mhchia
2019-09-04 18:25:51 +08:00
parent 51d547ccc5
commit db0da8083a
2 changed files with 18 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import asyncio
import sys
from typing import Union
import pexpect
import pytest
@ -7,7 +8,7 @@ import pytest
from tests.factories import FloodsubFactory, GossipsubFactory, PubsubFactory
from tests.pubsub.configs import GOSSIPSUB_PARAMS
from .daemon import make_p2pd
from .daemon import Daemon, make_p2pd
@pytest.fixture
@ -42,7 +43,7 @@ def is_gossipsub():
@pytest.fixture
async def p2pds(num_p2pds, is_host_secure, is_gossipsub, unused_tcp_port_factory):
p2pds = await asyncio.gather(
p2pds: Union[Daemon, Exception] = await asyncio.gather(
*[
make_p2pd(
unused_tcp_port_factory(),
@ -51,8 +52,15 @@ async def p2pds(num_p2pds, is_host_secure, is_gossipsub, unused_tcp_port_factory
is_gossipsub=is_gossipsub,
)
for _ in range(num_p2pds)
]
],
return_exceptions=True,
)
p2pds_succeeded = tuple(p2pd for p2pd in p2pds if isinstance(p2pd, Daemon))
if len(p2pds_succeeded) != len(p2pds):
# Not all succeeded. Close the succeeded ones and print the failed ones(exceptions).
await asyncio.gather(*[p2pd.close() for p2pd in p2pds_succeeded])
exceptions = tuple(p2pd for p2pd in p2pds if isinstance(p2pd, Exception))
raise Exception(f"not all p2pds succeed: first exception={exceptions[0]}")
try:
yield p2pds
finally: