Skip to content

Commit

Permalink
Use usleep instead of sleep/nanosleep on Mbed OS. (#8393)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Aug 31, 2021
1 parent d3ad011 commit c0060a8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 35 deletions.
9 changes: 1 addition & 8 deletions src/messaging/tests/TestReliableMessageProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<time_t>(s);
sleep_time.tv_nsec = static_cast<long>(millisecs * 1000000);

nanosleep(&sleep_time, nullptr);
usleep(static_cast<useconds_t>(millisecs * 1000));
}

void CheckAddClearRetrans(nlTestSuite * inSuite, void * inContext)
Expand Down
2 changes: 1 addition & 1 deletion src/platform/tests/TestPlatformMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static bool sleepRan;

static void SleepSome(intptr_t)
{
sleep(1);
usleep(static_cast<useconds_t>(1 * 1000 * 1000));
sleepRan = true;
}

Expand Down
22 changes: 4 additions & 18 deletions src/platform/tests/TestPlatformTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,28 +70,14 @@ static const struct time_test_vector test_vector_system_time_us[] = {

#include <unistd.h>

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<useconds_t>(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);
}

// =================================
Expand Down
9 changes: 1 addition & 8 deletions src/protocols/secure_channel/tests/TestPASESession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<time_t>(s);
sleep_time.tv_nsec = static_cast<long>(millisecs * 1000000);

nanosleep(&sleep_time, nullptr);
usleep(static_cast<useconds_t>(millisecs * 1000));
}

class PASETestLoopbackTransport : public Test::LoopbackTransport
Expand Down

0 comments on commit c0060a8

Please sign in to comment.