Skip to content

Commit 0b4b2af

Browse files
authored
Merge pull request #12901 from kjbracey-arm/mailchrono
Rationalise Mail/Queue/MemoryPool timing APIs
2 parents a4f6645 + 1814463 commit 0b4b2af

File tree

7 files changed

+418
-338
lines changed

7 files changed

+418
-338
lines changed

TESTS/mbedmicro-rtos-mbed/MemoryPool/main.cpp

Lines changed: 66 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@
2323
#error [NOT_SUPPORTED] MemoryPool test cases require a RTOS to run.
2424
#else
2525

26+
#define TEST_ASSERT_DURATION_WITHIN(delta, expected, actual) \
27+
do { \
28+
using ct = std::common_type_t<decltype(delta), decltype(expected), decltype(actual)>; \
29+
TEST_ASSERT_INT_WITHIN(ct(delta).count(), ct(expected).count(), ct(actual).count()); \
30+
} while (0)
31+
32+
#define TEST_ASSERT_TIME_POINT_WITHIN(delta, expected, actual) \
33+
do { \
34+
using ct_tp = std::common_type_t<decltype(expected), decltype(actual)>; \
35+
using ct = std::common_type_t<decltype(delta), ct_tp::duration>; \
36+
TEST_ASSERT_INT_WITHIN(ct(delta).count(), ct(expected.time_since_epoch()).count(), ct(actual.time_since_epoch()).count()); \
37+
} while (0)
38+
2639
using namespace utest::v1;
40+
using namespace std::chrono;
2741

2842
#define THREAD_STACK_SIZE 512
29-
#define TEST_TIMEOUT 50
43+
#define TEST_TIMEOUT 50ms
3044

3145
/* Enum used to select block allocation method. */
3246
typedef enum {
@@ -68,7 +82,7 @@ void comp_set(COMPLEX_TYPE *object, int a, char b, int c)
6882
object->c = c;
6983
}
7084

71-
/* Template for functional tests for alloc(), calloc() functions
85+
/* Template for functional tests for try_alloc(), try_calloc() functions
7286
* of MemoryPool object.
7387
*
7488
* Given MemoryPool object of the specified type and queue size has
@@ -89,9 +103,9 @@ void test_mem_pool_alloc_success(AllocType atype)
89103
for (i = 0; i < numOfEntries; i++) {
90104
/* Allocate memory block. */
91105
if (atype == ALLOC) {
92-
p_blocks[i] = mem_pool.alloc();
106+
p_blocks[i] = mem_pool.try_alloc();
93107
} else {
94-
p_blocks[i] = mem_pool.calloc();
108+
p_blocks[i] = mem_pool.try_calloc();
95109
}
96110

97111
/* Show that memory pool block has been allocated. */
@@ -112,7 +126,7 @@ void test_mem_pool_alloc_success(AllocType atype)
112126
}
113127
}
114128

115-
/* Template for functional tests for alloc(), calloc() functions
129+
/* Template for functional tests for try_alloc(), try_calloc() functions
116130
* of MemoryPool object.
117131
*
118132
* Complex memory pool block type is used.
@@ -135,9 +149,9 @@ void test_mem_pool_alloc_success_complex(AllocType atype)
135149
for (i = 0; i < numOfEntries; i++) {
136150
/* Allocate memory block. */
137151
if (atype == ALLOC) {
138-
p_blocks[i] = mem_pool.alloc();
152+
p_blocks[i] = mem_pool.try_alloc();
139153
} else {
140-
p_blocks[i] = mem_pool.calloc();
154+
p_blocks[i] = mem_pool.try_calloc();
141155
}
142156

143157
/* Show that memory pool block has been allocated. */
@@ -158,7 +172,7 @@ void test_mem_pool_alloc_success_complex(AllocType atype)
158172
}
159173
}
160174

