Skip to content

Commit

Permalink
Move sleeping methods to TimeUtils
Browse files Browse the repository at this point in the history
* Remove dependency between chip-tool and tests
  • Loading branch information
jlatusek committed Jan 3, 2024
1 parent 827f6ca commit d2e527b
Show file tree
Hide file tree
Showing 25 changed files with 110 additions and 144 deletions.
2 changes: 1 addition & 1 deletion examples/chip-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static_library("chip-tool-utils") {
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/app/icd/client:manager",
"${chip_root}/src/app/server",
"${chip_root}/src/app/tests/suites/commands/interaction_model",
"${chip_root}/src/app/commands/interaction_model",
"${chip_root}/src/controller/data_model",
"${chip_root}/src/credentials:file_attestation_trust_store",
"${chip_root}/src/lib",
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/clusters/ClusterCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include <app/tests/suites/commands/interaction_model/InteractionModel.h>
#include <app/commands/interaction_model/InteractionModel.h>

#include "DataModelLogger.h"
#include "ModelCommand.h"
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/clusters/ReportCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include <app/tests/suites/commands/interaction_model/InteractionModel.h>
#include <app/commands/interaction_model/InteractionModel.h>

#include "DataModelLogger.h"
#include "ModelCommand.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include <app/tests/suites/commands/interaction_model/InteractionModel.h>
#include <app/commands/interaction_model/InteractionModel.h>

#include "DataModelLogger.h"
#include "ModelCommand.h"
Expand Down
2 changes: 1 addition & 1 deletion examples/darwin-framework-tool/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ executable("darwin-framework-tool") {
"${chip_root}/src/app/tests/suites/commands/delay",

# IM is needed for MTRError
"${chip_root}/src/app/tests/suites/commands/interaction_model",
"${chip_root}/src/app/commands/interaction_model",

# Log is needed by tests UserPrompt and Log
"${chip_root}/src/app/tests/suites/commands/log",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class TestCommandBridge : public CHIPCommandBridge,
void Wait()
{
if (mDelayInMs.HasValue()) {
chip::test_utils::SleepMillis(mDelayInMs.Value());
chip::SleepMillis(mDelayInMs.Value());
}
};

Expand Down
3 changes: 1 addition & 2 deletions examples/placeholder/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ executable("chip-${chip_tests_zap_config}") {
"${chip_root}/src/app/tests/suites/credentials:dac_provider",
"${chip_root}/src/app/tests/suites/pics",
"${chip_root}/src/lib",
"${chip_root}/src/lib/support:testing", # For sleepMillis. TODO: this is
# odd and should be fixed
"${chip_root}/src/lib/support",
"${chip_root}/src/platform/logging:headers",
"${chip_root}/third_party/jsoncpp",
]
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ chip_data_model("tv-casting-common") {
deps = [
"${chip_root}/examples/common/tracing:commandline",
"${chip_root}/src/app/icd/client:manager",
"${chip_root}/src/app/tests/suites/commands/interaction_model",
"${chip_root}/src/app/commands/interaction_model",
"${chip_root}/src/lib/support/jsontlv",
"${chip_root}/src/tracing",
"${chip_root}/src/tracing/json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ static_library("interaction_model") {
public_deps = [
"${chip_root}/src/app",
"${chip_root}/src/lib/support",
"${chip_root}/src/lib/support:testing",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <app/ReadClient.h>
#include <app/WriteClient.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/UnitTestUtils.h>
#include <lib/support/TimeUtils.h>

inline constexpr uint8_t kMaxAllowedPaths = 64;

Expand Down Expand Up @@ -258,7 +258,7 @@ class InteractionModelCommands

if (mRepeatDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(mRepeatDelayInMs.Value());
chip::SleepMillis(mRepeatDelayInMs.Value());
}
}
return CHIP_NO_ERROR;
Expand Down Expand Up @@ -410,7 +410,7 @@ class InteractionModelWriter

if (mRepeatDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(mRepeatDelayInMs.Value());
chip::SleepMillis(mRepeatDelayInMs.Value());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/TestTimedHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void TestTimedHandler::TestFollowingMessageTooSlow(nlTestSuite * aSuite, void *
NL_TEST_ASSERT(aSuite, delegate.mError == CHIP_NO_ERROR);

// Sleep for > 50ms so we miss our time window.
chip::test_utils::SleepMillis(75);
chip::SleepMillis(75);

// Send an empty payload, which will error out but not with the
// UNSUPPORTED_ACCESS status we expect if we miss our timeout.
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/suites/include/TestRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class TestRunner

if (mDelayInMs.HasValue())
{
chip::test_utils::SleepMillis(mDelayInMs.Value());
chip::SleepMillis(mDelayInMs.Value());
}

// Ensure we increment mTestIndex before we start running the relevant
Expand Down
4 changes: 2 additions & 2 deletions src/controller/tests/TestReadChunking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ void TestReadChunking::TestDynamicEndpoint(nlTestSuite * apSuite, void * apConte
NL_TEST_ASSERT(apSuite, readCallback.mOnReportEnd);
}

chip::test_utils::SleepMillis(SecondsToMilliseconds(2));
chip::SecondsToMilliseconds(2));

// Destroying the read client will terminate the subscription transaction.
ctx.DrainAndServiceIO();
Expand Down Expand Up @@ -1045,7 +1045,7 @@ void TestReadChunking::TestSetDirtyBetweenChunks(nlTestSuite * apSuite, void * a
}
}

chip::test_utils::SleepMillis(SecondsToMilliseconds(3));
chip::SleepMillis(SecondsToMilliseconds(3));

// Destroying the read client will terminate the subscription transaction.
ctx.DrainAndServiceIO();
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/Framework/Matter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@
039546A52991E132006D42A8 /* InteractionModel.cpp */,
);
name = interaction_model;
path = ../../src/app/tests/suites/commands/interaction_model;
path = ../../src/app/commands/interaction_model;
sourceTree = "<group>";
};
039546A72991E185006D42A8 /* delay */ = {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/tests/TestTLV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4365,7 +4365,7 @@ static void TLVReaderFuzzTest(nlTestSuite * inSuite, void * inContext)

memcpy(fuzzedData, Encoding1, sizeof(fuzzedData));

now = chip::test_utils::TimeMonotonicMillis();
now = chip::TimeMonotonicMillis();
endTime = now + sFuzzTestDurationMillis + 1;

srand(static_cast<unsigned int>(now));
Expand Down Expand Up @@ -4408,7 +4408,7 @@ static void TLVReaderFuzzTest(nlTestSuite * inSuite, void * inContext)
return;
}

now = chip::test_utils::TimeMonotonicMillis();
now = chip::TimeMonotonicMillis();
if (now >= endTime)
return;

Expand Down
2 changes: 0 additions & 2 deletions src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ static_library("testing") {
"UnitTestExtendedAssertions.h",
"UnitTestRegistration.cpp",
"UnitTestRegistration.h",
"UnitTestUtils.cpp",
"UnitTestUtils.h",
]

public_deps = [
Expand Down
59 changes: 59 additions & 0 deletions src/lib/support/TimeUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
#include <lib/core/CHIPCore.h>
#include <lib/support/SafeInt.h>

#include <platform/CHIPDeviceConfig.h>
#if CHIP_DEVICE_LAYER_TARGET_EFR32 || CHIP_DEVICE_LAYER_TARGET_AMEBA
#include <FreeRTOS.h>
#include <task.h>
#else
#include <time.h>
#include <unistd.h>
#endif

#include "TimeUtils.h"

namespace chip {
Expand Down Expand Up @@ -591,4 +600,54 @@ bool UnixEpochToChipEpochTime(uint32_t unixEpochTimeSeconds, uint32_t & outChipE
return true;
}

#if CHIP_DEVICE_LAYER_TARGET_EFR32 || CHIP_DEVICE_LAYER_TARGET_AMEBA

namespace {

constexpr uint64_t TicksToMillis(uint32_t ticks)
{
const uint64_t seconds = ticks / configTICK_RATE_HZ;
const uint64_t millis_ticks = ticks - (seconds * configTICK_RATE_HZ);
const uint64_t millis = (((millis_ticks * 1000) + (configTICK_RATE_HZ / 2)) / configTICK_RATE_HZ);
return (seconds * 1000) + millis;
}

} // namespace

uint64_t TimeMonotonicMillis()
{
return TicksToMillis(xTaskGetTickCount());
}

void SleepMillis(uint64_t millisecs)
{
uint32_t ticks = static_cast<uint32_t>(millisecs / portTICK_PERIOD_MS);
vTaskDelay(ticks == 0 ? 1 : ticks); // delay at least 1 tick
}

void SleepMicros(uint64_t microsecs)
{
// FreeRTOS currently only sleep for intervals of 1ms
SleepMillis((microsecs + 500) / 1000);
}

#else

void SleepMicros(uint64_t microsecs)
{
usleep(static_cast<useconds_t>(microsecs));
}

void SleepMillis(uint64_t millisecs)
{
SleepMicros(millisecs * 1000);
}

uint64_t TimeMonotonicMillis()
{
return static_cast<uint64_t>(time(nullptr) * 1000);
}

#endif

} // namespace chip
25 changes: 25 additions & 0 deletions src/lib/support/TimeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,29 @@ inline uint64_t SecondsToMilliseconds(uint32_t seconds)
return SecondsToMilliseconds(seconds);
}

/**
* @def SleepMicros
*
* @brief
* Sleep for the specified number of microseconds.
*/

void SleepMicros(uint64_t microsecs);

/**
* @def SleepMillis
*
* @brief
* Sleep for the specified number of milliseconds.
*/
void SleepMillis(uint64_t millisecs);

/**
* @def TimeMonotonicMillis
*
* @brief
* Return the current time in milliseconds since an arbitrary epoch.
*/
uint64_t TimeMonotonicMillis();

} // namespace chip
84 changes: 0 additions & 84 deletions src/lib/support/UnitTestUtils.cpp

This file was deleted.

30 changes: 0 additions & 30 deletions src/lib/support/UnitTestUtils.h

This file was deleted.

Loading

0 comments on commit d2e527b

Please sign in to comment.