From 9e7e30ccb0ebceeebf42fe1aec71a3d84111ccfe Mon Sep 17 00:00:00 2001 From: Tobias Waldekranz Date: Fri, 10 May 2024 21:51:08 +0000 Subject: [PATCH] test: Run negative ping tests in parallel Rather than having to wait for each timeout sequentially, run each test in parallel. --- test/case/ietf_interfaces/routing_basic.py | 4 +-- test/case/ietf_routing/static_routing.py | 4 +-- .../bridge_vlan_separation.py | 8 +++--- test/infamy/__init__.py | 2 +- test/infamy/util.py | 28 +++++++++++++++++++ 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/test/case/ietf_interfaces/routing_basic.py b/test/case/ietf_interfaces/routing_basic.py index 1f42152f..2f43a625 100755 --- a/test/case/ietf_interfaces/routing_basic.py +++ b/test/case/ietf_interfaces/routing_basic.py @@ -90,7 +90,7 @@ with infamy.Test() as test: ns1.addip("10.0.0.10") ns1.addroute("default", "10.0.0.1") - ns0.must_not_reach("10.0.0.10") - ns1.must_not_reach("192.168.0.10") + infamy.parallel(lambda: ns0.must_not_reach("10.0.0.10"), + lambda: ns1.must_not_reach("192.168.0.10")) test.succeed() diff --git a/test/case/ietf_routing/static_routing.py b/test/case/ietf_routing/static_routing.py index 4dd2f590..1e3bde60 100755 --- a/test/case/ietf_routing/static_routing.py +++ b/test/case/ietf_routing/static_routing.py @@ -237,8 +237,8 @@ with infamy.Test() as test: ns0.addip("192.168.10.2") ns0.addroute("192.168.200.1/32", "192.168.10.1") - ns0.must_not_reach("192.168.200.1") - ns0.must_not_reach("2001:db8:3c4d:200::1") + infamy.parallel(lambda: ns0.must_not_reach("192.168.200.1"), + lambda: ns0.must_not_reach("2001:db8:3c4d:200::1")) test.succeed() diff --git a/test/case/infix_interfaces/bridge_vlan_separation.py b/test/case/infix_interfaces/bridge_vlan_separation.py index eaa4b269..ad97b72d 100755 --- a/test/case/infix_interfaces/bridge_vlan_separation.py +++ b/test/case/infix_interfaces/bridge_vlan_separation.py @@ -154,9 +154,9 @@ with infamy.Test() as test: ns10.must_reach("10.0.0.3") ns11.must_reach("10.0.0.4") - ns10.must_not_reach("10.0.0.4") - ns11.must_not_reach("10.0.0.3") - ns10.must_not_reach("10.0.0.2") - ns11.must_not_reach("10.0.0.1") + infamy.parallel(lambda: ns10.must_not_reach("10.0.0.4"), + lambda: ns11.must_not_reach("10.0.0.3"), + lambda: ns10.must_not_reach("10.0.0.2"), + lambda: ns11.must_not_reach("10.0.0.1")) test.succeed() diff --git a/test/infamy/__init__.py b/test/infamy/__init__.py index ba217045..ba758864 100644 --- a/test/infamy/__init__.py +++ b/test/infamy/__init__.py @@ -6,7 +6,7 @@ from .furl import Furl from .netns import IsolatedMacVlan from .sniffer import Sniffer from .tap import Test -from .util import until +from .util import parallel, until def std_topology(name): return os.path.realpath( diff --git a/test/infamy/util.py b/test/infamy/util.py index 0c7e82d2..3e8316ea 100644 --- a/test/infamy/util.py +++ b/test/infamy/util.py @@ -1,7 +1,35 @@ import time +import threading import infamy.neigh import infamy.netconf as netconf +class ParallelFn(threading.Thread): + def __init__(self, group=None, target=None, name=None, + args=(), kwargs={}, Verbose=None): + threading.Thread.__init__(self, group, target, name, args, kwargs) + self._exc, self._return = None, None + + def run(self): + if self._target is not None: + try: + self._return = self._target(*self._args, + **self._kwargs) + except Exception as e: + self._exc = e + + def join(self, *args): + threading.Thread.join(self, *args) + + if self._exc: + raise self._exc + + return self._return + +def parallel(*fns): + ths = [ParallelFn(target=fn) for fn in fns] + [th.start() for th in ths] + return [th.join() for th in ths] + def until(fn, attempts=10, interval=1): for attempt in range(attempts): if fn():