From 45694341e9b7e234b0c63c02a7a0daf38f13cb9d Mon Sep 17 00:00:00 2001 From: C Freeman Date: Mon, 26 Jun 2023 11:42:34 -0400 Subject: [PATCH] Python tests: fix compare_time, add test (#27457) * Python tests: fix compare_time, add test * Restyled by isort --------- Co-authored-by: Restyled.io --- .../TestMatterTestingSupport.py | 33 +++++++++++++++++-- src/python_testing/matter_testing_support.py | 5 +-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/python_testing/TestMatterTestingSupport.py b/src/python_testing/TestMatterTestingSupport.py index 59476e033d4a52..8762e9d8ff15bc 100644 --- a/src/python_testing/TestMatterTestingSupport.py +++ b/src/python_testing/TestMatterTestingSupport.py @@ -21,9 +21,9 @@ import chip.clusters as Clusters from chip.clusters.Types import Nullable, NullValue from chip.tlv import uint -from matter_testing_support import (MatterBaseTest, async_test_body, default_matter_test_main, parse_pics, type_matches, - utc_time_in_matter_epoch) -from mobly import asserts +from matter_testing_support import (MatterBaseTest, async_test_body, compare_time, default_matter_test_main, parse_pics, + type_matches, utc_time_in_matter_epoch) +from mobly import asserts, signals def get_raw_type_list(): @@ -157,6 +157,33 @@ async def test_pics_support(self): except ValueError: pass + def test_time_compare_function(self): + # only offset, exact match + compare_time(received=1000, offset=timedelta(microseconds=1000), utc=0, tolerance=timedelta()) + # only utc, exact match + compare_time(received=1000, offset=timedelta(), utc=1000, tolerance=timedelta()) + # both, exact match + compare_time(received=2000, offset=timedelta(microseconds=1000), utc=1000, tolerance=timedelta()) + # both, negative offset + compare_time(received=0, offset=timedelta(microseconds=-1000), utc=1000, tolerance=timedelta()) + + # Exact match, within delta, both + compare_time(received=2000, offset=timedelta(microseconds=1000), utc=1000, tolerance=timedelta(seconds=5)) + + # Just inside tolerance + compare_time(received=1001, offset=timedelta(), utc=2000, tolerance=timedelta(microseconds=1000)) + + # Just outside tolerance + try: + compare_time(received=999, offset=timedelta(), utc=2000, tolerance=timedelta(microseconds=1000)) + asserts.fail("Expected failure case for time just outside of the tolerance failed") + except signals.TestFailure: + pass + + # everything in the seconds range + compare_time(received=timedelta(seconds=3600).total_seconds() * 1000000, + offset=timedelta(seconds=3605), utc=0, tolerance=timedelta(seconds=5)) + if __name__ == "__main__": default_matter_test_main() diff --git a/src/python_testing/matter_testing_support.py b/src/python_testing/matter_testing_support.py index 9f72c6af858784..d3e768c806e84e 100644 --- a/src/python_testing/matter_testing_support.py +++ b/src/python_testing/matter_testing_support.py @@ -188,10 +188,11 @@ def compare_time(received: int, offset: timedelta = timedelta(), utc: int = None if utc is None: utc = utc_time_in_matter_epoch() - expected = utc + offset.microseconds + # total seconds includes fractional for microseconds + expected = utc + offset.total_seconds()*1000000 delta_us = abs(expected - received) delta = timedelta(microseconds=delta_us) - asserts.assert_less(delta, tolerance, "Received time is out of tolerance") + asserts.assert_less_equal(delta, tolerance, "Received time is out of tolerance") @dataclass