161-
/* Template for functional tests for alloc(), calloc() functions
175+
/* Template for functional tests for try_alloc(), try_calloc() functions
162176
* of MemoryPool object.
163177
*
164178
* Given MemoryPool has already max number of blocks allocated from the pool.
@@ -177,9 +191,9 @@ void test_mem_pool_alloc_fail(AllocType atype)
177191
/* Allocate all available blocks. */
178192
for (i = 0; i < numOfEntries; i++) {
179193
if (atype == ALLOC) {
180-
p_blocks[i] = mem_pool.alloc();
194+
p_blocks[i] = mem_pool.try_alloc();
181195
} else {
182-
p_blocks[i] = mem_pool.calloc();
196+
p_blocks[i] = mem_pool.try_calloc();
183197
}
184198

185199
/* Show that memory pool block has been allocated. */
@@ -188,9 +202,9 @@ void test_mem_pool_alloc_fail(AllocType atype)
188202

189203
/* There are no more blocks available. Try to allocate another block. */
190204
if (atype == ALLOC) {
191-
p_extra_block = mem_pool.alloc();
205+
p_extra_block = mem_pool.try_alloc();
192206
} else {
193-
p_extra_block = mem_pool.calloc();
207+
p_extra_block = mem_pool.try_calloc();
194208
}
195209

196210
/* Show that memory pool block has NOT been allocated. */
@@ -216,9 +230,9 @@ void test_mem_pool_free_success(AllocType atype)
216230
/* Allocate all available blocks. */
217231
for (i = 0; i < numOfEntries; i++) {
218232
if (atype == ALLOC) {
219-
p_blocks[i] = mem_pool.alloc();
233+
p_blocks[i] = mem_pool.try_alloc();
220234
} else {
221-
p_blocks[i] = mem_pool.calloc();
235+
p_blocks[i] = mem_pool.try_calloc();
222236
}
223237

224238
/* Show that memory pool block has been allocated. */
@@ -234,7 +248,7 @@ void test_mem_pool_free_success(AllocType atype)
234248
}
235249
}
236250

237-
/* Template for functional tests for alloc(), calloc() functions
251+
/* Template for functional tests for try_alloc(), try_calloc() functions
238252
* of MemoryPool object.
239253
*
240254
* Basic memory pool block type is used.
@@ -256,9 +270,9 @@ void test_mem_pool_free_realloc_last(AllocType atype)
256270
/* Allocate all available blocks. */
257271
for (i = 0; i < numOfEntries; i++) {
258272
if (atype == ALLOC) {
259-
p_blocks[i] = mem_pool.alloc();
273+
p_blocks[i] = mem_pool.try_alloc();
260274
} else {
261-
p_blocks[i] = mem_pool.calloc();
275+
p_blocks[i] = mem_pool.try_calloc();
262276
}
263277

264278
/* Init block. */
@@ -276,9 +290,9 @@ void test_mem_pool_free_realloc_last(AllocType atype)
276290

277291
/* Try to allocate another block (one block is now available). */
278292
if (atype == ALLOC) {
279-
p_blocks[numOfEntries - 1] = mem_pool.alloc();
293+
p_blocks[numOfEntries - 1] = mem_pool.try_alloc();
280294
} else {
281-
p_blocks[numOfEntries - 1] = mem_pool.calloc();
295+
p_blocks[numOfEntries - 1] = mem_pool.try_calloc();
282296
}
283297

284298
/* Show that memory pool block has been now allocated. */
@@ -290,7 +304,7 @@ void test_mem_pool_free_realloc_last(AllocType atype)
290304
}
291305
}
292306

293-
/* Template for functional tests for alloc(), calloc() functions
307+
/* Template for functional tests for try_alloc(), try_calloc() functions
294308
* of MemoryPool object.
295309
*
296310
* Complex memory pool block type is used.
@@ -312,9 +326,9 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
312326
/* Allocate all available blocks. */
313327
for (i = 0; i < numOfEntries; i++) {
314328
if (atype == ALLOC) {
315-
p_blocks[i] = mem_pool.alloc();
329+
p_blocks[i] = mem_pool.try_alloc();
316330
} else {
317-
p_blocks[i] = mem_pool.calloc();
331+
p_blocks[i] = mem_pool.try_calloc();
318332
}
319333

320334
/* Init block. */
@@ -332,9 +346,9 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
332346

333347
/* Try to allocate another block (one block is now available). */
334348
if (atype == ALLOC) {
335-
p_blocks[numOfEntries - 1] = mem_pool.alloc();
349+
p_blocks[numOfEntries - 1] = mem_pool.try_alloc();
336350
} else {
337-
p_blocks[numOfEntries - 1] = mem_pool.calloc();
351+
p_blocks[numOfEntries - 1] = mem_pool.try_calloc();
338352
}
339353

340354
/* Show that memory pool block has been now allocated. */
@@ -346,7 +360,7 @@ void test_mem_pool_free_realloc_last_complex(AllocType atype)
346360
}
347361
}
348362

