Skip to content

Commit dcdd2bb

Browse files
vTaskDelay: will track and move time forward and coordinates with timers.
1 parent 0aa2c01 commit dcdd2bb

File tree

8 files changed

+206
-18
lines changed

8 files changed

+206
-18
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/// @brief Support methods to help with unit testing for FreeRTOS task
2+
/// related methods.
3+
/// @ingroup
4+
/// @cond
5+
///***************************************************************************
6+
///
7+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
8+
///
9+
/// This program is open source software: you can redistribute it and/or
10+
/// modify it under the terms of the GNU General Public License as published
11+
/// by the Free Software Foundation, either version 3 of the License, or
12+
/// (at your option) any later version.
13+
///
14+
/// Alternatively, upon written permission from Matthew Eshleman, this program
15+
/// may be distributed and modified under the terms of a Commercial
16+
/// License. For further details, see the Contact Information below.
17+
///
18+
/// Contact Information:
19+
/// Matthew Eshleman
20+
/// https://covemountainsoftware.com
21+
22+
///***************************************************************************
23+
/// @endcond
24+
#ifndef CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_TASK_HPP
25+
#define CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_TASK_HPP
26+
27+
namespace cms {
28+
namespace test {
29+
30+
/**
31+
* Init, prepare for task related usage, primarily for time
32+
* related tracking.
33+
*/
34+
void TaskInit();
35+
36+
/**
37+
* Clean up/destroy the fake CppUTest for task details, primarily
38+
* related to time.
39+
*/
40+
void TaskDestroy();
41+
}
42+
}
43+
44+
#endif //CPPUTEST_FOR_FREERTOS_LIB_CPPUTEST_FREERTOS_TASK_HPP

cpputest-for-freertos-lib/include/cpputest_freertos_timers.hpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,35 @@
77
namespace cms {
88
namespace test {
99

10-
void InitTimers();
11-
void DestroyTimers();
10+
/**
11+
* Initialize the functional but fake CppUTest for FreeRTOS timers.
12+
*/
13+
void TimersInit();
14+
15+
/**
16+
* Clean up/destroy the fake CppUTest for FreeRTOS timers.
17+
*/
18+
void TimersDestroy();
19+
20+
/**
21+
* check if the current unit test has initialized the timer
22+
* subsystem or not.
23+
* @return true: yes, TimersInit() was called, so timers are active.
24+
*/
25+
bool TimersIsActive();
26+
27+
/**
28+
* Move Time Forward.
29+
* @param duration
30+
*/
1231
void MoveTimeForward(std::chrono::nanoseconds duration);
1332

33+
/**
34+
* Get the current time, as duration since Init was called.
35+
* @return
36+
*/
37+
std::chrono::nanoseconds GetCurrentInternalTime();
38+
1439
} //namespace
1540
}//namespace
1641

cpputest-for-freertos-lib/src/cpputest_freertos_task.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@
2828
/// @endcond
2929
#include "FreeRTOS.h"
3030
#include "task.h"
31+
#include "cpputest_freertos_timers.hpp"
32+
33+
namespace cms {
34+
namespace test {
35+
36+
static TickType_t s_tickCount = 0;
37+
38+
void TaskInit()
39+
{
40+
s_tickCount = 0;
41+
}
42+
43+
void TaskDestroy()
44+
{
45+
s_tickCount = 0;
46+
}
47+
48+
} //namespace test
49+
} //namespace cms
3150

3251
extern "C" BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
3352
const char * const pcName,
@@ -48,4 +67,31 @@ extern "C" BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
4867
extern "C" void vTaskDelete( TaskHandle_t xTaskToDelete )
4968
{
5069
(void)xTaskToDelete;
51-
}
70+
}
71+
72+
extern "C" void vTaskDelay(const TickType_t ticks)
73+
{
74+
if (cms::test::TimersIsActive())
75+
{
76+
auto duration = std::chrono::milliseconds {pdTICKS_TO_MS(ticks)};
77+
cms::test::MoveTimeForward(duration);
78+
}
79+
else
80+
{
81+
cms::test::s_tickCount += ticks;
82+
}
83+
}
84+
85+
extern "C" TickType_t xTaskGetTickCount(void)
86+
{
87+
if (cms::test::TimersIsActive())
88+
{
89+
auto current = cms::test::GetCurrentInternalTime();
90+
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(current);
91+
return pdMS_TO_TICKS(milliseconds.count());
92+
}
93+
else
94+
{
95+
return cms::test::s_tickCount;
96+
}
97+
}

