@@ -67,11 +67,17 @@ chip::EndpointId kInvalidTestEndpointId = 3;
67
67
chip::DataVersion kTestDataVersion1 = 3 ;
68
68
chip::DataVersion kTestDataVersion2 = 5 ;
69
69
70
+ static chip::System::Clock::Internal::MockClock mockClock;
71
+ static chip::System::Clock::ClockBase * realClock;
72
+
70
73
class TestContext : public chip ::Test::AppContext
71
74
{
72
75
public:
73
76
static int Initialize (void * context)
74
77
{
78
+ realClock = &chip::System::SystemClock ();
79
+ chip::System::Clock::Internal::SetSystemClockForTesting (&mockClock);
80
+
75
81
if (AppContext::Initialize (context) != SUCCESS)
76
82
return FAILURE;
77
83
@@ -97,6 +103,7 @@ class TestContext : public chip::Test::AppContext
97
103
static int Finalize (void * context)
98
104
{
99
105
chip::app::EventManagement::DestroyEventManagement ();
106
+ chip::System::Clock::Internal::SetSystemClockForTesting (realClock);
100
107
101
108
if (AppContext::Finalize (context) != SUCCESS)
102
109
return FAILURE;
@@ -1598,17 +1605,9 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
1598
1605
1599
1606
// Test report with 2 different path
1600
1607
1601
- // Wait for min interval to elapse
1602
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
1603
- while (true )
1604
- {
1605
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
1606
- System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ))
1607
- {
1608
- break ;
1609
- }
1610
- ctx.GetIOContext ().DriveIO ();
1611
- }
1608
+ // Advance monotonic timestamp for min interval to elapse
1609
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ));
1610
+ ctx.GetIOContext ().DriveIO ();
1612
1611
1613
1612
delegate.mGotReport = false ;
1614
1613
delegate.mGotEventResponse = false ;
@@ -1626,17 +1625,10 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
1626
1625
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 2 );
1627
1626
1628
1627
// Test report with 2 different path, and 1 same path
1629
- // Wait for min interval to elapse
1630
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1631
- while (true )
1632
- {
1633
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
1634
- System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ))
1635
- {
1636
- break ;
1637
- }
1638
- ctx.GetIOContext ().DriveIO ();
1639
- }
1628
+ // Advance monotonic timestamp for min interval to elapse
1629
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ));
1630
+ ctx.GetIOContext ().DriveIO ();
1631
+
1640
1632
delegate.mGotReport = false ;
1641
1633
delegate.mNumAttributeResponse = 0 ;
1642
1634
err = engine->GetReportingEngine ().SetDirty (dirtyPath1);
@@ -1652,17 +1644,10 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
1652
1644
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 2 );
1653
1645
1654
1646
// Test report with 3 different path, and one path is overlapped with another
1655
- // Wait for min interval to elapse
1656
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1657
- while (true )
1658
- {
1659
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
1660
- System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ))
1661
- {
1662
- break ;
1663
- }
1664
- ctx.GetIOContext ().DriveIO ();
1665
- }
1647
+ // Advance monotonic timestamp for min interval to elapse
1648
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ));
1649
+ ctx.GetIOContext ().DriveIO ();
1650
+
1666
1651
delegate.mGotReport = false ;
1667
1652
delegate.mNumAttributeResponse = 0 ;
1668
1653
err = engine->GetReportingEngine ().SetDirty (dirtyPath1);
@@ -1678,17 +1663,10 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
1678
1663
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 2 );
1679
1664
1680
1665
// Test report with 3 different path, all are not overlapped, one path is not interested for current subscription
1681
- // Wait for min interval to elapse
1682
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1683
- while (true )
1684
- {
1685
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
1686
- System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ))
1687
- {
1688
- break ;
1689
- }
1690
- ctx.GetIOContext ().DriveIO ();
1691
- }
1666
+ // Advance monotonic timestamp for min interval to elapse
1667
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMinIntervalFloorSeconds ));
1668
+ ctx.GetIOContext ().DriveIO ();
1669
+
1692
1670
delegate.mGotReport = false ;
1693
1671
delegate.mNumAttributeResponse = 0 ;
1694
1672
err = engine->GetReportingEngine ().SetDirty (dirtyPath1);
@@ -1704,16 +1682,8 @@ void TestReadInteraction::TestSubscribeRoundtrip(nlTestSuite * apSuite, void * a
1704
1682
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 2 );
1705
1683
1706
1684
// Test empty report
1707
- // Wait for max interval to elapse
1708
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1709
- while (true )
1710
- {
1711
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
1712
- System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ))
1713
- {
1714
- break ;
1715
- }
1716
- }
1685
+ // Advance monotonic timestamp for min interval to elapse
1686
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ));
1717
1687
ctx.GetIOContext ().DriveIO ();
1718
1688
1719
1689
NL_TEST_ASSERT (apSuite, engine->GetReportingEngine ().IsRunScheduled ());
@@ -1788,8 +1758,7 @@ void TestReadInteraction::TestSubscribeUrgentWildcardEvent(nlTestSuite * apSuite
1788
1758
NL_TEST_ASSERT (apSuite, err == CHIP_NO_ERROR);
1789
1759
1790
1760
ctx.DrainAndServiceIO ();
1791
-
1792
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
1761
+ System::Clock::Timestamp startTime = mockClock.GetMonotonicTimestamp ();
1793
1762
1794
1763
NL_TEST_ASSERT (apSuite, engine->GetNumActiveReadHandlers () == 2 );
1795
1764
NL_TEST_ASSERT (apSuite, engine->ActiveHandlerAt (0 ) != nullptr );
@@ -1813,60 +1782,60 @@ void TestReadInteraction::TestSubscribeUrgentWildcardEvent(nlTestSuite * apSuite
1813
1782
nonUrgentDelegate.mGotEventResponse = false ;
1814
1783
nonUrgentDelegate.mGotReport = false ;
1815
1784
1816
- // wait for min interval 2 seconds (in test, we use 1 .6 seconds considering the time variation), expect no event is
1785
+ // wait for min interval 1 seconds (in test, we use 0 .6 seconds considering the time variation), expect no event is
1817
1786
// received, then wait for 0.8 seconds, then the urgent event would be sent out
1818
1787
// currently DriveIOUntil will call `DriveIO` at least once, which means that if there is any CPU scheduling issues,
1819
1788
// there's a chance 1.9s will already have elapsed by the time we get there, which will result in DriveIO being called when
1820
1789
// it shouldn't. Better fix could happen inside DriveIOUntil, not sure the sideeffect there.
1790
+
1791
+ // Advance monotonic looping to allow events to trigger
1821
1792
while (true )
1822
1793
{
1823
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (600 ))
1794
+ ctx.GetIOContext ().DriveIO ();
1795
+ if ((mockClock.GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (600 ))
1824
1796
{
1825
1797
break ;
1826
1798
}
1827
- ctx. GetIOContext (). DriveIO (); // at least one IO loop is guaranteed
1799
+ mockClock. AdvanceMonotonic ( System::Clock::Milliseconds32 ( 10 ));
1828
1800
}
1801
+ ctx.GetIOContext ().DriveIO ();
1829
1802
1830
1803
NL_TEST_ASSERT (apSuite, !delegate.mGotEventResponse );
1831
1804
NL_TEST_ASSERT (apSuite, !nonUrgentDelegate.mGotEventResponse );
1832
1805
1833
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1806
+ // Advance monotonic timestamp for min interval to elapse
1807
+ startTime = mockClock.GetMonotonicTimestamp ();
1834
1808
while (true )
1835
1809
{
1836
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (800 ))
1810
+ ctx.GetIOContext ().DriveIO ();
1811
+ if ((mockClock.GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (800 ))
1837
1812
{
1838
1813
break ;
1839
1814
}
1840
- ctx. GetIOContext (). DriveIO (); // at least one IO loop is guaranteed
1815
+ mockClock. AdvanceMonotonic ( System::Clock::Milliseconds32 ( 10 ));
1841
1816
}
1817
+ ctx.GetIOContext ().DriveIO ();
1818
+
1842
1819
NL_TEST_ASSERT (apSuite, delegate.mGotEventResponse );
1843
1820
NL_TEST_ASSERT (apSuite, !nonUrgentDelegate.mGotEventResponse );
1844
1821
1845
1822
// Since we just sent a report for our urgent subscription, the min interval of the urgent subcription should have been
1846
1823
// updated
1847
1824
NL_TEST_ASSERT (apSuite,
1848
- reportScheduler->GetMinTimestampForHandler (delegate.mpReadHandler ) >
1849
- System::SystemClock ().GetMonotonicTimestamp ());
1825
+ reportScheduler->GetMinTimestampForHandler (delegate.mpReadHandler ) > mockClock.GetMonotonicTimestamp ());
1850
1826
NL_TEST_ASSERT (apSuite, !delegate.mpReadHandler ->IsDirty ());
1851
1827
delegate.mGotEventResponse = false ;
1852
1828
1853
1829
// For our non-urgent subscription, we did not send anything, so the min interval should of the non urgent subcription
1854
1830
// should be in the past
1855
1831
NL_TEST_ASSERT (apSuite,
1856
1832
reportScheduler->GetMinTimestampForHandler (nonUrgentDelegate.mpReadHandler ) <
1857
- System::SystemClock () .GetMonotonicTimestamp ());
1833
+ mockClock .GetMonotonicTimestamp ());
1858
1834
NL_TEST_ASSERT (apSuite, !nonUrgentDelegate.mpReadHandler ->IsDirty ());
1859
1835
1860
- // Wait for the min interval timer to fire.
1861
- startTime = System::SystemClock ().GetMonotonicTimestamp ();
1862
- while (true )
1863
- {
1864
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (2100 ))
1865
- {
1866
- break ;
1867
- }
1868
- ctx.GetIOContext ().DriveIO (); // at least one IO loop is guaranteed
1869
- }
1836
+ // Advance monotonic timestamp for min interval to elapse
1837
+ mockClock.AdvanceMonotonic (System::Clock::Milliseconds32 (2100 ));
1838
+ ctx.GetIOContext ().DriveIO ();
1870
1839
1871
1840
// No reporting should have happened.
1872
1841
NL_TEST_ASSERT (apSuite, !delegate.mGotEventResponse );
@@ -2338,16 +2307,8 @@ void TestReadInteraction::TestSubscribeInvalidAttributePathRoundtrip(nlTestSuite
2338
2307
NL_TEST_ASSERT (apSuite, engine->ActiveHandlerAt (0 ) != nullptr );
2339
2308
delegate.mpReadHandler = engine->ActiveHandlerAt (0 );
2340
2309
2341
- // Wait for max interval to elapse
2342
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
2343
- while (true )
2344
- {
2345
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
2346
- System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ))
2347
- {
2348
- break ;
2349
- }
2350
- }
2310
+ // Advance monotonic timestamp for min interval to elapse
2311
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ));
2351
2312
ctx.GetIOContext ().DriveIO ();
2352
2313
2353
2314
NL_TEST_ASSERT (apSuite, engine->GetReportingEngine ().IsRunScheduled ());
@@ -2540,15 +2501,7 @@ void TestReadInteraction::TestPostSubscribeRoundtripStatusReportTimeout(nlTestSu
2540
2501
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 2 );
2541
2502
2542
2503
// Wait for max interval to elapse
2543
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
2544
- while (true )
2545
- {
2546
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
2547
- System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ))
2548
- {
2549
- break ;
2550
- }
2551
- }
2504
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ));
2552
2505
ctx.GetIOContext ().DriveIO ();
2553
2506
2554
2507
delegate.mGotReport = false ;
@@ -2898,15 +2851,7 @@ void TestReadInteraction::TestPostSubscribeRoundtripChunkStatusReportTimeout(nlT
2898
2851
dirtyPath1.mEndpointId = Test::kMockEndpoint3 ;
2899
2852
dirtyPath1.mAttributeId = Test::MockAttributeId (4 );
2900
2853
2901
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
2902
- while (true )
2903
- {
2904
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
2905
- System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ))
2906
- {
2907
- break ;
2908
- }
2909
- }
2854
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ));
2910
2855
ctx.GetIOContext ().DriveIO ();
2911
2856
2912
2857
err = engine->GetReportingEngine ().SetDirty (dirtyPath1);
@@ -3010,15 +2955,7 @@ void TestReadInteraction::TestPostSubscribeRoundtripChunkReportTimeout(nlTestSui
3010
2955
dirtyPath1.mEndpointId = Test::kMockEndpoint3 ;
3011
2956
dirtyPath1.mAttributeId = Test::MockAttributeId (4 );
3012
2957
3013
- System::Clock::Timestamp startTime = System::SystemClock ().GetMonotonicTimestamp ();
3014
- while (true )
3015
- {
3016
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >=
3017
- System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ))
3018
- {
3019
- break ;
3020
- }
3021
- }
2958
+ mockClock.AdvanceMonotonic (System::Clock::Seconds16 (readPrepareParams.mMaxIntervalCeilingSeconds ));
3022
2959
ctx.GetIOContext ().DriveIO ();
3023
2960
3024
2961
err = engine->GetReportingEngine ().SetDirty (dirtyPath1);
@@ -3127,26 +3064,27 @@ void TestReadInteraction::TestPostSubscribeRoundtripChunkReport(nlTestSuite * ap
3127
3064
3128
3065
// wait for min interval 1 seconds(in test, we use 0.9second considering the time variation), expect no event is received,
3129
3066
// then wait for 0.5 seconds, then all chunked dirty reports are sent out, which would not honor minInterval
3130
- System::Clock::Timestamp startTime = System::SystemClock () .GetMonotonicTimestamp ();
3067
+ System::Clock::Timestamp startTime = mockClock .GetMonotonicTimestamp ();
3131
3068
while (true )
3132
3069
{
3133
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (900 ))
3070
+ ctx.GetIOContext ().DriveIO ();
3071
+ if ((mockClock.GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (900 ))
3134
3072
{
3135
3073
break ;
3136
3074
}
3137
- ctx. GetIOContext (). DriveIO ( );
3075
+ mockClock. AdvanceMonotonic ( System::Clock::Milliseconds32 ( 10 ) );
3138
3076
}
3139
3077
NL_TEST_ASSERT (apSuite, delegate.mNumAttributeResponse == 0 );
3140
- startTime = System::SystemClock () .GetMonotonicTimestamp ();
3078
+ startTime = mockClock .GetMonotonicTimestamp ();
3141
3079
while (true )
3142
3080
{
3143
- if ((System::SystemClock ().GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (500 ))
3081
+ ctx.GetIOContext ().DriveIO ();
3082
+ if ((mockClock.GetMonotonicTimestamp () - startTime) >= System::Clock::Milliseconds32 (500 ))
3144
3083
{
3145
3084
break ;
3146
3085
}
3147
- ctx. GetIOContext (). DriveIO ( );
3086
+ mockClock. AdvanceMonotonic ( System::Clock::Milliseconds32 ( 10 ) );
3148
3087
}
3149
- ctx.DrainAndServiceIO ();
3150
3088
}
3151
3089
// Two chunked reports carry 4 attributeDataIB: 1 with a list of 3 items,
3152
3090
// and then one per remaining item.
0 commit comments