forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix timer cancellation to be reliable in LayerImplSelect. (project-ch…
…ip#22375) * Duplicate src/system/tests/TestSystemScheduleLambda.cpp history in src/system/tests/TestSystemScheduleWork.cpp history. * Fix timer cancellation to be reliable in LayerImplSelect. To minimize risk, the changes here keep the "grab all the timers we should fire, then fire them" setup instead of switching to the "fire the timers one at a time" approach LayerImplFreeRTOS uses. The fix consists of the following parts: 1) Store the list of timers to fire in a member, so we can cancel things from that list as needed. 2) To avoid canceling things scheduled by ScheduleWork, remove the CancelTimer call in ScheduleWork. This does mean we now allow multiple timers for the same callback+appState in the timer list, if they were created by ScheduleWork, but that should be OK, since the only reason that pair needs to be unique is to allow cancellation and we never want to cancel the things ScheduleWork schedules. As a followup we should stop using the timer list for ScheduleWork altogether and use ScheduleLambda like LayerImplFreeRTOS does, but that involves fixing the unit tests that call ScheduleWork without actually running the platfor manager event loop and expect it to work somehow. TestRead was failing the sanity assert for not losing track of timers to fire, because it was spinning a nested event loop. The changes to that test stop it from doing that. Fixes project-chip#19387 Fixes project-chip#22160 * Add a unit test that timer cancelling works even for currently "in progress" timers * Address review comments. * Fix shadowing problem. * Turn off TestSystemScheduleWork on esp32 QEMU for now. * Turn off TestSystemScheduleWork on the fake platform too. The fake platform's event loop does not actually process the SystemLayer events. Co-authored-by: Andrei Litvin <[email protected]>
- Loading branch information
1 parent
add21bb
commit dd9f009
Showing
8 changed files
with
385 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <system/SystemConfig.h> | ||
|
||
#include <lib/support/CodeUtils.h> | ||
#include <lib/support/ErrorStr.h> | ||
#include <lib/support/UnitTestRegistration.h> | ||
#include <nlunit-test.h> | ||
#include <platform/CHIPDeviceLayer.h> | ||
|
||
static void IncrementIntCounter(chip::System::Layer *, void * state) | ||
{ | ||
++(*static_cast<int *>(state)); | ||
} | ||
|
||
static void StopEventLoop(chip::System::Layer *, void *) | ||
{ | ||
chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); | ||
} | ||
|
||
static void CheckScheduleWorkTwice(nlTestSuite * inSuite, void * aContext) | ||
{ | ||
int * callCount = new int(0); | ||
NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, callCount) == CHIP_NO_ERROR); | ||
NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(IncrementIntCounter, callCount) == CHIP_NO_ERROR); | ||
NL_TEST_ASSERT(inSuite, chip::DeviceLayer::SystemLayer().ScheduleWork(StopEventLoop, nullptr) == CHIP_NO_ERROR); | ||
chip::DeviceLayer::PlatformMgr().RunEventLoop(); | ||
NL_TEST_ASSERT(inSuite, *callCount == 2); | ||
delete callCount; | ||
} | ||
|
||
// Test Suite | ||
|
||
/** | ||
* Test Suite. It lists all the test functions. | ||
*/ | ||
// clang-format off | ||
static const nlTest sTests[] = | ||
{ | ||
NL_TEST_DEF("System::TestScheduleWorkTwice", CheckScheduleWorkTwice), | ||
NL_TEST_SENTINEL() | ||
}; | ||
// clang-format on | ||
|
||
static int TestSetup(void * aContext); | ||
static int TestTeardown(void * aContext); | ||
|
||
// clang-format off | ||
static nlTestSuite kTheSuite = | ||
{ | ||
"chip-system-schedule-work", | ||
&sTests[0], | ||
TestSetup, | ||
TestTeardown | ||
}; | ||
// clang-format on | ||
|
||
/** | ||
* Set up the test suite. | ||
*/ | ||
static int TestSetup(void * aContext) | ||
{ | ||
if (chip::Platform::MemoryInit() != CHIP_NO_ERROR) | ||
{ | ||
return FAILURE; | ||
} | ||
|
||
if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) | ||
return FAILURE; | ||
|
||
return (SUCCESS); | ||
} | ||
|
||
/** | ||
* Tear down the test suite. | ||
* Free memory reserved at TestSetup. | ||
*/ | ||
static int TestTeardown(void * aContext) | ||
{ | ||
chip::DeviceLayer::PlatformMgr().Shutdown(); | ||
chip::Platform::MemoryShutdown(); | ||
return (SUCCESS); | ||
} | ||
|
||
int TestSystemScheduleWork(void) | ||
{ | ||
// Run test suit againt one lContext. | ||
nlTestRunner(&kTheSuite, nullptr); | ||
|
||
return nlTestRunnerStats(&kTheSuite); | ||
} | ||
|
||
CHIP_REGISTER_TEST_SUITE(TestSystemScheduleWork) |
Oops, something went wrong.