From 10063152cee349bed8a7366ba2a9569e08a9d10c Mon Sep 17 00:00:00 2001 From: Vivien Nicolas Date: Wed, 23 Feb 2022 18:03:41 +0100 Subject: [PATCH] [YAML] Make busyWait a dedicated method instead of always generating it (#15416) * [YAML] Make busyWait a dedicated method instead of always generating it * Update generated tests content --- .../tests/partials/test_cluster.zapt | 9 +---- .../suites/commands/delay/DelayCommands.cpp | 11 +++++ .../suites/commands/delay/DelayCommands.h | 3 ++ .../chip-tool/zap-generated/test/Commands.h | 40 ++++--------------- 4 files changed, 24 insertions(+), 39 deletions(-) diff --git a/examples/chip-tool/templates/tests/partials/test_cluster.zapt b/examples/chip-tool/templates/tests/partials/test_cluster.zapt index ee24a8f8fd9ff0..6ed5b188809583 100644 --- a/examples/chip-tool/templates/tests/partials/test_cluster.zapt +++ b/examples/chip-tool/templates/tests/partials/test_cluster.zapt @@ -288,13 +288,8 @@ class {{filename}}: public TestCommand {{~#*inline "maybeWait"}} {{#if busyWaitMs}} - { - using namespace chip::System::Clock::Literals; - // Busy-wait for {{busyWaitMs}} milliseconds. - auto & clock = chip::System::SystemClock(); - auto start = clock.GetMonotonicTimestamp(); - while (clock.GetMonotonicTimestamp() - start < {{busyWaitMs}}_ms); - } + using namespace chip::System::Clock::Literals; + BusyWaitFor({{busyWaitMs}}_ms); {{/if}} {{/inline~}} diff --git a/src/app/tests/suites/commands/delay/DelayCommands.cpp b/src/app/tests/suites/commands/delay/DelayCommands.cpp index 1aa2374a1cbd5b..97a59c246c3c2b 100644 --- a/src/app/tests/suites/commands/delay/DelayCommands.cpp +++ b/src/app/tests/suites/commands/delay/DelayCommands.cpp @@ -63,3 +63,14 @@ CHIP_ERROR DelayCommands::RunInternal(const char * command) VerifyOrReturnError(system(command) == 0, CHIP_ERROR_INTERNAL); return ContinueOnChipMainThread(CHIP_NO_ERROR); } + +CHIP_ERROR DelayCommands::BusyWaitFor(chip::System::Clock::Milliseconds32 durationInMs) +{ + auto & clock = chip::System::SystemClock(); + auto start = clock.GetMonotonicTimestamp(); + while (clock.GetMonotonicTimestamp() - start < durationInMs) + { + // nothing to do. + }; + return CHIP_NO_ERROR; +} diff --git a/src/app/tests/suites/commands/delay/DelayCommands.h b/src/app/tests/suites/commands/delay/DelayCommands.h index b7200042b898e5..8aaccd91f844a8 100644 --- a/src/app/tests/suites/commands/delay/DelayCommands.h +++ b/src/app/tests/suites/commands/delay/DelayCommands.h @@ -37,6 +37,9 @@ class DelayCommands CHIP_ERROR WaitForCommissionableAdvertisement(); CHIP_ERROR WaitForOperationalAdvertisement(); + // Busy-wait for a given duration in milliseconds + CHIP_ERROR BusyWaitFor(chip::System::Clock::Milliseconds32 durationInMs); + private: static void OnWaitForMsFn(chip::System::Layer * systemLayer, void * context); CHIP_ERROR RunInternal(const char * command); diff --git a/zzz_generated/chip-tool/zap-generated/test/Commands.h b/zzz_generated/chip-tool/zap-generated/test/Commands.h index a12fd503ef449b..fe70fbb65bf4d8 100644 --- a/zzz_generated/chip-tool/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/test/Commands.h @@ -73539,14 +73539,8 @@ class TestClusterComplexTypes : public TestCommand ReturnErrorOnFailure( chip::Controller::InvokeCommand(mDevices[kIdentityAlpha], this, success, failure, endpoint, request, 1)); - { - using namespace chip::System::Clock::Literals; - // Busy-wait for 100 milliseconds. - auto & clock = chip::System::SystemClock(); - auto start = clock.GetMonotonicTimestamp(); - while (clock.GetMonotonicTimestamp() - start < 100_ms) - ; - } + using namespace chip::System::Clock::Literals; + BusyWaitFor(100_ms); return CHIP_NO_ERROR; } @@ -73604,14 +73598,8 @@ class TestClusterComplexTypes : public TestCommand ReturnErrorOnFailure( chip::Controller::InvokeCommand(mDevices[kIdentityAlpha], this, success, failure, endpoint, request, 1)); - { - using namespace chip::System::Clock::Literals; - // Busy-wait for 100 milliseconds. - auto & clock = chip::System::SystemClock(); - auto start = clock.GetMonotonicTimestamp(); - while (clock.GetMonotonicTimestamp() - start < 100_ms) - ; - } + using namespace chip::System::Clock::Literals; + BusyWaitFor(100_ms); return CHIP_NO_ERROR; } @@ -73706,14 +73694,8 @@ class TestClusterComplexTypes : public TestCommand ReturnErrorOnFailure(cluster.WriteAttribute( timedWriteBooleanArgument, this, OnSuccessCallback_10, OnFailureCallback_10, 1)); - { - using namespace chip::System::Clock::Literals; - // Busy-wait for 100 milliseconds. - auto & clock = chip::System::SystemClock(); - auto start = clock.GetMonotonicTimestamp(); - while (clock.GetMonotonicTimestamp() - start < 100_ms) - ; - } + using namespace chip::System::Clock::Literals; + BusyWaitFor(100_ms); return CHIP_NO_ERROR; } @@ -73853,14 +73835,8 @@ class TestClusterComplexTypes : public TestCommand ReturnErrorOnFailure(cluster.WriteAttribute( booleanArgument, this, OnSuccessCallback_16, OnFailureCallback_16, 1)); - { - using namespace chip::System::Clock::Literals; - // Busy-wait for 100 milliseconds. - auto & clock = chip::System::SystemClock(); - auto start = clock.GetMonotonicTimestamp(); - while (clock.GetMonotonicTimestamp() - start < 100_ms) - ; - } + using namespace chip::System::Clock::Literals; + BusyWaitFor(100_ms); return CHIP_NO_ERROR; }