|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Project CHIP Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <pw_unit_test/framework.h> |
| 18 | +#include <system/SystemConfig.h> |
| 19 | + |
| 20 | +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && !CHIP_SYSTEM_CONFIG_USE_DISPATCH |
| 21 | + |
| 22 | +#include <lib/support/CodeUtils.h> |
| 23 | +#include <platform/CHIPDeviceLayer.h> |
| 24 | + |
| 25 | +#include <functional> |
| 26 | +#include <string> |
| 27 | + |
| 28 | +using namespace chip; |
| 29 | +using namespace chip::System::Clock; |
| 30 | +using namespace chip::System::Clock::Literals; |
| 31 | + |
| 32 | +class TestEventLoopHandler : public ::testing::Test |
| 33 | +{ |
| 34 | +public: |
| 35 | + static void SetUpTestSuite() |
| 36 | + { |
| 37 | + ASSERT_EQ(Platform::MemoryInit(), CHIP_NO_ERROR); |
| 38 | + ASSERT_EQ(DeviceLayer::PlatformMgr().InitChipStack(), CHIP_NO_ERROR); |
| 39 | + } |
| 40 | + |
| 41 | + static void TearDownTestSuite() |
| 42 | + { |
| 43 | + DeviceLayer::PlatformMgr().Shutdown(); |
| 44 | + Platform::MemoryShutdown(); |
| 45 | + } |
| 46 | + |
| 47 | + static System::LayerSocketsLoop & SystemLayer() { return static_cast<System::LayerSocketsLoop &>(DeviceLayer::SystemLayer()); } |
| 48 | + |
| 49 | + template <class Lambda> |
| 50 | + static void Schedule(Timeout delay, Lambda lambda) |
| 51 | + { |
| 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)); |
| 60 | + } |
| 61 | + |
| 62 | + template <class Lambda> |
| 63 | + static void ScheduleNextTick(Lambda lambda) |
| 64 | + { |
| 65 | + // ScheduleLambda is based on device events, which are greedily processed until the |
| 66 | + // queue is empty, so we can't use it to wait for the next event loop tick. Just use |
| 67 | + // a timer with a very short delay. |
| 68 | + Schedule(1_ms, lambda); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +TEST_F(TestEventLoopHandler, EventLoopHandlerSequence) |
| 73 | +{ |
| 74 | + struct : public System::EventLoopHandler |
| 75 | + { |
| 76 | + std::string trace; |
| 77 | + Timestamp PrepareEvents(Timestamp now) override |
| 78 | + { |
| 79 | + trace.append("P"); |
| 80 | + return Timestamp::max(); |
| 81 | + } |
| 82 | + void HandleEvents() override { trace.append("H"); } |
| 83 | + } loopHandler; |
| 84 | + |
| 85 | + ScheduleNextTick([&] { |
| 86 | + loopHandler.trace.append("1"); |
| 87 | + SystemLayer().AddLoopHandler(loopHandler); |
| 88 | + loopHandler.trace.append("A"); |
| 89 | + ScheduleNextTick([&] { // "P" |
| 90 | + loopHandler.trace.append("2"); |
| 91 | + ScheduleNextTick([&] { // "H", "P" |
| 92 | + loopHandler.trace.append("3"); |
| 93 | + SystemLayer().RemoveLoopHandler(loopHandler); |
| 94 | + loopHandler.trace.append("R"); |
| 95 | + ScheduleNextTick([&] { |
| 96 | + loopHandler.trace.append("4"); |
| 97 | + DeviceLayer::PlatformMgr().StopEventLoopTask(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| 104 | + EXPECT_EQ(loopHandler.trace, std::string("1AP2HP3R4")); |
| 105 | +} |
| 106 | + |
| 107 | +TEST_F(TestEventLoopHandler, EventLoopHandlerWake) |
| 108 | +{ |
| 109 | + struct : public System::EventLoopHandler |
| 110 | + { |
| 111 | + Timestamp startTimestamp = System::SystemClock().GetMonotonicTimestamp(); |
| 112 | + Timestamp wakeTimestamp = Timestamp::max(); |
| 113 | + |
| 114 | + Timestamp PrepareEvents(Timestamp now) override { return now + 400_ms; } |
| 115 | + void HandleEvents() override |
| 116 | + { |
| 117 | + // StartTimer() (called by Schedule()) is liable to causes an immediate |
| 118 | + // wakeup via Signal(), so ignore this call if it's only been a few ms. |
| 119 | + auto now = System::SystemClock().GetMonotonicTimestamp(); |
| 120 | + if (now - startTimestamp >= 100_ms) |
| 121 | + { |
| 122 | + wakeTimestamp = now; |
| 123 | + DeviceLayer::PlatformMgr().StopEventLoopTask(); |
| 124 | + } |
| 125 | + } |
| 126 | + } loopHandler; |
| 127 | + |
| 128 | + SystemLayer().AddLoopHandler(loopHandler); |
| 129 | + Schedule(1000_ms, [] { DeviceLayer::PlatformMgr().StopEventLoopTask(); }); |
| 130 | + chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| 131 | + SystemLayer().RemoveLoopHandler(loopHandler); |
| 132 | + |
| 133 | + Timestamp sleepDuration = loopHandler.wakeTimestamp - loopHandler.startTimestamp; |
| 134 | + EXPECT_GE(sleepDuration.count(), 400u); // loopHandler requested wake-up after 400ms |
| 135 | + EXPECT_LE(sleepDuration.count(), 500u); // allow some slack for test machine load |
| 136 | +} |
| 137 | + |
| 138 | +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS && !CHIP_SYSTEM_CONFIG_USE_DISPATCH |
0 commit comments