Added test for barrier pattern

This commit is contained in:
dario 2024-04-18 21:34:40 +02:00
parent d44b87c987
commit 38cef13ff3
3 changed files with 134 additions and 1 deletions

View File

@ -2,6 +2,6 @@
#ifndef STA_TASTY_CONFIG_HPP #ifndef STA_TASTY_CONFIG_HPP
#define STA_TASTY_CONFIG_HPP #define STA_TASTY_CONFIG_HPP
#define TASTY_CASE_9 #define TASTY_CASE_12
#endif // STA_TASTY_CONFIG_HPP #endif // STA_TASTY_CONFIG_HPP

123
src/cases/case12.cpp Normal file
View File

@ -0,0 +1,123 @@
/**
* @file case12.cpp
* @author Dario van den Boom
* @brief
* @version 0.1
* @date 2024-04-18
*
* @copyright Copyright (c) 2024
*
*/
/**
* Tests if the barrier is working correctly.
*/
#include <sta/tasty/config.hpp>
#ifdef TASTY_CASE_12
#include <sta/tacos.hpp>
#include <sta/tasty/utils.hpp>
#include <sta/rtos/patterns/barrier.hpp>
#include <sta/time.hpp>
namespace sta
{
namespace tasty
{
Barrier * barrier;
class BarrierThread : public tacos::TacosThread
{
public:
BarrierThread(const char * name, uint32_t duration, osPriority_t priority = osPriorityNormal)
: tacos::TacosThread{name, priority, 512},
duration_{duration}
{
STA_ASSERT(name != nullptr);
}
void init() override
{
barrier->subscribe();
}
void func() override
{
sleep(duration_);
barrier->wait();
}
private:
uint32_t duration_;
};
class Observer : public tacos::TacosThread
{
public:
Observer()
: tacos::TacosThread{"Observer", osPriorityAboveNormal}
{
}
void func() override
{
sleep(10);
STA_TASTY_ASSERT(barrier->getNumSubscribedThreads() == 4);
STA_TASTY_ASSERT(barrier->getNumEntered() == 0);
STA_TASTY_ASSERT(barrier->getNumLeft() == 4);
// Wait until B and D enter the barrier.
sleep(11);
STA_TASTY_ASSERT(barrier->getNumEntered() == 2);
STA_TASTY_ASSERT(barrier->getNumLeft() == 0);
sleep(50);
STA_TASTY_ASSERT(barrier->getNumEntered() == 3);
STA_TASTY_ASSERT(barrier->getNumLeft() == 0);
uint32_t before = now();
barrier->externalWait();
uint32_t after = now();
// The time until A reached the barrier and opened it should have been less than 40 ticks.
STA_TASTY_ASSERT(after - before < 40);
STA_TASTY_ASSERT(barrier->getNumLeft() == 0);
STA_TASTY_ASSERT(barrier->getNumEntered() == 4);
// Force the barrier to stay closed for at least 200 ticks.
barrier->setMinimumWaitDuration(200);
sleep(5);
STA_TASTY_ASSERT(barrier->getNumLeft() == 4);
STA_TASTY_ASSERT(barrier->getNumEntered() == 0);
sleep(150);
STA_TASTY_ASSERT(barrier->getNumLeft() == 4);
STA_TASTY_ASSERT(barrier->getNumEntered() == 4);
sleep(55);
STA_TASTY_ASSERT(barrier->getNumLeft() == 4);
STA_TASTY_ASSERT(barrier->getNumEntered() == 0);
STA_TASTY_TERMINATE();
}
};
void onTastyInit()
{
barrier = new Barrier("Test Barrier");
tacos::addThread<BarrierThread>(ALL_STATES, "A", 100);
tacos::addThread<BarrierThread>(ALL_STATES, "B", 10);
tacos::addThread<BarrierThread>(ALL_STATES, "C", 50);
tacos::addThread<BarrierThread>(ALL_STATES, "D", 10);
tacos::addThread<Observer>(ALL_STATES);
}
} // namespace tasty
}
#endif // TASTY_CASE_12

View File

@ -10,6 +10,10 @@
#include <sta/debug/debug.hpp> #include <sta/debug/debug.hpp>
#include <sta/tasty/utils.hpp> #include <sta/tasty/utils.hpp>
#include <FreeRTOS.h>
#include <task.h>
namespace sta namespace sta
{ {
namespace tasty namespace tasty
@ -30,3 +34,9 @@ namespace sta
} // namespace tacos } // namespace tacos
} // namespace sta } // namespace sta
void vApplicationStackOverflowHook(xTaskHandle xTask, signed char *pcTaskName)
{
// Dummy assert to signal test failure due to stack overflow.
STA_TASTY_ASSERT(false);
}