Skip to content

Commit 8a5f58b

Browse files
committed
Convert Timeout test to Chrono
Now tests only the Chrono `attach(duration)` method, not the deprecated `attach` and `attach_us` calls.
1 parent 25fad5d commit 8a5f58b

File tree

3 files changed

+55
-106
lines changed

3 files changed

+55
-106
lines changed

TESTS/mbed_drivers/lp_timeout/main.cpp

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,37 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
3737
}
3838

3939
Case cases[] = {
40-
Case("Callback called once (attach)", test_single_call<AttachTester<LowPowerTimeout> >),
41-
Case("Callback called once (attach_us)", test_single_call<AttachUSTester<LowPowerTimeout> >),
40+
Case("Callback called once", test_single_call<LowPowerTimeout>),
4241

43-
Case("Callback not called when cancelled (attach)", test_cancel<AttachTester<LowPowerTimeout> >),
44-
Case("Callback not called when cancelled (attach_us)", test_cancel<AttachUSTester<LowPowerTimeout> >),
42+
Case("Callback not called when cancelled", test_cancel<LowPowerTimeout>),
4543

46-
Case("Callback override (attach)", test_override<AttachTester<LowPowerTimeout> >),
47-
Case("Callback override (attach_us)", test_override<AttachUSTester<LowPowerTimeout> >),
44+
Case("Callback override", test_override<LowPowerTimeout>),
4845

49-
Case("Multiple timeouts running in parallel (attach)", test_multiple<AttachTester<LowPowerTimeout> >),
50-
Case("Multiple timeouts running in parallel (attach_us)", test_multiple<AttachUSTester<LowPowerTimeout> >),
46+
Case("Multiple timeouts running in parallel", test_multiple<LowPowerTimeout>),
5147

52-
Case("Zero delay (attach)", test_no_wait<AttachTester<LowPowerTimeout> >),
53-
Case("Zero delay (attach_us)", test_no_wait<AttachUSTester<LowPowerTimeout> >),
48+
Case("Zero delay", test_no_wait<LowPowerTimeout>),
5449

55-
Case("Reschedule in callback (attach)", test_reschedule<AttachTester<LowPowerTimeout> >),
56-
Case("Reschedule in callback (attach_us)", test_reschedule<AttachUSTester<LowPowerTimeout> >),
50+
Case("Reschedule in callback", test_reschedule<LowPowerTimeout>),
5751

58-
Case("10 ms delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 10000, SHORT_DELTA_US>,
59-
greentea_failure_handler),
60-
Case("10 ms delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 10000, SHORT_DELTA_US>,
52+
Case("10 ms delay accuracy", test_delay_accuracy<LowPowerTimeout, 10000, SHORT_DELTA_US>,
6153
greentea_failure_handler),
6254

63-
Case("1 s delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
64-
greentea_failure_handler),
65-
Case("1 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
55+
Case("1 s delay accuracy (attach)", test_delay_accuracy<LowPowerTimeout, 1000000, LONG_DELTA_US>,
6656
greentea_failure_handler),
6757

68-
Case("5 s delay accuracy (attach)", test_delay_accuracy<AttachTester<LowPowerTimeout>, 5000000, LONG_DELTA_US>,
69-
greentea_failure_handler),
70-
Case("5 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<LowPowerTimeout>, 5000000, LONG_DELTA_US>,
58+
Case("5 s delay accuracy (attach)", test_delay_accuracy<LowPowerTimeout, 5000000, LONG_DELTA_US>,
7159
greentea_failure_handler),
7260

7361
#if DEVICE_SLEEP
74-
Case("1 s delay during sleep (attach)", test_sleep<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
75-
greentea_failure_handler),
76-
Case("1 s delay during sleep (attach_us)", test_sleep<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
62+
Case("1 s delay during sleep (attach)", test_sleep<LowPowerTimeout, 1000000, LONG_DELTA_US>,
7763
greentea_failure_handler),
7864

79-
Case("1 s delay during deepsleep (attach)", test_deepsleep<AttachTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
80-
greentea_failure_handler),
81-
Case("1 s delay during deepsleep (attach_us)", test_deepsleep<AttachUSTester<LowPowerTimeout>, 1000000, LONG_DELTA_US>,
65+
Case("1 s delay during deepsleep (attach)", test_deepsleep<LowPowerTimeout, 1000000, LONG_DELTA_US>,
8266
greentea_failure_handler),
8367
#endif
8468

8569
#if !defined(SKIP_TIME_DRIFT_TESTS)
86-
Case("Timing drift (attach)", test_drift<AttachTester<LowPowerTimeout> >),
87-
Case("Timing drift (attach_us)", test_drift<AttachUSTester<LowPowerTimeout> >),
70+
Case("Timing drift (attach)", test_drift<LowPowerTimeout>),
8871
#endif
8972
};
9073

TESTS/mbed_drivers/timeout/main.cpp

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,49 +33,34 @@ utest::v1::status_t greentea_failure_handler(const Case *const source, const fai
3333
}
3434

3535
Case cases[] = {
36-
Case("Callback called once (attach)", test_single_call<AttachTester<Timeout> >),
37-
Case("Callback called once (attach_us)", test_single_call<AttachUSTester<Timeout> >),
36+
Case("Callback called once", test_single_call<Timeout>),
3837

39-
Case("Callback not called when cancelled (attach)", test_cancel<AttachTester<Timeout> >),
40-
Case("Callback not called when cancelled (attach_us)", test_cancel<AttachUSTester<Timeout> >),
38+
Case("Callback not called when cancelled", test_cancel<Timeout>),
4139

42-
Case("Callback override (attach)", test_override<AttachTester<Timeout> >),
43-
Case("Callback override (attach_us)", test_override<AttachUSTester<Timeout> >),
40+
Case("Callback override", test_override<Timeout>),
4441

45-
Case("Multiple timeouts running in parallel (attach)", test_multiple<AttachTester<Timeout> >),
46-
Case("Multiple timeouts running in parallel (attach_us)", test_multiple<AttachUSTester<Timeout> >),
42+
Case("Multiple timeouts running in parallel", test_multiple<Timeout>),
4743

48-
Case("Zero delay (attach)", test_no_wait<AttachTester<Timeout> >),
49-
Case("Zero delay (attach_us)", test_no_wait<AttachUSTester<Timeout> >),
44+
Case("Zero delay", test_no_wait<Timeout>),
5045

51-
Case("Reschedule in callback (attach)", test_reschedule<AttachTester<Timeout> >),
52-
Case("Reschedule in callback (attach_us)", test_reschedule<AttachUSTester<Timeout> >),
46+
Case("Reschedule in callback", test_reschedule<Timeout>),
5347

54-
Case("10 ms delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 10000, SHORT_DELTA_US>,
55-
greentea_failure_handler),
56-
Case("10 ms delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 10000, SHORT_DELTA_US>,
48+
Case("10 ms delay accuracy", test_delay_accuracy<Timeout, 10000, SHORT_DELTA_US>,
5749
greentea_failure_handler),
5850

59-
Case("1 s delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 1000000, LONG_DELTA_US>,
60-
greentea_failure_handler),
61-
Case("1 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 1000000, LONG_DELTA_US>,
51+
Case("1 s delay accuracy", test_delay_accuracy<Timeout, 1000000, LONG_DELTA_US>,
6252
greentea_failure_handler),
6353

64-
Case("5 s delay accuracy (attach)", test_delay_accuracy<AttachTester<Timeout>, 5000000, LONG_DELTA_US>,
65-
greentea_failure_handler),
66-
Case("5 s delay accuracy (attach_us)", test_delay_accuracy<AttachUSTester<Timeout>, 5000000, LONG_DELTA_US>,
54+
Case("5 s delay accuracy", test_delay_accuracy<Timeout, 5000000, LONG_DELTA_US>,
6755
greentea_failure_handler),
6856

6957
#if DEVICE_SLEEP
70-
Case("1 s delay during sleep (attach)", test_sleep<AttachTester<Timeout>, 1000000, LONG_DELTA_US>,
71-
greentea_failure_handler),
72-
Case("1 s delay during sleep (attach_us)", test_sleep<AttachUSTester<Timeout>, 1000000, LONG_DELTA_US>,
58+
Case("1 s delay during sleep", test_sleep<Timeout, 1000000, LONG_DELTA_US>,
7359
greentea_failure_handler),
7460
#endif
7561

7662
#if !defined(SKIP_TIME_DRIFT_TESTS)
77-
Case("Timing drift (attach)", test_drift<AttachTester<Timeout> >),
78-
Case("Timing drift (attach_us)", test_drift<AttachUSTester<Timeout> >),
63+
Case("Timing drift", test_drift<Timeout>),
7964
#endif
8065
};
8166

TESTS/mbed_drivers/timeout/timeout_tests.h

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#include "mbed.h"
2121
#include "unity/unity.h"
2222

23+
using namespace std::chrono;
24+
2325
#define NUM_TIMEOUTS 16
24-
#define DRIFT_TEST_PERIOD_US 10000
26+
const microseconds DRIFT_TEST_PERIOD = 10ms;
2527

26-
const float TEST_DELAY_S = 0.01;
27-
const uint32_t TEST_DELAY_MS = 1000.0F * TEST_DELAY_S;
28-
const us_timestamp_t TEST_DELAY_US = 1000000.0F * TEST_DELAY_S;
28+
const milliseconds TEST_DELAY = 10ms;
2929

3030
/* Timeouts are quite arbitrary due to large number of boards with varying level of accuracy */
3131
#define LONG_DELTA_US (100000)
@@ -41,24 +41,6 @@ void cnt_callback(volatile uint32_t *cnt)
4141
(*cnt)++;
4242
}
4343

44-
template<typename TimeoutType>
45-
class AttachTester: public TimeoutType {
46-
public:
47-
void attach_callback(Callback<void()> func, us_timestamp_t delay_us)
48-
{
49-
TimeoutType::attach(func, (float) delay_us / 1000000.0f);
50-
}
51-
};
52-
53-
template<typename TimeoutType>
54-
class AttachUSTester: public TimeoutType {
55-
public:
56-
void attach_callback(Callback<void()> func, us_timestamp_t delay_us)
57-
{
58-
TimeoutType::attach_us(func, delay_us);
59-
}
60-
};
61-
6244
/** Template for tests: callback called once
6345
*
6446
* Test callback called once
@@ -77,15 +59,15 @@ void test_single_call(void)
7759
Semaphore sem(0, 1);
7860
T timeout;
7961

80-
timeout.attach_callback(mbed::callback(sem_callback, &sem), TEST_DELAY_US);
62+
timeout.attach(mbed::callback(sem_callback, &sem), TEST_DELAY);
8163

8264
bool acquired = sem.try_acquire();
8365
TEST_ASSERT_FALSE(acquired);
8466

85-
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
67+
acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
8668
TEST_ASSERT_TRUE(acquired);
8769

88-
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
70+
acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
8971
TEST_ASSERT_FALSE(acquired);
9072

9173
timeout.detach();
@@ -109,13 +91,13 @@ void test_cancel(void)
10991
Semaphore sem(0, 1);
11092
T timeout;
11193

112-
timeout.attach_callback(mbed::callback(sem_callback, &sem), 2.0f * TEST_DELAY_US);
94+
timeout.attach(mbed::callback(sem_callback, &sem), 2 * TEST_DELAY);
11395

114-
bool acquired = sem.try_acquire_for(TEST_DELAY_MS);
96+
bool acquired = sem.try_acquire_for(TEST_DELAY);
11597
TEST_ASSERT_FALSE(acquired);
11698
timeout.detach();
11799

118-
acquired = sem.try_acquire_for(TEST_DELAY_MS + 2);
100+
acquired = sem.try_acquire_for(TEST_DELAY + 2ms);
119101
TEST_ASSERT_FALSE(acquired);
120102
}
121103

@@ -142,13 +124,13 @@ void test_override(void)
142124
Semaphore sem2(0, 1);
143125
T timeout;
144126

145-
timeout.attach_callback(mbed::callback(sem_callback, &sem1), 2.0f * TEST_DELAY_US);
127+
timeout.attach(mbed::callback(sem_callback, &sem1), 2 * TEST_DELAY);
146128

147-
bool acquired = sem1.try_acquire_for(TEST_DELAY_MS);
129+
bool acquired = sem1.try_acquire_for(TEST_DELAY);
148130
TEST_ASSERT_FALSE(acquired);
149-
timeout.attach_callback(mbed::callback(sem_callback, &sem2), 2.0f * TEST_DELAY_US);
131+
timeout.attach(mbed::callback(sem_callback, &sem2), 2 * TEST_DELAY);
150132

151-
acquired = sem2.try_acquire_for(2 * TEST_DELAY_MS + 2);
133+
acquired = sem2.try_acquire_for(2 * TEST_DELAY + 2ms);
152134
TEST_ASSERT_TRUE(acquired);
153135
acquired = sem1.try_acquire();
154136
TEST_ASSERT_FALSE(acquired);
@@ -176,9 +158,9 @@ void test_multiple(void)
176158
volatile uint32_t callback_count = 0;
177159
T timeouts[NUM_TIMEOUTS];
178160
for (size_t i = 0; i < NUM_TIMEOUTS; i++) {
179-
timeouts[i].attach_callback(mbed::callback(cnt_callback, &callback_count), TEST_DELAY_US);
161+
timeouts[i].attach(mbed::callback(cnt_callback, &callback_count), TEST_DELAY);
180162
}
181-
ThisThread::sleep_for(TEST_DELAY_MS + 2);
163+
ThisThread::sleep_for(TEST_DELAY + 2ms);
182164
TEST_ASSERT_EQUAL(NUM_TIMEOUTS, callback_count);
183165
}
184166

@@ -200,7 +182,7 @@ void test_no_wait(void)
200182
for (int i = 0; i < 100; i++) {
201183
Semaphore sem(0, 1);
202184
T timeout;
203-
timeout.attach_callback(mbed::callback(sem_callback, &sem), 0ULL);
185+
timeout.attach(mbed::callback(sem_callback, &sem), 0s);
204186
bool sem_acquired = sem.try_acquire();
205187
TEST_ASSERT_EQUAL(true, sem_acquired);
206188
timeout.detach();
@@ -227,11 +209,11 @@ void test_delay_accuracy(void)
227209
Timer timer;
228210

229211
timer.start();
230-
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
212+
timeout.attach(mbed::callback(sem_callback, &sem), microseconds(delay_us));
231213

232214
sem.acquire();
233215
timer.stop();
234-
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
216+
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
235217

236218
timeout.detach();
237219
}
@@ -262,15 +244,15 @@ void test_sleep(void)
262244

263245
sleep_manager_lock_deep_sleep();
264246
timer.start();
265-
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
247+
timeout.attach(mbed::callback(sem_callback, &sem), microseconds(delay_us));
266248

267249
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
268250
TEST_ASSERT_FALSE_MESSAGE(deep_sleep_allowed, "Deep sleep should be disallowed");
269251
sem.acquire();
270252
timer.stop();
271253

272254
sleep_manager_unlock_deep_sleep();
273-
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
255+
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
274256

275257
timeout.detach();
276258
}
@@ -316,17 +298,17 @@ void test_deepsleep(void)
316298
* hardware buffers are empty. However, such an API does not exist now,
317299
* so we'll use the ThisThread::sleep_for() function for now.
318300
*/
319-
ThisThread::sleep_for(20);
301+
ThisThread::sleep_for(20ms);
320302

321303
timer.start();
322-
timeout.attach_callback(mbed::callback(sem_callback, &sem), delay_us);
304+
timeout.attach(mbed::callback(sem_callback, &sem), microseconds(delay_us));
323305

324306
bool deep_sleep_allowed = sleep_manager_can_deep_sleep_test_check();
325307
TEST_ASSERT_TRUE_MESSAGE(deep_sleep_allowed, "Deep sleep should be allowed");
326308
sem.acquire();
327309
timer.stop();
328310

329-
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.read_high_resolution_us());
311+
TEST_ASSERT_UINT64_WITHIN(delta_us, delay_us, timer.elapsed_time().count());
330312

331313
timeout.detach();
332314
}
@@ -336,14 +318,14 @@ void test_deepsleep(void)
336318
template<typename TimeoutTesterType>
337319
class TimeoutDriftTester {
338320
public:
339-
TimeoutDriftTester(us_timestamp_t period = 1000) :
321+
TimeoutDriftTester(microseconds period = 1ms) :
340322
_callback_count(0), _period(period), _timeout()
341323
{
342324
}
343325

344326
void reschedule_callback(void)
345327
{
346-
_timeout.attach_callback(mbed::callback(this, &TimeoutDriftTester::reschedule_callback), _period);
328+
_timeout.attach(mbed::callback(this, &TimeoutDriftTester::reschedule_callback), _period);
347329
_callback_count++;
348330
}
349331

@@ -359,7 +341,7 @@ class TimeoutDriftTester {
359341

360342
private:
361343
volatile uint32_t _callback_count;
362-
us_timestamp_t _period;
344+
microseconds _period;
363345
TimeoutTesterType _timeout;
364346
};
365347

@@ -376,11 +358,10 @@ class TimeoutDriftTester {
376358
template<typename T>
377359
void test_reschedule(void)
378360
{
379-
volatile uint32_t callback_count = 0;
380-
TimeoutDriftTester<T> timeout;
361+
TimeoutDriftTester<T> timeout(TEST_DELAY);
381362

382363
timeout.reschedule_callback();
383-
ThisThread::sleep_for(TEST_DELAY_MS * 5);
364+
ThisThread::sleep_for(TEST_DELAY * 5);
384365
TEST_ASSERT(timeout.get_callback_count() >= 3);
385366
}
386367

@@ -410,7 +391,7 @@ void test_drift(void)
410391
char _key[11] = { };
411392
char _value[128] = { };
412393
int expected_key = 1;
413-
TimeoutDriftTester<T> timeout(DRIFT_TEST_PERIOD_US);
394+
TimeoutDriftTester<T> timeout(DRIFT_TEST_PERIOD);
414395

415396
greentea_send_kv("timing_drift_check_start", 0);
416397
timeout.reschedule_callback();
@@ -421,11 +402,11 @@ void test_drift(void)
421402
expected_key = strcmp(_key, "base_time");
422403
} while (expected_key);
423404

424-
greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD_US);
405+
greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD.count());
425406

426407
// wait for 2nd signal from host
427408
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
428-
greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD_US);
409+
greentea_send_kv(_key, timeout.get_callback_count() * DRIFT_TEST_PERIOD.count());
429410

430411
timeout.detach_callback();
431412

0 commit comments

Comments
 (0)