|
| 1 | +/* |
| 2 | + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * 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, WITHOUT |
| 13 | + * 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 | +#include "mbed.h" |
| 18 | +#include "greentea-client/test_env.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "utest/utest.h" |
| 21 | +#include "mbed_stats.h" |
| 22 | +#include <stdlib.h> |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +#if !defined(MBED_THREAD_STATS_ENABLED) |
| 26 | + #warning [NOT_SUPPORTED] test not supported |
| 27 | +#endif |
| 28 | + |
| 29 | +using namespace utest::v1; |
| 30 | + |
| 31 | +static EventFlags ef; |
| 32 | +static int32_t counter = 0; |
| 33 | + |
| 34 | +#define TEST_STACK_SIZE 320 |
| 35 | +#define FLAG_SIGNAL_DEC 0x2 |
| 36 | +#define MAX_THREAD_STATS 0x8 |
| 37 | + |
| 38 | +void decrement_on_event() |
| 39 | +{ |
| 40 | + uint32_t ret = ef.wait_all(FLAG_SIGNAL_DEC); |
| 41 | + TEST_ASSERT_FALSE(ret & osFlagsError); |
| 42 | + TEST_ASSERT_EQUAL(FLAG_SIGNAL_DEC, ret); |
| 43 | + counter--; |
| 44 | +} |
| 45 | + |
| 46 | +void increment_with_delay() |
| 47 | +{ |
| 48 | + while (1) { |
| 49 | + counter++; |
| 50 | + Thread::wait(500); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void test_case_single_thread_stats() |
| 55 | +{ |
| 56 | + Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1"); |
| 57 | + t1.start(increment_with_delay); |
| 58 | + |
| 59 | + // Read stats |
| 60 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 61 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 62 | + TEST_ASSERT_EQUAL(4, count); |
| 63 | + |
| 64 | + for (int i = 0; i < count; i++) { |
| 65 | + if (0 == strcmp (stats[i].thread_name, "Th1")) { |
| 66 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size); |
| 67 | + TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority); |
| 68 | + break; |
| 69 | + } |
| 70 | + } |
| 71 | + delete[] stats; |
| 72 | + t1.terminate(); |
| 73 | +} |
| 74 | + |
| 75 | +void test_case_less_count() |
| 76 | +{ |
| 77 | + // Default Mbed OS has 3 threads |
| 78 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[2]; |
| 79 | + int count = mbed_stats_thread_get_each(stats, 2); |
| 80 | + TEST_ASSERT_EQUAL(2, count); |
| 81 | + delete[] stats; |
| 82 | +} |
| 83 | + |
| 84 | +void test_case_multi_threads_blocked() |
| 85 | +{ |
| 86 | + Thread t1(osPriorityNormal, TEST_STACK_SIZE, NULL, "Th1"); |
| 87 | + Thread t2(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th2"); |
| 88 | + t1.start(increment_with_delay); |
| 89 | + t2.start(decrement_on_event); |
| 90 | + |
| 91 | + // Read stats |
| 92 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 93 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 94 | + TEST_ASSERT_EQUAL(5, count); |
| 95 | + |
| 96 | + for (int i = 0; i < count; i++) { |
| 97 | + if (0 == strcmp (stats[i].thread_name, "Th2")) { |
| 98 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size); |
| 99 | + TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority); |
| 100 | + TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].thread_state); |
| 101 | + } else if (0 == strcmp (stats[i].thread_name, "Th1")) { |
| 102 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size); |
| 103 | + TEST_ASSERT_EQUAL(osPriorityNormal, stats[i].thread_priority); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Signal blocked thread |
| 108 | + uint32_t ret = ef.set(FLAG_SIGNAL_DEC); |
| 109 | + TEST_ASSERT_FALSE(ret & osFlagsError); |
| 110 | + |
| 111 | + Thread::wait(100); |
| 112 | + |
| 113 | + count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 114 | + TEST_ASSERT_EQUAL(4, count); |
| 115 | + |
| 116 | + delete[] stats; |
| 117 | + t1.terminate(); |
| 118 | +} |
| 119 | + |
| 120 | +void test_case_multi_threads_terminate() |
| 121 | +{ |
| 122 | + Thread t1(osPriorityNormal1, TEST_STACK_SIZE, NULL, "Th1"); |
| 123 | + Thread t2(osPriorityNormal2, TEST_STACK_SIZE, NULL, "Th2"); |
| 124 | + t2.start(increment_with_delay); |
| 125 | + t1.start(decrement_on_event); |
| 126 | + |
| 127 | + // Read stats |
| 128 | + mbed_stats_thread_t *stats = new mbed_stats_thread_t[MAX_THREAD_STATS]; |
| 129 | + int count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 130 | + TEST_ASSERT_EQUAL(5, count); |
| 131 | + |
| 132 | + for (int i = 0; i < count; i++) { |
| 133 | + if (0 == strcmp (stats[i].thread_name, "Th2")) { |
| 134 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size); |
| 135 | + TEST_ASSERT_EQUAL(osPriorityNormal2, stats[i].thread_priority); |
| 136 | + } else if (0 == strcmp (stats[i].thread_name, "Th1")) { |
| 137 | + TEST_ASSERT_EQUAL(TEST_STACK_SIZE, stats[i].thread_stack_size); |
| 138 | + TEST_ASSERT_EQUAL(osPriorityNormal1, stats[i].thread_priority); |
| 139 | + TEST_ASSERT_EQUAL(osThreadBlocked, stats[i].thread_state); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + t1.terminate(); |
| 144 | + t2.terminate(); |
| 145 | + |
| 146 | + count = mbed_stats_thread_get_each(stats, MAX_THREAD_STATS); |
| 147 | + TEST_ASSERT_EQUAL(3, count); |
| 148 | + |
| 149 | + delete[] stats; |
| 150 | + t1.terminate(); |
| 151 | +} |
| 152 | + |
| 153 | +Case cases[] = { |
| 154 | + Case("Single Thread Stats", test_case_single_thread_stats), |
| 155 | + Case("Less count value", test_case_less_count), |
| 156 | + Case("Multiple Threads blocked", test_case_multi_threads_blocked), |
| 157 | + Case("Multiple Threads terminate", test_case_multi_threads_terminate), |
| 158 | +}; |
| 159 | + |
| 160 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 161 | +{ |
| 162 | + GREENTEA_SETUP(20, "default_auto"); |
| 163 | + return greentea_test_setup_handler(number_of_cases); |
| 164 | +} |
| 165 | + |
| 166 | +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 167 | + |
| 168 | +int main() |
| 169 | +{ |
| 170 | + Harness::run(specification); |
| 171 | +} |
0 commit comments