|
| 1 | + |
| 2 | +/* mbed Microcontroller Library |
| 3 | + * Copyright (c) 2018 ARM Limited |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "greentea-client/test_env.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "utest/utest.h" |
| 21 | + |
| 22 | +#include "mbed.h" |
| 23 | + |
| 24 | +#if !defined(MBED_THREAD_STATS_ENABLED) |
| 25 | +#error [NOT_SUPPORTED] test not supported |
| 26 | +#endif |
| 27 | + |
| 28 | +using namespace utest::v1; |
| 29 | + |
| 30 | +static EventFlags ef; |
| 31 | +static int32_t counter = 0; |
| 32 | + |
| 33 | +#define TEST_STACK_SIZE 320 |
| 34 | +#define FLAG_SIGNAL_DEC 0x2 |
| 35 | +#define MAX_THREAD_STATS 0x8 |
| 36 | + |
| 37 | +void decrement_on_event() |
| 38 | +{ |
| 39 | + uint32_t ret = ef.wait_all(FLAG_SIGNAL_DEC); |
| 40 | + TEST_ASSERT_FALSE(ret & osFlagsError); |
| 41 | + TEST_ASSERT_EQUAL(FLAG_SIGNAL_DEC, ret); |
| 42 | + counter--; |
| 43 | +} |
| 44 | + |
| 45 | +void increment_with_delay() |
| 46 | +{ |
| 47 | + while (1) { |
| 48 | + counter++; |
| 49 | + Thread::wait(500); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +void test_case_single_thread_stats() |
| 54 | +{ |
| 55 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 56 | + int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 57 | + Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1"); |
| 58 | + t1.start(increment_with_delay); |
| 59 | + |
| 60 | + // Read stats |
| 61 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 62 | + TEST_ASSERT_EQUAL(1, (count - old_count)); |
| 63 | + |
| 64 | + for (int i = 0; i < count; i++) { |
| 65 | + if (0 == strcmp(stats[i].name, "Th1")) { |
| 66 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size); |
| 67 | + TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + t1.terminate(); |
| 73 | + delete[] stats; |
| 74 | +} |
| 75 | + |
| 76 | +#define SINGLE_ELEMENT 1 |
| 77 | +void test_case_less_count() |
| 78 | +{ |
| 79 | + // Default Mbed OS has 3 threads |
| 80 | + mbed_stats_thread_t stats; |
| 81 | + int count = mbed_stats_thread_get_each(&stats, SINGLE_ELEMENT); |
| 82 | + TEST_ASSERT_EQUAL(SINGLE_ELEMENT, count); |
| 83 | +} |
| 84 | + |
| 85 | +void test_case_multi_threads_blocked() |
| 86 | +{ |
| 87 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 88 | + int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 89 | + |
| 90 | + Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1"); |
| 91 | + Thread t2(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th2"); |
| 92 | + t1.start(increment_with_delay); |
| 93 | + t2.start(decrement_on_event); |
| 94 | + |
| 95 | + // Read stats |
| 96 | + |
| 97 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 98 | + TEST_ASSERT_EQUAL(2, (count - old_count)); |
| 99 | + for (int i = 0; i < count; i++) { |
| 100 | + if (0 == strcmp(stats[i].name, "Th2")) { |
| 101 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size); |
| 102 | + TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority); |
| 103 | + TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state); |
| 104 | + } else if (0 == strcmp (stats[i].name, "Th1")) { |
| 105 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size); |
| 106 | + TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].priority); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Signal blocked thread |
| 111 | + uint32_t ret = ef.set(FLAG_SIGNAL_DEC); |
| 112 | + TEST_ASSERT_FALSE(ret & osFlagsError); |
| 113 | + |
| 114 | + Thread::wait(100); |
| 115 | + |
| 116 | + count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 117 | + TEST_ASSERT_EQUAL(1, (count - old_count)); |
| 118 | + |
| 119 | + t1.terminate(); |
| 120 | + delete[] stats; |
| 121 | +} |
| 122 | + |
| 123 | +void test_case_multi_threads_terminate() |
| 124 | +{ |
| 125 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 126 | + int old_count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 127 | + |
| 128 | + Thread t1(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th1"); |
| 129 | + Thread t2(osPriorityNormal2, TEST_STACK_SIZE, NULL, "Th2"); |
| 130 | + t2.start(increment_with_delay); |
| 131 | + t1.start(decrement_on_event); |
| 132 | + |
| 133 | + // Read stats |
| 134 | + |
| 135 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 136 | + TEST_ASSERT_EQUAL(2, (count - old_count)); |
| 137 | + |
| 138 | + for (int i = 0; i < count; i++) { |
| 139 | + if (0 == strcmp(stats[i].name, "Th2")) { |
| 140 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size); |
| 141 | + TEST_ASSERT_EQUAL(osPriorityNormal2, stats[i].priority); |
| 142 | + } else if (0 == strcmp(stats[i].name, "Th1")) { |
| 143 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].stack_size); |
| 144 | + TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].priority); |
| 145 | + TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].state); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + t1.terminate(); |
| 150 | + t2.terminate(); |
| 151 | + |
| 152 | + count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 153 | + TEST_ASSERT_EQUAL(count, old_count); |
| 154 | + |
| 155 | + delete[] stats; |
| 156 | +} |
| 157 | + |
| 158 | +Case cases[] = { |
| 159 | + Case("Single Thread Stats", test_case_single_thread_stats), |
| 160 | + Case("Less count value", test_case_less_count), |
| 161 | + Case("Multiple Threads blocked", test_case_multi_threads_blocked), |
| 162 | + Case("Multiple Threads terminate", test_case_multi_threads_terminate), |
| 163 | +}; |
| 164 | + |
| 165 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 166 | +{ |
| 167 | + GREENTEA_SETUP(20, "default_auto"); |
| 168 | + return greentea_test_setup_handler(number_of_cases); |
| 169 | +} |
| 170 | + |
| 171 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 172 | + |
| 173 | +int main() |
| 174 | +{ |
| 175 | + Harness::run(specification); |
| 176 | +} |
0 commit comments