2008-12-06 09:02:28 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2008-12-06 09:02:28 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2008-12-06 09:02:28 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2008-12-06 09:02:28 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.h"
|
2008-12-06 09:02:28 +00:00
|
|
|
#include "test.h"
|
|
|
|
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_events Events test
|
|
|
|
*
|
2010-04-27 11:53:03 +00:00
|
|
|
* File: @ref testevt.c
|
|
|
|
*
|
2009-05-08 14:06:45 +00:00
|
|
|
* <h2>Description</h2>
|
2009-08-30 06:59:43 +00:00
|
|
|
* This module implements the test sequence for the @ref events subsystem.
|
2009-05-08 14:06:45 +00:00
|
|
|
*
|
|
|
|
* <h2>Objective</h2>
|
2009-08-30 06:59:43 +00:00
|
|
|
* Objective of the test module is to cover 100% of the @ref events subsystem.
|
2009-05-08 14:06:45 +00:00
|
|
|
*
|
|
|
|
* <h2>Preconditions</h2>
|
|
|
|
* The module requires the following kernel options:
|
|
|
|
* - @p CH_USE_EVENTS
|
|
|
|
* - @p CH_USE_EVENTS_TIMEOUT
|
|
|
|
* .
|
|
|
|
* In case some of the required options are not enabled then some or all tests
|
|
|
|
* may be skipped.
|
|
|
|
*
|
|
|
|
* <h2>Test Cases</h2>
|
|
|
|
* - @subpage test_events_001
|
|
|
|
* - @subpage test_events_002
|
|
|
|
* - @subpage test_events_003
|
|
|
|
* .
|
|
|
|
* @file testevt.c
|
|
|
|
* @brief Events test source file
|
|
|
|
* @file testevt.h
|
|
|
|
* @brief Events test header file
|
|
|
|
*/
|
|
|
|
|
2011-03-23 13:12:41 +00:00
|
|
|
#if CH_USE_EVENTS || defined(__DOXYGEN__)
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
#define ALLOWED_DELAY MS2ST(5)
|
|
|
|
|
2009-05-16 10:57:16 +00:00
|
|
|
/*
|
|
|
|
* Note, the static initializers are not really required because the
|
|
|
|
* variables are explicitly initialized in each test case. It is done in order
|
|
|
|
* to test the macros.
|
|
|
|
*/
|
|
|
|
static EVENTSOURCE_DECL(es1);
|
|
|
|
static EVENTSOURCE_DECL(es2);
|
2008-12-06 09:02:28 +00:00
|
|
|
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_events_001 Events registration and dispatch
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* Two event listeners are registered on an event source and then unregistered
|
|
|
|
* in the same order.<br>
|
|
|
|
* The test expects that the even source has listeners after the registrations
|
|
|
|
* and after the first unregistration, then, after the second unegistration,
|
|
|
|
* the test expects no more listeners.<br>
|
|
|
|
* In the second part the test dispatches three event flags and verifies that
|
|
|
|
* the associated event handlers are invoked in LSb-first order.
|
|
|
|
*/
|
|
|
|
|
2008-12-06 09:02:28 +00:00
|
|
|
static void evt1_setup(void) {
|
|
|
|
|
2012-09-16 08:41:45 +00:00
|
|
|
chEvtGetAndClearEvents(ALL_EVENTS);
|
2008-12-06 09:02:28 +00:00
|
|
|
}
|
|
|
|
|
2009-10-17 10:33:37 +00:00
|
|
|
static void h1(eventid_t id) {(void)id;test_emit_token('A');}
|
|
|
|
static void h2(eventid_t id) {(void)id;test_emit_token('B');}
|
|
|
|
static void h3(eventid_t id) {(void)id;test_emit_token('C');}
|
2010-06-25 08:55:40 +00:00
|
|
|
static ROMCONST evhandler_t evhndl[] = {h1, h2, h3};
|
2009-04-17 14:53:04 +00:00
|
|
|
|
|
|
|
static void evt1_execute(void) {
|
2013-07-19 14:51:35 +00:00
|
|
|
event_listener_t el1, el2;
|
2009-04-17 14:53:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing chEvtRegisterMask() and chEvtUnregister().
|
|
|
|
*/
|
2013-07-20 07:24:12 +00:00
|
|
|
chEvtObjectInit(&es1);
|
2009-04-17 14:53:04 +00:00
|
|
|
chEvtRegisterMask(&es1, &el1, 1);
|
|
|
|
chEvtRegisterMask(&es1, &el2, 2);
|
2010-09-21 10:22:06 +00:00
|
|
|
test_assert(1, chEvtIsListeningI(&es1), "no listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
chEvtUnregister(&es1, &el1);
|
2010-09-21 10:22:06 +00:00
|
|
|
test_assert(2, chEvtIsListeningI(&es1), "no listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
chEvtUnregister(&es1, &el2);
|
2010-09-21 10:22:06 +00:00
|
|
|
test_assert(3, !chEvtIsListeningI(&es1), "stuck listener");
|
2009-04-17 14:53:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Testing chEvtDispatch().
|
|
|
|
*/
|
|
|
|
chEvtDispatch(evhndl, 7);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_sequence(4, "ABC");
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testevt1 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Events, registration and dispatch",
|
2009-04-17 14:53:04 +00:00
|
|
|
evt1_setup,
|
|
|
|
NULL,
|
|
|
|
evt1_execute
|
|
|
|
};
|
|
|
|
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_events_002 Events wait and broadcast
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* In this test the following APIs are indipently tested by starting threads
|
|
|
|
* that signal/broadcast events after fixed delays:
|
|
|
|
* - @p chEvtWaitOne()
|
|
|
|
* - @p chEvtWaitAny()
|
|
|
|
* - @p chEvtWaitAll()
|
|
|
|
* .
|
|
|
|
* After each test phase the test verifies that the events have been served at
|
|
|
|
* the expected time and that there are no stuck event flags.
|
|
|
|
*/
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
static void evt2_setup(void) {
|
|
|
|
|
2012-09-16 08:41:45 +00:00
|
|
|
chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static msg_t thread1(void *p) {
|
|
|
|
|
|
|
|
chThdSleepMilliseconds(50);
|
2013-07-19 12:22:31 +00:00
|
|
|
chEvtSignal((thread_t *)p, 1);
|
2009-04-17 14:53:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static msg_t thread2(void *p) {
|
2008-12-06 09:02:28 +00:00
|
|
|
|
2009-10-17 10:33:37 +00:00
|
|
|
(void)p;
|
2008-12-06 09:02:28 +00:00
|
|
|
chEvtBroadcast(&es1);
|
|
|
|
chThdSleepMilliseconds(50);
|
|
|
|
chEvtBroadcast(&es2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
static void evt2_execute(void) {
|
2008-12-06 09:02:28 +00:00
|
|
|
eventmask_t m;
|
2013-07-19 14:51:35 +00:00
|
|
|
event_listener_t el1, el2;
|
2008-12-06 09:02:28 +00:00
|
|
|
systime_t target_time;
|
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitOne() without wait.
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
2012-09-16 08:41:45 +00:00
|
|
|
chEvtAddEvents(5);
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, m == 1, "single event error");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(2, m == 4, "single event error");
|
2012-09-16 08:41:45 +00:00
|
|
|
m = chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, m == 0, "stuck event");
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
/*
|
|
|
|
* Test on chEvtWaitOne() with wait.
|
|
|
|
*/
|
|
|
|
test_wait_tick();
|
2013-06-16 12:21:30 +00:00
|
|
|
target_time = chVTGetSystemTime() + MS2ST(50);
|
2009-04-17 14:53:04 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread1, chThdSelf());
|
|
|
|
m = chEvtWaitOne(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(4, target_time, target_time + ALLOWED_DELAY);
|
|
|
|
test_assert(5, m == 1, "single event error");
|
2012-09-16 08:41:45 +00:00
|
|
|
m = chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(6, m == 0, "stuck event");
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_threads();
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitAny() without wait.
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
2012-09-16 08:41:45 +00:00
|
|
|
chEvtAddEvents(5);
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitAny(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(7, m == 5, "unexpected pending bit");
|
2012-09-16 08:41:45 +00:00
|
|
|
m = chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(8, m == 0, "stuck event");
|
2008-12-06 09:02:28 +00:00
|
|
|
|
|
|
|
/*
|
2009-04-17 14:53:04 +00:00
|
|
|
* Test on chEvtWaitAny() with wait.
|
|
|
|
*/
|
|
|
|
test_wait_tick();
|
2013-06-16 12:21:30 +00:00
|
|
|
target_time = chVTGetSystemTime() + MS2ST(50);
|
2009-04-17 14:53:04 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread1, chThdSelf());
|
|
|
|
m = chEvtWaitAny(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(9, target_time, target_time + ALLOWED_DELAY);
|
|
|
|
test_assert(10, m == 1, "single event error");
|
2012-09-16 08:41:45 +00:00
|
|
|
m = chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(11, m == 0, "stuck event");
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_threads();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test on chEvtWaitAll().
|
2008-12-06 09:02:28 +00:00
|
|
|
*/
|
2013-07-20 07:24:12 +00:00
|
|
|
chEvtObjectInit(&es1);
|
|
|
|
chEvtObjectInit(&es2);
|
2008-12-06 09:02:28 +00:00
|
|
|
chEvtRegisterMask(&es1, &el1, 1);
|
|
|
|
chEvtRegisterMask(&es2, &el2, 4);
|
2009-04-17 14:53:04 +00:00
|
|
|
test_wait_tick();
|
2013-06-16 12:21:30 +00:00
|
|
|
target_time = chVTGetSystemTime() + MS2ST(50);
|
2009-04-17 14:53:04 +00:00
|
|
|
threads[0] = chThdCreateStatic(wa[0], WA_SIZE, chThdGetPriority() - 1,
|
|
|
|
thread2, "A");
|
2008-12-06 09:02:28 +00:00
|
|
|
m = chEvtWaitAll(5);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert_time_window(12, target_time, target_time + ALLOWED_DELAY);
|
2012-09-16 08:41:45 +00:00
|
|
|
m = chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(13, m == 0, "stuck event");
|
2008-12-06 09:02:28 +00:00
|
|
|
test_wait_threads();
|
|
|
|
chEvtUnregister(&es1, &el1);
|
|
|
|
chEvtUnregister(&es2, &el2);
|
2010-09-21 10:22:06 +00:00
|
|
|
test_assert(14, !chEvtIsListeningI(&es1), "stuck listener");
|
|
|
|
test_assert(15, !chEvtIsListeningI(&es2), "stuck listener");
|
2008-12-06 09:02:28 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testevt2 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Events, wait and broadcast",
|
2009-04-17 14:53:04 +00:00
|
|
|
evt2_setup,
|
2009-02-16 18:22:49 +00:00
|
|
|
NULL,
|
2009-04-17 14:53:04 +00:00
|
|
|
evt2_execute
|
|
|
|
};
|
|
|
|
|
2011-03-23 13:12:41 +00:00
|
|
|
#if CH_USE_EVENTS_TIMEOUT || defined(__DOXYGEN__)
|
2009-05-08 14:06:45 +00:00
|
|
|
/**
|
|
|
|
* @page test_events_003 Events timeout
|
|
|
|
*
|
|
|
|
* <h2>Description</h2>
|
|
|
|
* In this test the following APIs are let to timeout twice: immediatly and
|
|
|
|
* after 10ms:
|
|
|
|
* In this test the following APIs are indipently tested by starting threads
|
|
|
|
* that broadcast events after fixed delays:
|
|
|
|
* - @p chEvtWaitOneTimeout()
|
|
|
|
* - @p chEvtWaitAnyTimeout()
|
|
|
|
* - @p chEvtWaitAllTimeout()
|
|
|
|
* .
|
|
|
|
* After each test phase the test verifies that there are no stuck event flags.
|
|
|
|
*/
|
|
|
|
|
2009-04-17 14:53:04 +00:00
|
|
|
static void evt3_setup(void) {
|
|
|
|
|
2012-09-16 08:41:45 +00:00
|
|
|
chEvtGetAndClearEvents(ALL_EVENTS);
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void evt3_execute(void) {
|
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tests various timeout situations.
|
|
|
|
*/
|
|
|
|
m = chEvtWaitOneTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(1, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAnyTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(2, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAllTimeout(ALL_EVENTS, TIME_IMMEDIATE);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(3, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitOneTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(4, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAnyTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(5, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
m = chEvtWaitAllTimeout(ALL_EVENTS, 10);
|
2009-04-25 11:12:10 +00:00
|
|
|
test_assert(6, m == 0, "spurious event");
|
2009-04-17 14:53:04 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase testevt3 = {
|
2010-06-15 11:34:16 +00:00
|
|
|
"Events, timeouts",
|
2009-04-17 14:53:04 +00:00
|
|
|
evt3_setup,
|
|
|
|
NULL,
|
|
|
|
evt3_execute
|
2008-12-06 09:02:28 +00:00
|
|
|
};
|
2009-05-01 13:34:51 +00:00
|
|
|
#endif /* CH_USE_EVENTS_TIMEOUT */
|
2009-02-16 18:22:49 +00:00
|
|
|
|
2010-04-27 11:53:03 +00:00
|
|
|
/**
|
|
|
|
* @brief Test sequence for events.
|
2009-02-16 18:22:49 +00:00
|
|
|
*/
|
2010-06-25 08:55:40 +00:00
|
|
|
ROMCONST struct testcase * ROMCONST patternevt[] = {
|
2011-03-23 13:12:41 +00:00
|
|
|
#if CH_USE_EVENTS || defined(__DOXYGEN__)
|
2009-02-16 18:22:49 +00:00
|
|
|
&testevt1,
|
2009-04-17 14:53:04 +00:00
|
|
|
&testevt2,
|
2011-03-23 13:12:41 +00:00
|
|
|
#if CH_USE_EVENTS_TIMEOUT || defined(__DOXYGEN__)
|
2009-04-17 14:53:04 +00:00
|
|
|
&testevt3,
|
|
|
|
#endif
|
2009-02-16 18:22:49 +00:00
|
|
|
#endif
|
|
|
|
NULL
|
|
|
|
};
|
2009-05-01 13:34:51 +00:00
|
|
|
|
|
|
|
#endif /* CH_USE_EVENTS */
|