cpputest-for-freertos-lib/src/cpputest_freertos_timers.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,44 @@ namespace test {
77

88
static FakeTimers* s_fakeTimers = nullptr;
99

10-
void InitTimers()
10+
void TimersInit()
1111
{
1212
configASSERT(s_fakeTimers == nullptr);
1313

1414
std::chrono::milliseconds sysTick { configTICK_RATE_HZ * 1/1000 };
1515
s_fakeTimers = new FakeTimers(sysTick);
1616
}
1717

18-
void DestroyTimers()
18+
void TimersDestroy()
1919
{
2020
configASSERT(s_fakeTimers != nullptr);
2121
delete s_fakeTimers;
2222
s_fakeTimers = nullptr;
2323
}
2424

25+
bool TimersIsActive()
26+
{
27+
return s_fakeTimers != nullptr;
28+
}
29+
2530
void MoveTimeForward(std::chrono::nanoseconds duration)
2631
{
2732
configASSERT(s_fakeTimers != nullptr);
2833
s_fakeTimers->MoveTimeForward(duration);
2934
}
3035

36+
std::chrono::nanoseconds GetCurrentInternalTime()
37+
{
38+
if (TimersIsActive())
39+
{
40+
return s_fakeTimers->GetCurrentInternalTime();
41+
}
42+
else
43+
{
44+
return std::chrono::nanoseconds (0);
45+
}
46+
}
47+
3148
std::chrono::nanoseconds TicksToChrono(TickType_t ticks)
3249
{
3350
std::chrono::milliseconds rtn(pdTICKS_TO_MS(ticks));
@@ -122,15 +139,6 @@ extern "C" BaseType_t xTimerGenericCommandFromTask( TimerHandle_t xTimer,
122139
return pdPASS;
123140
}
124141

125-
extern "C" TickType_t xTaskGetTickCount(void)
126-
{
127-
configASSERT(s_fakeTimers != nullptr);
128-
auto current = s_fakeTimers->GetCurrentInternalTime();
129-
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(current);
130-
131-
return pdMS_TO_TICKS(milliseconds.count());
132-
}
133-
134142
extern "C" BaseType_t xTimerIsTimerActive( TimerHandle_t xTimer )
135143
{
136144
configASSERT(s_fakeTimers != nullptr);

cpputest-for-freertos-lib/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(TEST_SOURCES
44
cpputest_freertos_assert_tests.cpp
55
cpputest_freertos_timers_tests.cpp
66
cpputest_freertos_queue_tests.cpp
7+
cpputest_freertos_task_tests.cpp
78
)
89

910
# this include expects TEST_SOURCES and TEST_APP_NAME to be
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/// @brief Tests of CppUTest FreeRTOS task methods.
2+
/// @ingroup
3+
/// @cond
4+
///***************************************************************************
5+
///
6+
/// Copyright (C) 2024 Matthew Eshleman. All rights reserved.
7+
///
8+
/// This program is open source software: you can redistribute it and/or
9+
/// modify it under the terms of the GNU General Public License as published
10+
/// by the Free Software Foundation, either version 3 of the License, or
11+
/// (at your option) any later version.
12+
///
13+
/// Alternatively, upon written permission from Matthew Eshleman, this program
14+
/// may be distributed and modified under the terms of a Commercial
15+
/// License. For further details, see the Contact Information below.
16+
///
17+
/// Contact Information:
18+
/// Matthew Eshleman
19+
/// https://covemountainsoftware.com
20+
21+
///***************************************************************************
22+
/// @endcond
23+
#include "FreeRTOS.h"
24+
#include "task.h"
25+
#include "cpputest_freertos_timers.hpp"
26+
#include "cpputest_freertos_task.hpp"
27+
#include "CppUTest/TestHarness.h"
28+
29+
TEST_GROUP(TaskTests)
30+
{
31+
void setup() final
32+
{
33+
cms::test::TaskInit();
34+
}
35+
36+
void teardown() final
37+
{
38+
cms::test::TaskDestroy();
39+
}
40+
};
41+
42+
TEST(TaskTests, task_delay_method_is_available_and_tracks_time_when_not_using_timers)
43+
{
44+
auto count1 = xTaskGetTickCount();
45+
vTaskDelay(100);
46+
auto count2 = xTaskGetTickCount();
47+
CHECK_EQUAL(100, count2 - count1);
48+
}
49+
50+
TEST(TaskTests, task_delay_method_will_move_timers_time_forward_if_timers_are_active)
51+
{
52+
cms::test::TimersInit();
53+
auto count1 = xTaskGetTickCount();
54+
vTaskDelay(100);
55+
auto count2 = xTaskGetTickCount();
56+
CHECK_EQUAL(100, count2 - count1);
57+
auto fromTimers = cms::test::GetCurrentInternalTime();
58+
59+
CHECK_TRUE(std::chrono::milliseconds(pdTICKS_TO_MS(100)) == fromTimers);
60+
cms::test::TimersDestroy();
61+
}

cpputest-for-freertos-lib/tests/cpputest_freertos_timers_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ TEST_GROUP(TimersTests)
3232
{
3333
void setup() final
3434
{
35-
cms::test::InitTimers();
35+
cms::test::TimersInit();
3636
}
3737

3838
void teardown() final
3939
{
40-
cms::test::DestroyTimers();
40+
cms::test::TimersDestroy();
4141
mock().clear();
4242
}
4343
};

example/services/hwLockCtrlService/test/hwLockCtrlServiceTests.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SOFTWARE.
2424
#include "hwLockCtrlService.h"
2525
#include "cpputest_freertos_timers.hpp"
2626
#include "cpputest_freertos_assert.hpp"
27+
#include "cpputest_freertos_task.hpp"
2728
#include "CppUTest/TestHarness.h"
2829
#include "CppUTestExt/MockSupport.h"
2930
#include "hwLockCtrl.h"
@@ -60,7 +61,8 @@ TEST_GROUP(HwLockCtrlServiceTests)
6061
{
6162
void setup() final
6263
{
63-
cms::test::InitTimers();
64+
cms::test::TaskInit();
65+
cms::test::TimersInit();
6466
HLCS_Init();
6567
HLCS_RegisterChangeStateCallback(TestLockStateCallback);
6668
HLCS_RegisterSelfTestResultCallback(TestSelfTestResultCallback);
@@ -70,7 +72,8 @@ TEST_GROUP(HwLockCtrlServiceTests)
7072
{
7173
HLCS_Destroy(); //ensure we are stopped/clean/destroyed.
7274
mock().clear();
73-
cms::test::DestroyTimers();
75+
cms::test::TimersDestroy();
76+
cms::test::TaskDestroy();
7477
}
7578

7679
void GiveProcessingTime()

0 commit comments

Comments
 (0)