@@ -26,18 +26,6 @@ using namespace chip;
26
26
using namespace chip ::System::Clock::Literals;
27
27
using chip::System::Clock::Timeout;
28
28
29
- class MockClock : public System ::Clock::ClockImpl
30
- {
31
- public:
32
- System::Clock::Microseconds64 GetMonotonicMicroseconds64 () override { return mMsec ; }
33
- System::Clock::Milliseconds64 GetMonotonicMilliseconds64 () override { return mMsec ; }
34
-
35
- void Advance (System::Clock::Milliseconds32 ms) { mMsec += ms; }
36
-
37
- private:
38
- System::Clock::Milliseconds64 mMsec ;
39
- };
40
-
41
29
PeerId MakePeerId (NodeId nodeId)
42
30
{
43
31
PeerId peerId;
@@ -46,10 +34,10 @@ PeerId MakePeerId(NodeId nodeId)
46
34
47
35
void TestSinglePeerAddRemove (nlTestSuite * inSuite, void * inContext)
48
36
{
49
- MockClock mockClock;
37
+ System::Clock::Internal:: MockClock mockClock;
50
38
mdns::Minimal::ActiveResolveAttempts attempts (&mockClock);
51
39
52
- mockClock.Advance (1234_ms32);
40
+ mockClock.AdvanceMonotonic (1234_ms32);
53
41
54
42
// Starting up, no scheduled peers are expected
55
43
NL_TEST_ASSERT (inSuite, !attempts.GetTimeUntilNextExpectedResponse ().HasValue ());
@@ -65,20 +53,20 @@ void TestSinglePeerAddRemove(nlTestSuite * inSuite, void * inContext)
65
53
66
54
// one Next schedule is called, expect to have a delay of 1000 ms
67
55
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(1000_ms32));
68
- mockClock.Advance (500_ms32);
56
+ mockClock.AdvanceMonotonic (500_ms32);
69
57
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(500_ms32));
70
58
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
71
59
72
60
// past due date: timeout should be 0
73
- mockClock.Advance (800_ms32);
61
+ mockClock.AdvanceMonotonic (800_ms32);
74
62
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(0_ms32));
75
63
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (1 )));
76
64
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
77
65
78
66
// one Next schedule is called, expect to have a delay of 2000 ms
79
67
// sincve the timeout doubles every time
80
68
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(2000_ms32));
81
- mockClock.Advance (100_ms32);
69
+ mockClock.AdvanceMonotonic (100_ms32);
82
70
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(1900_ms32));
83
71
84
72
// once complete, nothing to schedule
@@ -89,10 +77,10 @@ void TestSinglePeerAddRemove(nlTestSuite * inSuite, void * inContext)
89
77
90
78
void TestRescheduleSamePeerId (nlTestSuite * inSuite, void * inContext)
91
79
{
92
- MockClock mockClock;
80
+ System::Clock::Internal:: MockClock mockClock;
93
81
mdns::Minimal::ActiveResolveAttempts attempts (&mockClock);
94
82
95
- mockClock.Advance (112233_ms32);
83
+ mockClock.AdvanceMonotonic (112233_ms32);
96
84
97
85
attempts.MarkPending (MakePeerId (1 ));
98
86
@@ -104,7 +92,7 @@ void TestRescheduleSamePeerId(nlTestSuite * inSuite, void * inContext)
104
92
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(1000_ms32));
105
93
106
94
// 2nd try goes to 2 seconds (once at least 1 second passes)
107
- mockClock.Advance (1234_ms32);
95
+ mockClock.AdvanceMonotonic (1234_ms32);
108
96
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(0_ms32));
109
97
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (1 )));
110
98
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
@@ -122,21 +110,21 @@ void TestRescheduleSamePeerId(nlTestSuite * inSuite, void * inContext)
122
110
void TestLRU (nlTestSuite * inSuite, void * inContext)
123
111
{
124
112
// validates that the LRU logic is working
125
- MockClock mockClock;
113
+ System::Clock::Internal:: MockClock mockClock;
126
114
mdns::Minimal::ActiveResolveAttempts attempts (&mockClock);
127
115
128
- mockClock.Advance (334455_ms32);
116
+ mockClock.AdvanceMonotonic (334455_ms32);
129
117
130
118
// add a single very old peer
131
119
attempts.MarkPending (MakePeerId (9999 ));
132
120
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (9999 )));
133
121
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
134
122
135
- mockClock.Advance (1000_ms32);
123
+ mockClock.AdvanceMonotonic (1000_ms32);
136
124
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (9999 )));
137
125
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
138
126
139
- mockClock.Advance (2000_ms32);
127
+ mockClock.AdvanceMonotonic (2000_ms32);
140
128
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (9999 )));
141
129
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
142
130
@@ -145,7 +133,7 @@ void TestLRU(nlTestSuite * inSuite, void * inContext)
145
133
for (uint32_t i = 1 ; i < mdns::Minimal::ActiveResolveAttempts::kRetryQueueSize ; i++)
146
134
{
147
135
attempts.MarkPending (MakePeerId (i));
148
- mockClock.Advance (1_ms32);
136
+ mockClock.AdvanceMonotonic (1_ms32);
149
137
150
138
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (i)));
151
139
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
@@ -159,7 +147,7 @@ void TestLRU(nlTestSuite * inSuite, void * inContext)
159
147
160
148
// add another element - this should overwrite peer 9999
161
149
attempts.MarkPending (MakePeerId (mdns::Minimal::ActiveResolveAttempts::kRetryQueueSize ));
162
- mockClock.Advance (32_s16);
150
+ mockClock.AdvanceMonotonic (32_s16);
163
151
164
152
for (Optional<PeerId> peerId = attempts.NextScheduledPeer (); peerId.HasValue (); peerId = attempts.NextScheduledPeer ())
165
153
{
@@ -182,7 +170,7 @@ void TestLRU(nlTestSuite * inSuite, void * inContext)
182
170
break ;
183
171
}
184
172
185
- mockClock.Advance (ms.Value ());
173
+ mockClock.AdvanceMonotonic (ms.Value ());
186
174
187
175
Optional<PeerId> peerId = attempts.NextScheduledPeer ();
188
176
while (peerId.HasValue ())
@@ -196,18 +184,18 @@ void TestLRU(nlTestSuite * inSuite, void * inContext)
196
184
197
185
void TestNextPeerOrdering (nlTestSuite * inSuite, void * inContext)
198
186
{
199
- MockClock mockClock;
187
+ System::Clock::Internal:: MockClock mockClock;
200
188
mdns::Minimal::ActiveResolveAttempts attempts (&mockClock);
201
189
202
- mockClock.Advance (123321_ms32);
190
+ mockClock.AdvanceMonotonic (123321_ms32);
203
191
204
192
// add a single peer that will be resolved quickly
205
193
attempts.MarkPending (MakePeerId (1 ));
206
194
207
195
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (1 )));
208
196
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
209
197
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(1000_ms32));
210
- mockClock.Advance (20_ms32);
198
+ mockClock.AdvanceMonotonic (20_ms32);
211
199
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(980_ms32));
212
200
213
201
// expect peerid to be resolve within 1 second from now
@@ -216,13 +204,13 @@ void TestNextPeerOrdering(nlTestSuite * inSuite, void * inContext)
216
204
// mock that we are querying 2 as well
217
205
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (2 )));
218
206
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
219
- mockClock.Advance (80_ms32);
207
+ mockClock.AdvanceMonotonic (80_ms32);
220
208
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(900_ms32));
221
209
222
210
// Peer 1 is done, now peer2 should be pending (in 980ms)
223
211
attempts.Complete (MakePeerId (1 ));
224
212
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(920_ms32));
225
- mockClock.Advance (20_ms32);
213
+ mockClock.AdvanceMonotonic (20_ms32);
226
214
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(900_ms32));
227
215
228
216
// Once peer 3 is added, queue should be
@@ -236,14 +224,14 @@ void TestNextPeerOrdering(nlTestSuite * inSuite, void * inContext)
236
224
// After the clock advance
237
225
// - 400 ms until peer id 2 is pending
238
226
// - 500 ms until peer id 3 is pending
239
- mockClock.Advance (500_ms32);
227
+ mockClock.AdvanceMonotonic (500_ms32);
240
228
241
229
NL_TEST_ASSERT (inSuite, attempts.GetTimeUntilNextExpectedResponse () == Optional<Timeout>(400_ms32));
242
230
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
243
231
244
232
// advancing the clock 'too long' will return both other entries, in reverse order due to how
245
233
// the internal cache is built
246
- mockClock.Advance (500_ms32);
234
+ mockClock.AdvanceMonotonic (500_ms32);
247
235
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (3 )));
248
236
NL_TEST_ASSERT (inSuite, attempts.NextScheduledPeer () == Optional<PeerId>::Value (MakePeerId (2 )));
249
237
NL_TEST_ASSERT (inSuite, !attempts.NextScheduledPeer ().HasValue ());
0 commit comments