349-
/* Template for functional tests for alloc(), calloc() functions
363+
/* Template for functional tests for try_alloc(), try_calloc() functions
350364
* of MemoryPool object.
351365
*
352366
* Basic memory pool block type is used.
@@ -368,9 +382,9 @@ void test_mem_pool_free_realloc_first(AllocType atype)
368382
/* Allocate all available blocks. */
369383
for (i = 0; i < numOfEntries; i++) {
370384
if (atype == ALLOC) {
371-
p_blocks[i] = mem_pool.alloc();
385+
p_blocks[i] = mem_pool.try_alloc();
372386
} else {
373-
p_blocks[i] = mem_pool.calloc();
387+
p_blocks[i] = mem_pool.try_calloc();
374388
}
375389

376390
/* Init block. */
@@ -388,9 +402,9 @@ void test_mem_pool_free_realloc_first(AllocType atype)
388402

389403
/* Try to allocate another block (one block is now available). */
390404
if (atype == ALLOC) {
391-
p_blocks[0] = mem_pool.alloc();
405+
p_blocks[0] = mem_pool.try_alloc();
392406
} else {
393-
p_blocks[0] = mem_pool.calloc();
407+
p_blocks[0] = mem_pool.try_calloc();
394408
}
395409

396410
/* Show that memory pool block has been now allocated. */
@@ -402,7 +416,7 @@ void test_mem_pool_free_realloc_first(AllocType atype)
402416
}
403417
}
404418

405-
/* Template for functional tests for alloc(), calloc() functions
419+
/* Template for functional tests for try_alloc(), try_calloc() functions
406420
* of MemoryPool object.
407421
*
408422
* Complex memory pool block type is used.
@@ -424,9 +438,9 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
424438
/* Allocate all available blocks. */
425439
for (i = 0; i < numOfEntries; i++) {
426440
if (atype == ALLOC) {
427-
p_blocks[i] = mem_pool.alloc();
441+
p_blocks[i] = mem_pool.try_alloc();
428442
} else {
429-
p_blocks[i] = mem_pool.calloc();
443+
p_blocks[i] = mem_pool.try_calloc();
430444
}
431445

432446
/* Init block. */
@@ -444,9 +458,9 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
444458

445459
/* Try to allocate another block (one block is now available). */
446460
if (atype == ALLOC) {
447-
p_blocks[0] = mem_pool.alloc();
461+
p_blocks[0] = mem_pool.try_alloc();
448462
} else {
449-
p_blocks[0] = mem_pool.calloc();
463+
p_blocks[0] = mem_pool.try_calloc();
450464
}
451465

452466
/* Show that memory pool block has been now allocated. */
@@ -458,7 +472,7 @@ void test_mem_pool_free_realloc_first_complex(AllocType atype)
458472
}
459473
}
460474

