From fadd08665d0b87aba54fa544799014017cb15a53 Mon Sep 17 00:00:00 2001 From: dario Date: Wed, 7 Feb 2024 18:51:25 +0100 Subject: [PATCH] Two more test cases for the watchdog --- src/cases/case10.cpp | 118 +++++++++++++++++++++++++++++++++++++++++++ src/cases/case11.cpp | 77 ++++++++++++++++++++++++++++ src/cases/case9.cpp | 2 - 3 files changed, 195 insertions(+), 2 deletions(-) create mode 100644 src/cases/case10.cpp create mode 100644 src/cases/case11.cpp diff --git a/src/cases/case10.cpp b/src/cases/case10.cpp new file mode 100644 index 0000000..2fde2f4 --- /dev/null +++ b/src/cases/case10.cpp @@ -0,0 +1,118 @@ +/* + * case10.cpp + * + * Created on: Feb 7, 2024 + * Author: Dario + */ + +/** + * Tests if threads are properly terminated and started because of state transitions. The thread status is updated accordingly and no memory leaks occur. + */ +#include +#ifdef TASTY_CASE_10 + +#include +#include +#include +#include + +namespace sta +{ + namespace tasty + { + RtosSignal * signal; + + class ThreadA : public tacos::TacosThread + { + public: + ThreadA() + : tacos::TacosThread{"A", osPriorityNormal} + { + + } + + void pointless_expensive_computation() + { + uint32_t ticks = osKernelGetSysTimerCount(); + uint32_t freq = osKernelGetSysTimerCount(); + uint64_t dummyCounter = 0; + + while (osKernelGetSysTimerCount() <= ticks + 2 * freq) + { + dummyCounter++; + } + } + + void func() override + { + this->sleep(3000); + + signal->notify(); + + // This should notify the watchdog that this thread should be monitored. + blocking( + pointless_expensive_computation(); + ) + + signal->notify(); + + pointless_expensive_computation(); + + } + }; + + std::shared_ptr thread_A; + + class Observer : public tacos::TacosThread + { + public: + Observer() + : tacos::TacosThread{"Observer", osPriorityNormal} + { + + } + + + + void func() override + { + uint16_t restarts = tacos::Watchdog::instance()->getNumRestarts(); + + // Wait for thread A to finish sleeping. + blocking( + signal->wait(); + ) + + // Thread A shouldn't have been restarted because sleep() sets the thread's status to WAITING. + STA_TASTY_ASSERT(tacos::Watchdog::instance()->getNumRestarts() == restarts); + + blocking( + signal->wait(); + ) + + // Thread A shouldn't have been restarted because the computationally expensive computation was wrapped in blocking() + STA_TASTY_ASSERT(tacos::Watchdog::instance()->getNumRestarts() == restarts); + + this->sleep(2500); + + STA_TASTY_ASSERT(tacos::Watchdog::instance()->getNumRestarts() == restarts + 1); + + STA_TASTY_TERMINATE(); + } + }; + + void onTastyInit() + { + signal = new RtosSignal(2); + + thread_A = tacos::addThread({0}); + tacos::addThread({0, 1}); + } + } // namespace tasty +} + +#endif // TASTY_CASE_10 + + + + diff --git a/src/cases/case11.cpp b/src/cases/case11.cpp new file mode 100644 index 0000000..ef43b51 --- /dev/null +++ b/src/cases/case11.cpp @@ -0,0 +1,77 @@ +/* + * case10.cpp + * + * Created on: Feb 7, 2024 + * Author: Dario + */ + +/** + * Tests if threads are properly terminated and started because of state transitions. The thread status is updated accordingly and no memory leaks occur. + */ +#include +#ifdef TASTY_CASE_11 + +#include +#include +#include +#include + +namespace sta +{ + namespace tasty + { + class ThreadA : public tacos::TacosThread + { + public: + ThreadA() + : tacos::TacosThread{"A", osPriorityNormal} + { + + } + + void init() override + { + // Tells the watchdog to ignore this thread. + watchdogIgnore(); + } + + void func() override + { + // Thread is stuck in infinite loop + while (true) + { + osThreadYield(); + } + } + }; + + class Observer : public tacos::TacosThread + { + public: + Observer() + : tacos::TacosThread{"Observer", osPriorityNormal} + { + + } + + + void func() override + { + this->sleep(2000); + + // The watchdog shouldn't have restarted thread A. + STA_TASTY_ASSERT(tacos::Watchdog::instance()->getNumRestarts() == 0); + + STA_TASTY_TERMINATE(); + } + }; + + void onTastyInit() + { + tacos::addThread({0}); + tacos::addThread({0, 1}); + } + } // namespace tasty +} + +#endif // TASTY_CASE_11 diff --git a/src/cases/case9.cpp b/src/cases/case9.cpp index 88bd40e..6c23e87 100644 --- a/src/cases/case9.cpp +++ b/src/cases/case9.cpp @@ -13,8 +13,6 @@ #include #include -#include -#include #include namespace sta