@@ -46,17 +46,18 @@ class TestEventLoopHandler : public ::testing::Test
46
46
47
47
static System::LayerSocketsLoop & SystemLayer () { return static_cast <System::LayerSocketsLoop &>(DeviceLayer::SystemLayer ()); }
48
48
49
+ // Schedules a call to the provided lambda and returns a cancel function.
49
50
template <class Lambda >
50
- static void Schedule (Timeout delay, Lambda lambda)
51
+ static std::function< void ()> Schedule (Timeout delay, Lambda lambda)
51
52
{
52
- SystemLayer (). StartTimer (
53
- delay,
54
- [](System::Layer * layer, void * ctx) {
55
- auto * function = static_cast <std::function< void ()> *>(ctx );
56
- (* function)() ;
57
- delete function ;
58
- },
59
- new std::function< void ()>(lambda)) ;
53
+ auto * function = new std::function< void ()>(lambda);
54
+ System::TimerCompleteCallback callback = [](System::Layer * layer, void * ctx) {
55
+ auto * function = static_cast <std::function< void ()> *>( ctx);
56
+ (* function)( );
57
+ delete function;
58
+ } ;
59
+ SystemLayer (). StartTimer (delay, callback, function);
60
+ return [=] { SystemLayer (). CancelTimer (callback, function); } ;
60
61
}
61
62
62
63
template <class Lambda >
@@ -125,10 +126,12 @@ TEST_F(TestEventLoopHandler, EventLoopHandlerWake)
125
126
}
126
127
} loopHandler;
127
128
129
+ // Schedule a fallback timer to ensure the test stops
130
+ auto cancelFallback = Schedule(1000_ms, [] { DeviceLayer::PlatformMgr ().StopEventLoopTask (); });
128
131
SystemLayer ().AddLoopHandler(loopHandler);
129
- Schedule (1000_ms, [] { DeviceLayer::PlatformMgr ().StopEventLoopTask (); });
130
132
chip::DeviceLayer::PlatformMgr ().RunEventLoop();
131
133
SystemLayer ().RemoveLoopHandler(loopHandler);
134
+ cancelFallback (); // avoid leaking the fallback timer
132
135
133
136
Timestamp sleepDuration = loopHandler.wakeTimestamp - loopHandler.startTimestamp;
134
137
EXPECT_GE (sleepDuration.count(), 400u ); // loopHandler requested wake-up after 400ms
0 commit comments