461-
/* Test alloc timeout
475+
/* Test try_alloc_for/try_alloc_until timeout
462476
*
463477
* Given a pool with one slot for int data
464478
* When a thread tries to allocate two blocks with @ TEST_TIMEOUT timeout
@@ -471,18 +485,18 @@ void test_mem_pool_timeout()
471485
Timer timer;
472486
timer.start();
473487

474-
int *item = mem_pool.alloc_for(TEST_TIMEOUT);
488+
int *item = mem_pool.try_alloc_for(TEST_TIMEOUT);
475489
TEST_ASSERT_NOT_NULL(item);
476-
TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, 0, timer.read_us());
490+
TEST_ASSERT_DURATION_WITHIN(TEST_TIMEOUT / 10, 0ms, timer.elapsed_time());
477491

478-
item = mem_pool.alloc_for(TEST_TIMEOUT);
492+
item = mem_pool.try_alloc_for(TEST_TIMEOUT);
479493
TEST_ASSERT_NULL(item);
480-
TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, TEST_TIMEOUT * 1000, timer.read_us());
494+
TEST_ASSERT_DURATION_WITHIN(TEST_TIMEOUT / 10, TEST_TIMEOUT, timer.elapsed_time());
481495

482-
uint64_t end_time = Kernel::get_ms_count() + TEST_TIMEOUT;
483-
item = mem_pool.alloc_until(end_time);
496+
auto end_time = Kernel::Clock::now() + TEST_TIMEOUT;
497+
item = mem_pool.try_alloc_until(end_time);
484498
TEST_ASSERT_NULL(item);
485-
TEST_ASSERT_UINT64_WITHIN(TEST_TIMEOUT * 100, end_time, Kernel::get_ms_count());
499+
TEST_ASSERT_TIME_POINT_WITHIN(TEST_TIMEOUT / 10, end_time, Kernel::Clock::now());
486500
}
487501

488502
namespace {
@@ -516,18 +530,18 @@ void test_mem_pool_waitforever()
516530
Timer timer;
517531
timer.start();
518532

519-
int *item = pool.alloc_for(osWaitForever);
533+
int *item = pool.try_alloc_for(Kernel::wait_for_u32_forever);
520534
TEST_ASSERT_NOT_NULL(item);
521-
TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, 0, timer.read_us());
535+
TEST_ASSERT_DURATION_WITHIN(TEST_TIMEOUT / 10, 0ms, timer.elapsed_time());
522536

523537
struct free_capture to_free;
524538
to_free.pool = &pool;
525539
to_free.item = item;
526540
t.start(callback(free_int_item, &to_free));
527541

528-
item = pool.alloc_for(osWaitForever);
542+
item = pool.try_alloc_for(Kernel::wait_for_u32_forever);
529543
TEST_ASSERT_EQUAL(item, to_free.item);
530-
TEST_ASSERT_UINT32_WITHIN(TEST_TIMEOUT * 100, TEST_TIMEOUT * 1000, timer.read_us());
544+
TEST_ASSERT_DURATION_WITHIN(TEST_TIMEOUT / 10, TEST_TIMEOUT, timer.elapsed_time());
531545

532546
t.join();
533547
}
@@ -632,12 +646,12 @@ void test_mem_pool_alloc_fail_wrapper()
632646
}
633647

634648
Case cases[] = {
635-
Case("Test: alloc()/calloc() - success, 4 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<int, 1>),
636-
Case("Test: alloc()/calloc() - success, 4 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<int, 3>),
637-
Case("Test: alloc()/calloc() - success, 1 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<char, 1>),
638-
Case("Test: alloc()/calloc() - success, 1 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<char, 3>),
639-
Case("Test: alloc()/calloc() - success, complex b_type, q_size equal to 1.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 1>),
640-
Case("Test: alloc()/calloc() - success, complex b_type, q_size equal to 3.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 3>),
649+
Case("Test: try_alloc()/try_calloc() - success, 4 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<int, 1>),
650+
Case("Test: try_alloc()/try_calloc() - success, 4 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<int, 3>),
651+
Case("Test: try_alloc()/try_calloc() - success, 1 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<char, 1>),
652+
Case("Test: try_alloc()/try_calloc() - success, 1 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<char, 3>),
653+
Case("Test: try_alloc()/try_calloc() - success, complex b_type, q_size equal to 1.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 1>),
654+
Case("Test: try_alloc()/try_calloc() - success, complex b_type, q_size equal to 3.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 3>),
641655

642656
Case("Test: free() - success, 4 bytes b_type, q_size equal to 1.", test_mem_pool_free_success_wrapper<int, 1>),
643657
Case("Test: free() - success, 4 bytes b_type, q_size equal to 3.", test_mem_pool_free_success_wrapper<int, 3>),

0 commit comments

Comments
 (0)