Skip to content

Commit 73a91e0

Browse files
Basic test refactoring
1 parent 24418b4 commit 73a91e0

File tree

1 file changed

+56
-35
lines changed

1 file changed

+56
-35
lines changed

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

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,57 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
18-
19-
/*
20-
* Tests is to measure the accuracy of Thread::wait() over a period of time
21-
*
22-
*
23-
* 1) DUT would start to update callback_trigger_count every milli sec
24-
* 2) Host would query what is current count base_time, Device responds by the callback_trigger_count
25-
* 3) Host after waiting for measurement stretch. It will query for device time again final_time.
26-
* 4) Host computes the drift considering base_time, final_time, transport delay and measurement stretch
27-
* 5) Finally host send the results back to device pass/fail based on tolerance.
28-
* 6) More details on tests can be found in timing_drift_auto.py
29-
*
30-
*/
31-
3217
#include "mbed.h"
3318
#include "greentea-client/test_env.h"
34-
#include "rtos.h"
19+
#include "utest/utest.h"
3520
#include "unity/unity.h"
3621

3722
#if defined(MBED_RTOS_SINGLE_THREAD)
3823
#error [NOT_SUPPORTED] test not supported
3924
#endif
4025

41-
#define TEST_STACK_SIZE 1024
26+
using utest::v1::Case;
27+
28+
#define TEST_STACK_SIZE 256
4229
#define ONE_MILLI_SEC 1000
4330

44-
volatile uint32_t callback_trigger_count = 0;
31+
volatile uint32_t elapsed_time_ms = 0;
32+
static const int test_timeout = 40;
4533

46-
static const int test_timeout = 240;
47-
bool test_result = false;
4834

49-
void update_tick_thread() {
35+
void update_tick_thread(Mutex *mutex)
36+
{
5037
while (true) {
5138
Thread::wait(1);
52-
++callback_trigger_count;
39+
mutex->lock();
40+
++elapsed_time_ms;
41+
mutex->unlock();
5342
}
5443
}
5544

56-
void gt_comm_wait_thread() {
45+
46+
/** Tests is to measure the accuracy of Thread::wait() over a period of time
47+
48+
Given
49+
a thread updating elapsed_time_ms every milli sec
50+
and host script for time measurement accuracy check (More details on tests can be found in timing_drift_auto.py)
51+
When host query what is current count base_time
52+
Then Device responds by the elapsed_time_ms
53+
When host query what is current count final_time
54+
Then Device responds by the elapsed_time_ms
55+
When host computes the drift considering base_time, final_time, transport delay and measurement stretch
56+
Then host send the results back to device pass/fail based on tolerance
57+
*/
58+
void test(void)
59+
{
5760
char _key[11] = { };
5861
char _value[128] = { };
5962
int expected_key = 1;
63+
Mutex mutex;
64+
uint32_t elapsed_time;
65+
66+
Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
67+
tick_thread.start(callback(update_tick_thread, &mutex));
6068

6169
greentea_send_kv("timing_drift_check_start", 0);
6270

@@ -65,28 +73,41 @@ void gt_comm_wait_thread() {
6573
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
6674
expected_key = strcmp(_key, "base_time");
6775
} while (expected_key);
68-
greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC);
76+
77+
mutex.lock();
78+
elapsed_time = elapsed_time_ms;
79+
mutex.unlock();
80+
// send base_time
81+
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
6982

7083
// wait for 2nd signal from host
7184
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
72-
greentea_send_kv(_key, callback_trigger_count * ONE_MILLI_SEC);
85+
86+
mutex.lock();
87+
elapsed_time = elapsed_time_ms;
88+
mutex.unlock();
89+
// send final_time
90+
greentea_send_kv(_key, elapsed_time * ONE_MILLI_SEC);
7391

7492
//get the results from host
7593
greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value));
7694

77-
if (strcmp("pass", _key) == 0) {
78-
test_result = true;
79-
}
95+
TEST_ASSERT_EQUAL_STRING_MESSAGE("pass", _key,"Host side script reported a fail...");
8096
}
8197

82-
int main() {
98+
Case cases[] = {
99+
Case("Test Thread::wait accuracy", test)
100+
};
101+
102+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
103+
{
83104
GREENTEA_SETUP(test_timeout, "timing_drift_auto");
84-
Thread tick_thread(osPriorityHigh, TEST_STACK_SIZE);
85-
Thread gt_conn_thread(osPriorityNormal, TEST_STACK_SIZE);
105+
return utest::v1::greentea_test_setup_handler(number_of_cases);
106+
}
86107

87-
tick_thread.start(update_tick_thread);
88-
gt_conn_thread.start(gt_comm_wait_thread);
89-
gt_conn_thread.join();
108+
utest::v1::Specification specification(greentea_test_setup, cases);
90109

91-
GREENTEA_TESTSUITE_RESULT(test_result);
110+
int main()
111+
{
112+
utest::v1::Harness::run(specification);
92113
}

0 commit comments

Comments
 (0)