Skip to content

Commit 9038d6d

Browse files
committed
src: python_testing: minor changes regardin controllers' id and async
* implementation of generate_random_node_id with exculdes values, the method add possibilty to run multiple test cases within one test session (the controllers' nodeid is the same for new controller in different test cases) * due to change in ChipDeviceController and usage of asyncio.Lock (which use get_event_loop) may cause `RuntimeError: There is no current event loop in thread 'MainThread'` while using multiple times `asyncio.run()` Signed-off-by: Michał Szablowski <michal.szablowski@nordicsemi.no>
1 parent 6e31453 commit 9038d6d

6 files changed

+50
-15
lines changed

src/python_testing/TC_ACE_1_2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from chip.clusters.Attribute import EventReadResult, SubscriptionTransaction, TypedAttributePath
3131
from chip.exceptions import ChipStackError
3232
from chip.interaction_model import Status
33-
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main
33+
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main, generate_random_nodeid
3434
from mobly import asserts
3535

3636

@@ -136,7 +136,7 @@ async def test_TC_ACE_1_2(self):
136136
fabric_admin = self.certificate_authority_manager.activeCaList[0].adminList[0]
137137

138138
TH1_nodeid = self.matter_test_config.controller_node_id
139-
TH2_nodeid = self.matter_test_config.controller_node_id + 1
139+
TH2_nodeid = generate_random_nodeid(excluded_nodeid=[self.matter_test_config.controller_node_id])
140140

141141
self.TH2 = fabric_admin.NewController(nodeId=TH2_nodeid,
142142
paaTrustStorePath=str(self.matter_test_config.paa_trust_store_path))

src/python_testing/TC_ACE_1_3.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import chip.clusters as Clusters
2828
from chip.interaction_model import Status
29-
from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main
29+
from matter_testing_support import MatterBaseTest, TestStep, async_test_body, default_matter_test_main, generate_random_nodeid
3030
from mobly import asserts
3131

3232

@@ -137,9 +137,24 @@ async def test_TC_ACE_1_3(self):
137137
fabric_admin = self.certificate_authority_manager.activeCaList[0].adminList[0]
138138

139139
TH0_nodeid = self.matter_test_config.controller_node_id
140-
TH1_nodeid = self.matter_test_config.controller_node_id + 1
141-
TH2_nodeid = self.matter_test_config.controller_node_id + 2
142-
TH3_nodeid = self.matter_test_config.controller_node_id + 3
140+
TH1_nodeid = generate_random_nodeid(
141+
excluded_nodeid=[
142+
TH0_nodeid
143+
]
144+
)
145+
TH2_nodeid = generate_random_nodeid(
146+
excluded_nodeid=[
147+
TH0_nodeid,
148+
TH1_nodeid
149+
]
150+
)
151+
TH3_nodeid = generate_random_nodeid(
152+
excluded_nodeid=[
153+
TH0_nodeid,
154+
TH1_nodeid,
155+
TH2_nodeid
156+
]
157+
)
143158

144159
TH1 = fabric_admin.NewController(nodeId=TH1_nodeid,
145160
paaTrustStorePath=str(self.matter_test_config.paa_trust_store_path),

src/python_testing/TC_ACE_1_5.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import chip.clusters as Clusters
2828
from chip import ChipDeviceCtrl
2929
from chip.interaction_model import Status
30-
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main
30+
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main, generate_random_nodeid
3131
from mobly import asserts
3232

3333

@@ -53,7 +53,7 @@ async def test_TC_ACE_1_5(self):
5353
new_fabric_admin = new_certificate_authority.NewFabricAdmin(vendorId=0xFFF1, fabricId=self.matter_test_config.fabric_id + 1)
5454

5555
TH1_nodeid = self.matter_test_config.controller_node_id
56-
TH2_nodeid = self.matter_test_config.controller_node_id + 2
56+
TH2_nodeid = generate_random_nodeid(excluded_nodeid=[TH1_nodeid])
5757

5858
self.th2 = new_fabric_admin.NewController(nodeId=TH2_nodeid,
5959
paaTrustStorePath=str(self.matter_test_config.paa_trust_store_path))

src/python_testing/TC_AccessChecker.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from chip.tlv import uint
1717
from global_attribute_ids import GlobalAttributeIds
1818
from matter_testing_support import (AttributePathLocation, ClusterPathLocation, MatterBaseTest, TestStep, async_test_body,
19-
default_matter_test_main)
19+
default_matter_test_main, generate_random_nodeid)
2020
from spec_parsing_support import XmlCluster, build_xml_clusters
2121

2222

@@ -66,7 +66,7 @@ async def setup_class(self):
6666
self._record_errors()
6767
# We need to run this test from two controllers so we can test access to the ACL cluster while retaining access to the ACL cluster
6868
fabric_admin = self.certificate_authority_manager.activeCaList[0].adminList[0]
69-
self.TH2_nodeid = self.matter_test_config.controller_node_id + 1
69+
self.TH2_nodeid = generate_random_nodeid(excluded_nodeid=[self.matter_test_config.controller_node_id])
7070
self.TH2 = fabric_admin.NewController(nodeId=self.TH2_nodeid)
7171

7272
# Both the tests in this suite are potentially long-running if there are a large number of attributes on the DUT

src/python_testing/TC_IDM_4_2.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from chip.clusters.Attribute import AttributePath, TypedAttributePath
2525
from chip.exceptions import ChipStackError
2626
from chip.interaction_model import Status
27-
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main
27+
from matter_testing_support import MatterBaseTest, async_test_body, default_matter_test_main, generate_random_nodeid
2828
from mobly import asserts
2929

3030
'''
@@ -134,7 +134,11 @@ async def test_TC_IDM_4_2(self):
134134
# Subscriber/client with limited access to the DUT
135135
# Will validate error status codes
136136
fabric_admin = self.certificate_authority_manager.activeCaList[0].adminList[0]
137-
CR2_nodeid = self.matter_test_config.controller_node_id + 1
137+
CR2_nodeid = generate_random_nodeid(
138+
excluded_nodeid=[
139+
self.matter_test_config.controller_node_id
140+
]
141+
)
138142
CR2: ChipDeviceController = fabric_admin.NewController(
139143
nodeId=CR2_nodeid,
140144
paaTrustStorePath=str(self.matter_test_config.paa_trust_store_path),

src/python_testing/matter_testing_support.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -1492,19 +1492,35 @@ def parse_matter_test_args(argv: Optional[List[str]] = None) -> MatterTestConfig
14921492

14931493
return convert_args_to_matter_config(parser.parse_known_args(argv)[0])
14941494

1495+
def async_to_sync(awaitable):
1496+
"Call sync, the async function."
1497+
# Usage of new implementation of ChipDeviceController cause the necessary to not use asyncio.run,
1498+
# because the initialization of `asyncio.Lock` use the `asyncio.get_event_loop()` on init.
1499+
# Usage of `asyncio.run()` and `asyncio.get_event_loop()` are exclusive.
1500+
loop = asyncio.get_event_loop()
1501+
return loop.run_until_complete(awaitable)
1502+
1503+
def generate_random_nodeid(excluded_nodeid: typing.Optional[typing.List] = None) -> int:
1504+
if excluded_nodeid:
1505+
while True:
1506+
node_id=random.randint(0, 2^64-1)
1507+
if node_id not in excluded_nodeid:
1508+
return node_id
1509+
else:
1510+
return random.randint(0, 2^64-1)
14951511

14961512
def async_test_body(body):
14971513
"""Decorator required to be applied whenever a `test_*` method is `async def`.
14981514
14991515
Since Mobly doesn't support asyncio directly, and the test methods are called
15001516
synchronously, we need a mechanism to allow an `async def` to be converted to
1501-
a asyncio-run synchronous method. This decorator does the wrapping.
1517+
a synchronous method using asyncio's event loop. This decorator does the wrapping.
15021518
"""
15031519

15041520
def async_runner(self: MatterBaseTest, *args, **kwargs):
15051521
timeout = self.matter_test_config.timeout if self.matter_test_config.timeout is not None else self.default_timeout
15061522
runner_with_timeout = asyncio.wait_for(body(self, *args, **kwargs), timeout=timeout)
1507-
return asyncio.run(runner_with_timeout)
1523+
return async_to_sync(runner_with_timeout)
15081524

15091525
return async_runner
15101526

@@ -1523,7 +1539,7 @@ def test_run_commissioning(self):
15231539
(conf.root_of_trust_index, conf.fabric_id, node_id))
15241540
logging.info("Commissioning method: %s" % conf.commissioning_method)
15251541

1526-
if not asyncio.run(self._commission_device(commission_idx)):
1542+
if not async_to_sync(self._commission_device(commission_idx)):
15271543
raise signals.TestAbortAll("Failed to commission node")
15281544

15291545
async def _commission_device(self, i) -> bool:

0 commit comments

Comments
 (0)