From 379953f6d0502b26a3029ebc2986a2cb013a2c45 Mon Sep 17 00:00:00 2001 From: Vincent Coubard Date: Mon, 2 Aug 2021 17:02:34 +0100 Subject: [PATCH] Use usleep instead of sleep/nanosleep on Mbed OS. (#8393) * Use usleep instead of nanoseep in tests to increase portability. nanosleep isn't available with newlib which is used by many embedded targets. * Use C++-style cast - static_cast replacement Co-authored-by: ATmobica --- .../tests/TestReliableMessageProtocol.cpp | 9 +------- src/platform/tests/TestPlatformMgr.cpp | 2 +- src/platform/tests/TestPlatformTime.cpp | 22 ++++--------------- .../secure_channel/tests/TestPASESession.cpp | 9 +------- 4 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index a46ef8303d6663..268ab5213981dd 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -183,14 +183,7 @@ class MockSessionEstablishmentDelegate : public ExchangeDelegate void test_os_sleep_ms(uint64_t millisecs) { - struct timespec sleep_time; - uint64_t s = millisecs / 1000; - - millisecs -= s * 1000; - sleep_time.tv_sec = static_cast(s); - sleep_time.tv_nsec = static_cast(millisecs * 1000000); - - nanosleep(&sleep_time, nullptr); + usleep(static_cast(millisecs * 1000)); } void CheckAddClearRetrans(nlTestSuite * inSuite, void * inContext) diff --git a/src/platform/tests/TestPlatformMgr.cpp b/src/platform/tests/TestPlatformMgr.cpp index 5a6ebae34f79fa..c825964d9bec9f 100644 --- a/src/platform/tests/TestPlatformMgr.cpp +++ b/src/platform/tests/TestPlatformMgr.cpp @@ -98,7 +98,7 @@ static bool sleepRan; static void SleepSome(intptr_t) { - sleep(1); + usleep(static_cast(1 * 1000 * 1000)); sleepRan = true; } diff --git a/src/platform/tests/TestPlatformTime.cpp b/src/platform/tests/TestPlatformTime.cpp index d5cb55fe1cb9d9..8f245f736b5460 100644 --- a/src/platform/tests/TestPlatformTime.cpp +++ b/src/platform/tests/TestPlatformTime.cpp @@ -70,28 +70,14 @@ static const struct time_test_vector test_vector_system_time_us[] = { #include -void test_os_sleep_ms(uint64_t millisecs) +void test_os_sleep_us(uint64_t microsecs) { - struct timespec sleep_time; - int s = millisecs / 1000; - - millisecs -= s * 1000; - sleep_time.tv_sec = s; - sleep_time.tv_nsec = millisecs * 1000000; - - nanosleep(&sleep_time, nullptr); + usleep(static_cast(microsecs)); } -void test_os_sleep_us(uint64_t microsecs) +void test_os_sleep_ms(uint64_t millisecs) { - struct timespec sleep_time; - int s = microsecs / 1000000; - - microsecs -= s * 1000000; - sleep_time.tv_sec = s; - sleep_time.tv_nsec = microsecs * 1000; - - nanosleep(&sleep_time, nullptr); + test_os_sleep_us(millisecs * 1000); } // ================================= diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index a6b0eab50df90a..d57b7f0f9b3d93 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -44,14 +44,7 @@ using TestContext = chip::Test::MessagingContext; static void test_os_sleep_ms(uint64_t millisecs) { - struct timespec sleep_time; - uint64_t s = millisecs / 1000; - - millisecs -= s * 1000; - sleep_time.tv_sec = static_cast(s); - sleep_time.tv_nsec = static_cast(millisecs * 1000000); - - nanosleep(&sleep_time, nullptr); + usleep(static_cast(millisecs * 1000)); } class PASETestLoopbackTransport : public Test::LoopbackTransport