|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2016 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES Ovoid CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "mbed.h" |
| 18 | +#include "greentea-client/test_env.h" |
| 19 | +#include "unity/unity.h" |
| 20 | +#include "utest/utest.h" |
| 21 | +#include "EventLoop.h" |
| 22 | +#include "Event.h" |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +using namespace utest::v1; |
| 26 | + |
| 27 | +static EventLoop queue(16); |
| 28 | +static osThreadId main_thread_id; |
| 29 | +static osThreadId expected_thread_id; |
| 30 | +static volatile uint32_t call_count; |
| 31 | + |
| 32 | +static void test_func() { |
| 33 | + if (expected_thread_id == NULL) { |
| 34 | + // We're testing the event queue, so save the queue thread ID for subsequent |
| 35 | + // tests (and make sure that we're not running in the main context). |
| 36 | + TEST_ASSERT_NOT_EQUAL((uint32_t)expected_thread_id, (uint32_t)main_thread_id); |
| 37 | + expected_thread_id = Thread::gettid(); |
| 38 | + } else { |
| 39 | + TEST_ASSERT_EQUAL((uint32_t)expected_thread_id, (uint32_t)Thread::gettid()); |
| 40 | + } |
| 41 | + call_count ++; |
| 42 | +} |
| 43 | + |
| 44 | +static void test_immediate_events() { |
| 45 | + expected_thread_id = main_thread_id; |
| 46 | + call_count = 0; |
| 47 | + // Post 10 "regular" events (they'll execute immediately in |
| 48 | + // the context of the main thread) |
| 49 | + for (unsigned i = 0; i < 10; i ++) { |
| 50 | + event(test_func, NULL).call(); |
| 51 | + TEST_ASSERT_EQUAL_UINT32(call_count, i + 1); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +static void test_queued_events() { |
| 56 | + // We don't know the event thread ID. it'll be set by the first call to test_func |
| 57 | + expected_thread_id = NULL; |
| 58 | + call_count = 0; |
| 59 | + // Start event thread now |
| 60 | + queue.start(); |
| 61 | + // Post 10 events that will be queued |
| 62 | + for (unsigned i = 0; i < 10; i ++) { |
| 63 | + event(test_func, &queue).call(); |
| 64 | + } |
| 65 | + // Wait for them to finish |
| 66 | + while (call_count < 10) { |
| 67 | + wait_ms(10); |
| 68 | + } |
| 69 | + // Wait a bit more and check count again to check that no more events are executed |
| 70 | + wait_ms(200); |
| 71 | + TEST_ASSERT_EQUAL_UINT32(call_count, 10); |
| 72 | +} |
| 73 | + |
| 74 | +static Case cases[] = { |
| 75 | + Case("Test immediate events", test_immediate_events), |
| 76 | + Case("Test queued events", test_queued_events) |
| 77 | +}; |
| 78 | + |
| 79 | +static status_t greentea_test_setup(const size_t number_of_cases) { |
| 80 | + GREENTEA_SETUP(20, "default_auto"); |
| 81 | + return greentea_test_setup_handler(number_of_cases); |
| 82 | +} |
| 83 | + |
| 84 | +static Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 85 | + |
| 86 | +int main() { |
| 87 | + main_thread_id = Thread::gettid(); |
| 88 | + // Run tests |
| 89 | + Harness::run(specification); |
| 90 | +} |
0 commit comments