diff --git a/src/os/inc/osapi-common.h b/src/os/inc/osapi-common.h index ac6b1bcdc..d16372e98 100644 --- a/src/os/inc/osapi-common.h +++ b/src/os/inc/osapi-common.h @@ -138,6 +138,33 @@ void OS_Application_Run(void); */ int32 OS_API_Init(void); +/*-------------------------------------------------------------------------------------*/ +/** + * @brief Teardown/de-initialization of OSAL API + * + * This is the inverse of OS_API_Init(). It will release all OS resources and + * return the system to a state similar to what it was prior to invoking + * OS_API_Init() initially. + * + * Normally for embedded applications, the OSAL is initialized after boot and will remain + * initialized in memory until the processor is rebooted. However for testing and + * developement purposes, it is potentially useful to reset back to initial conditions. + * + * For testing purposes, this API is designed/intended to be compatible with the + * UtTest_AddTeardown() routine provided by the UT-Assert subsystem. + * + * @note This is a "best-effort" routine and it may not always be possible/guaranteed + * to recover all resources, particularly in the case of off-nominal conditions, or if + * a resource is used outside of OSAL. + * + * For example, while this will attempt to unload all dynamically-loaded modules, doing + * so may not be possible and/or may induce undefined behavior if resources are in use by + * tasks/functions outside of OSAL. + * + * @return None + */ +void OS_API_Teardown(void); + /*-------------------------------------------------------------------------------------*/ /** * @brief Background thread implementation - waits forever for events to occur. diff --git a/src/os/shared/src/osapi-common.c b/src/os/shared/src/osapi-common.c index 2543d9887..ab3f16309 100644 --- a/src/os/shared/src/osapi-common.c +++ b/src/os/shared/src/osapi-common.c @@ -219,6 +219,28 @@ int32 OS_API_Init(void) return (return_code); } /* end OS_API_Init */ +/*---------------------------------------------------------------- + * + * Function: OS_API_Teardown + * + * Purpose: Implemented per public OSAL API + * See description in API and header file for detail + * + *-----------------------------------------------------------------*/ +void OS_API_Teardown(void) +{ + /* + * This should delete any remaining user-created objects/tasks + */ + OS_DeleteAllObjects(); + + /* + * This should cause the "internal" objects (e.g. console utility task) + * to exit, and will prevent any new objects from being created. + */ + OS_ApplicationShutdown(true); +} + /*---------------------------------------------------------------- * * Function: OS_RegisterEventHandler diff --git a/src/tests/bin-sem-flush-test/bin-sem-flush-test.c b/src/tests/bin-sem-flush-test/bin-sem-flush-test.c index 33297a34c..9916c8f42 100644 --- a/src/tests/bin-sem-flush-test/bin-sem-flush-test.c +++ b/src/tests/bin-sem-flush-test/bin-sem-flush-test.c @@ -177,6 +177,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/bin-sem-test/bin-sem-test.c b/src/tests/bin-sem-test/bin-sem-test.c index 36fa6cc4f..847616ff0 100644 --- a/src/tests/bin-sem-test/bin-sem-test.c +++ b/src/tests/bin-sem-test/bin-sem-test.c @@ -186,6 +186,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/bin-sem-timeout-test/bin-sem-timeout-test.c b/src/tests/bin-sem-timeout-test/bin-sem-timeout-test.c index 78c72022e..cb926a1a2 100644 --- a/src/tests/bin-sem-timeout-test/bin-sem-timeout-test.c +++ b/src/tests/bin-sem-timeout-test/bin-sem-timeout-test.c @@ -181,6 +181,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/count-sem-test/count-sem-test.c b/src/tests/count-sem-test/count-sem-test.c index 48efe2c8b..4001392b8 100644 --- a/src/tests/count-sem-test/count-sem-test.c +++ b/src/tests/count-sem-test/count-sem-test.c @@ -151,6 +151,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/file-api-test/file-api-test.c b/src/tests/file-api-test/file-api-test.c index a0ced207f..5b548ee52 100644 --- a/src/tests/file-api-test/file-api-test.c +++ b/src/tests/file-api-test/file-api-test.c @@ -57,6 +57,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * This test case requires a fixed virtual dir for one test case. * Just map /test to a dir of the same name, relative to current dir. diff --git a/src/tests/file-sys-add-fixed-map-api-test/file-sys-add-fixed-map-api-test.c b/src/tests/file-sys-add-fixed-map-api-test/file-sys-add-fixed-map-api-test.c index e3fd8567d..710da0d29 100644 --- a/src/tests/file-sys-add-fixed-map-api-test/file-sys-add-fixed-map-api-test.c +++ b/src/tests/file-sys-add-fixed-map-api-test/file-sys-add-fixed-map-api-test.c @@ -96,6 +96,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/idmap-api-test/idmap-api-test.c b/src/tests/idmap-api-test/idmap-api-test.c index 3a9ab8327..d8cd1ca72 100644 --- a/src/tests/idmap-api-test/idmap-api-test.c +++ b/src/tests/idmap-api-test/idmap-api-test.c @@ -348,6 +348,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/mutex-test/mutex-test.c b/src/tests/mutex-test/mutex-test.c index d47de5741..a34330e85 100644 --- a/src/tests/mutex-test/mutex-test.c +++ b/src/tests/mutex-test/mutex-test.c @@ -217,6 +217,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/network-api-test/network-api-test.c b/src/tests/network-api-test/network-api-test.c index 6e53a42ad..d2689bdff 100644 --- a/src/tests/network-api-test/network-api-test.c +++ b/src/tests/network-api-test/network-api-test.c @@ -658,6 +658,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/osal-core-test/osal-core-test.c b/src/tests/osal-core-test/osal-core-test.c index a70aa4aec..f4d728619 100644 --- a/src/tests/osal-core-test/osal-core-test.c +++ b/src/tests/osal-core-test/osal-core-test.c @@ -96,6 +96,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UtTest_Add(TestTasks, NULL, NULL, "TASK"); UtTest_Add(TestQueues, NULL, NULL, "MSGQ"); UtTest_Add(TestBinaries, NULL, NULL, "BSEM"); diff --git a/src/tests/queue-test/queue-test.c b/src/tests/queue-test/queue-test.c index 9d36294b1..a7f1526c4 100644 --- a/src/tests/queue-test/queue-test.c +++ b/src/tests/queue-test/queue-test.c @@ -247,6 +247,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/select-test/select-test.c b/src/tests/select-test/select-test.c index 84b213964..6479e4c41 100644 --- a/src/tests/select-test/select-test.c +++ b/src/tests/select-test/select-test.c @@ -596,6 +596,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/sem-speed-test/sem-speed-test.c b/src/tests/sem-speed-test/sem-speed-test.c index 3c2a61379..fdda695b6 100644 --- a/src/tests/sem-speed-test/sem-speed-test.c +++ b/src/tests/sem-speed-test/sem-speed-test.c @@ -150,6 +150,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/shell-test/shell-test.c b/src/tests/shell-test/shell-test.c index f05ff0f9f..81627a0ee 100644 --- a/src/tests/shell-test/shell-test.c +++ b/src/tests/shell-test/shell-test.c @@ -101,6 +101,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/symbol-api-test/symbol-api-test.c b/src/tests/symbol-api-test/symbol-api-test.c index 60f5a767c..bfe43ea0d 100644 --- a/src/tests/symbol-api-test/symbol-api-test.c +++ b/src/tests/symbol-api-test/symbol-api-test.c @@ -112,6 +112,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/time-base-api-test/time-base-api-test.c b/src/tests/time-base-api-test/time-base-api-test.c index 94bc69a9b..775b5229a 100644 --- a/src/tests/time-base-api-test/time-base-api-test.c +++ b/src/tests/time-base-api-test/time-base-api-test.c @@ -264,6 +264,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/timer-add-api-test/timer-add-api-test.c b/src/tests/timer-add-api-test/timer-add-api-test.c index 5ff9541b4..917db9e8d 100644 --- a/src/tests/timer-add-api-test/timer-add-api-test.c +++ b/src/tests/timer-add-api-test/timer-add-api-test.c @@ -199,6 +199,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the test setup and check routines in UT assert */ diff --git a/src/tests/timer-test/timer-test.c b/src/tests/timer-test/timer-test.c index 79ba39d5b..2863dafbd 100644 --- a/src/tests/timer-test/timer-test.c +++ b/src/tests/timer-test/timer-test.c @@ -74,6 +74,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * Register the timer test setup and check routines in UT assert */ diff --git a/src/unit-test-coverage/shared/src/coveragetest-common.c b/src/unit-test-coverage/shared/src/coveragetest-common.c index 479105e2d..f4921740b 100644 --- a/src/unit-test-coverage/shared/src/coveragetest-common.c +++ b/src/unit-test-coverage/shared/src/coveragetest-common.c @@ -128,6 +128,18 @@ void Test_OS_API_Init(void) UT_ResetState(UT_KEY(OS_TaskAPI_Init)); } +void Test_OS_API_Teardown(void) +{ + /* + * Test Case For: + * void OS_API_Teardown(void); + */ + + /* Just need to call the API for coverage; there are no conditionals + * and the internal functions are each tested separately */ + OS_API_Teardown(); +} + void Test_OS_ApplicationExit(void) { /* @@ -325,4 +337,5 @@ void UtTest_Setup(void) ADD_TEST(OS_IdleLoopAndShutdown); ADD_TEST(OS_ApplicationExit); ADD_TEST(OS_NotifyEvent); + ADD_TEST(OS_API_Teardown); } diff --git a/src/unit-tests/oscore-test/ut_oscore_test.c b/src/unit-tests/oscore-test/ut_oscore_test.c index e1662fd66..9a48689b8 100644 --- a/src/unit-tests/oscore-test/ut_oscore_test.c +++ b/src/unit-tests/oscore-test/ut_oscore_test.c @@ -188,6 +188,9 @@ void UT_os_init_task_get_info_test() void UtTest_Setup(void) { + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UtTest_Add(UT_os_apiinit_test, NULL, NULL, "OS_API_Init"); UtTest_Add(UT_os_printf_test, NULL, NULL, "OS_printf"); diff --git a/src/unit-tests/osfile-test/ut_osfile_test.c b/src/unit-tests/osfile-test/ut_osfile_test.c index 925fdb612..db9484a8b 100644 --- a/src/unit-tests/osfile-test/ut_osfile_test.c +++ b/src/unit-tests/osfile-test/ut_osfile_test.c @@ -132,6 +132,9 @@ void UT_os_init_file_misc() void UtTest_Setup(void) { + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UT_os_initfs_test(); if (UT_os_setup_fs() == OS_SUCCESS) diff --git a/src/unit-tests/osfilesys-test/ut_osfilesys_test.c b/src/unit-tests/osfilesys-test/ut_osfilesys_test.c index 5a8496d72..248ae5296 100644 --- a/src/unit-tests/osfilesys-test/ut_osfilesys_test.c +++ b/src/unit-tests/osfilesys-test/ut_osfilesys_test.c @@ -121,6 +121,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UT_os_init_fs_misc(); UtTest_Add(UT_os_makefs_test, NULL, NULL, "OS_mkfs"); diff --git a/src/unit-tests/osloader-test/ut_osloader_test.c b/src/unit-tests/osloader-test/ut_osloader_test.c index 7dc7d9c6c..d0adbff8b 100644 --- a/src/unit-tests/osloader-test/ut_osloader_test.c +++ b/src/unit-tests/osloader-test/ut_osloader_test.c @@ -67,6 +67,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + /* * This test needs to load the modules from the filesystem, so * there must be a virtual path corresponding to the path where diff --git a/src/unit-tests/osnetwork-test/ut_osnetwork_test.c b/src/unit-tests/osnetwork-test/ut_osnetwork_test.c index a550d2f4a..7277b83dd 100644 --- a/src/unit-tests/osnetwork-test/ut_osnetwork_test.c +++ b/src/unit-tests/osnetwork-test/ut_osnetwork_test.c @@ -65,6 +65,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UtTest_Add(UT_os_networkgetid_test, NULL, NULL, "OS_NetworkGetID"); UtTest_Add(UT_os_networkgethostname_test, NULL, NULL, "OS_NetworkGetHostName"); } diff --git a/src/unit-tests/ostimer-test/ut_ostimer_test.c b/src/unit-tests/ostimer-test/ut_ostimer_test.c index dcbe25d10..68e94ec89 100644 --- a/src/unit-tests/ostimer-test/ut_ostimer_test.c +++ b/src/unit-tests/ostimer-test/ut_ostimer_test.c @@ -193,6 +193,9 @@ void UtTest_Setup(void) UtAssert_Abort("OS_API_Init() failed"); } + /* the test should call OS_API_Teardown() before exiting */ + UtTest_AddTeardown(OS_API_Teardown, "Cleanup"); + UT_os_init_timer_misc(); UtTest_Add(UT_os_timercreate_test, UT_os_setup_timercreate_test, NULL, "OS_TimerCreate");