Skip to content

Commit

Permalink
Start test works
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStarch committed Apr 11, 2024
1 parent f98b6c4 commit 97fe724
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 21 deletions.
11 changes: 11 additions & 0 deletions Os/Posix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ register_fprime_module(Os_Task_Posix)
register_fprime_implementation(Os/Task Os_Task_Posix "${CMAKE_CURRENT_LIST_DIR}/DefaultTask.cpp")

#### Os/Task/Posix Unit Tests ####
set(UT_SOURCE_FILES
"${CMAKE_CURRENT_LIST_DIR}/../test/ut/task/CommonTests.cpp"
"${CMAKE_CURRENT_LIST_DIR}/../test/ut/task/TaskRules.cpp"
"${CMAKE_CURRENT_LIST_DIR}/test/ut/PosixTaskTests.cpp"
)
set(UT_MOD_DEPS
Os
STest
)
choose_fprime_implementation(Os/Task Os_Task_Posix)
register_fprime_ut(PosixTaskTest)
8 changes: 8 additions & 0 deletions Os/Posix/test/ut/PosixTaskTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <gtest/gtest.h>
#include "STest/Scenario/Scenario.hpp"

int main(int argc, char** argv) {
STest::Random::seed();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
3 changes: 1 addition & 2 deletions Os/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Os {

class TaskInterface {
public:
static const FwSizeType TASK_DEFAULT;
static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
enum Status {
OP_OK, //!< message sent/received okay
INVALID_HANDLE, //!< Task handle invalid
Expand Down Expand Up @@ -203,7 +203,6 @@ namespace Os {
//! behaviour.
class Task final : public TaskInterface {
public:
static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
//! Wrapper for task routine that ensures `onStart()` is called once the task actually begins
// TODO: does this belong here or in the parent class
class TaskRoutineWrapper {
Expand Down
2 changes: 1 addition & 1 deletion Os/test/ut/file/CommonTests.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// \title Os/test/ut/file/CommonFileTests.cpp
// \title Os/test/ut/file/CommonTests.cpp
// \brief common test implementations
// ======================================================================
#include "Os/test/ut/file/CommonTests.hpp"
Expand Down
2 changes: 1 addition & 1 deletion Os/test/ut/file/CommonTests.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ======================================================================
// \title Os/test/ut/file/CommonFileTests.hpp
// \title Os/test/ut/file/CommonTests.hpp
// \brief definitions used in common file testing
// ======================================================================
#include <Os/File.hpp>
Expand Down
29 changes: 29 additions & 0 deletions Os/test/ut/task/CommonTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// ======================================================================
// \title Os/test/ut/task/CommonTests.cpp
// \brief common test implementations
// ======================================================================
#include <gtest/gtest.h>
#include "Os/Task.hpp"
#include "Os/test/ut/task/RulesHeaders.hpp"

namespace Os {
namespace Test {
namespace Task {
void run_test(void *argument) {
RunNotification &run_notification = *reinterpret_cast<RunNotification *>(argument);
run_notification.m_lock.lock();
run_notification.m_notification = true;
run_notification.m_lock.unlock();
}
}
}
}

// Ensure that open mode changes work reliably
TEST(Functionality, StartTask) {
Os::Test::Task::Tester tester;
Os::Test::Task::Tester::Start rule;
rule.apply(tester);
}


16 changes: 3 additions & 13 deletions Os/test/ut/task/RulesHeaders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
#ifndef __RULES_HEADERS__
#define __RULES_HEADERS__

#include "STest/STest/Rule/Rule.hpp"
#include "STest/Rule/Rule.hpp"
#include "STest/Scenario/BoundedScenario.hpp"
#include "STest/Scenario/RandomScenario.hpp"
#include "STest/Scenario/Scenario.hpp"
#include "TaskRules.hpp"
#include "Os/Task.hpp"
#include "Os/Mutex.hpp"

Expand All @@ -20,24 +19,15 @@ struct RunNotification {
bool m_notification = false;
};

void run_test(void* argument) {
RunNotification& run_notification = *reinterpret_cast<RunNotification*>(argument);
run_notification.m_lock.lock();
run_notification.m_notification = true;
run_notification.m_lock.unlock();
}

void run_test(void* argument);

struct Tester {
Tester() : m_arguments("MyTask", &run_test, &m_notification) {}

private:
Os::Task::Arguments m_arguments;
Os::Task::State m_state = Os::Task::State::NOT_STARTED;
Os::Task m_task;
RunNotification m_notification;


public:
#include "TaskRules.hpp"
};

Expand Down
22 changes: 18 additions & 4 deletions Os/test/ut/task/TaskRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,24 @@ bool Os::Test::Task::Tester::Start::precondition(
void Os::Test::Task::Tester::Start::action(
Os::Test::Task::Tester &state //!< The test state
) {
printf("--> Rule: %s \n", this->name);
state.m_task.start(state.m_arguments);
do {
printf("--> Rule: Start \n");
TaskString name("StartRuleTask");
Os::Task::Arguments arguments(name, &run_test, &state.m_notification);
state.m_task.start(arguments);

bool done = false;
FwSizeType i = 0;
for (i = 0; i < 100 and not done; i++) {
Os::Task::delay(Fw::Time(0, 1000));
state.m_notification.m_lock.lock();
done = state.m_notification.m_notification;
state.m_notification.m_lock.unlock();

if (state.m_task.isCooperative()) {
// TODO: run
}
}
ASSERT_LT(i, 100) << "Task did not run within 100ms";

} while ();
}

0 comments on commit 97fe724

Please sign in to comment.