ADD test for multi-tasking
This commit is contained in:
@ -23,15 +23,23 @@
|
||||
***************************************************************************/
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
|
||||
#include "gear_device.hpp"
|
||||
#include "greentea-client/test_env.h"
|
||||
#include "mbed.h"
|
||||
#include "mbed_trace.h" // NOLINT
|
||||
#include "multi_tasking/bike_system.hpp"
|
||||
#include "static_scheduling/bike_system.hpp"
|
||||
#include "static_scheduling_with_event/bike_system.hpp"
|
||||
#include "task_logger.hpp"
|
||||
#include "unity/unity.h"
|
||||
#include "utest/utest.h"
|
||||
|
||||
#if defined(MBED_CONF_MBED_TRACE_ENABLE)
|
||||
#define TRACE_GROUP "TEST_BIKE_SYSTEM"
|
||||
#endif // MBED_CONF_MBED_TRACE_ENAB
|
||||
|
||||
namespace utest {
|
||||
namespace v1 {
|
||||
|
||||
@ -140,6 +148,235 @@ static void test_bike_system_with_event() {
|
||||
}
|
||||
}
|
||||
|
||||
// test_multi_tasking_bike_system handler function
|
||||
static void test_multi_tasking_bike_system() {
|
||||
tr_info("test multi tasking bike system");
|
||||
|
||||
// create the BikeSystem instance
|
||||
multi_tasking::BikeSystem bikeSystem;
|
||||
|
||||
// run the bike system in a separate thread
|
||||
Thread thread;
|
||||
osStatus status =
|
||||
thread.start(callback(&bikeSystem, &multi_tasking::BikeSystem::start));
|
||||
|
||||
if (status != osOK) {
|
||||
tr_error("Thread bike system is not OK !");
|
||||
}
|
||||
|
||||
tr_info("bike system has started");
|
||||
|
||||
// let the bike system run for 20 secs
|
||||
ThisThread::sleep_for(20s);
|
||||
|
||||
// check whether scheduling was correct
|
||||
// Order is kGearTaskIndex, kSpeedTaskIndex, kTemperatureTaskIndex,
|
||||
// kResetTaskIndex, kDisplayTask1Index, kDisplayTask2Index
|
||||
// When we use event handling, we do not check the computation time
|
||||
constexpr std::chrono::microseconds taskPeriods[] = {
|
||||
800000us, 400000us, 1600000us, 800000us, 1600000us, 1600000us};
|
||||
|
||||
// allow for 2 msecs offset (with EventQueue)
|
||||
constexpr uint64_t kDeltaUs = 2000;
|
||||
|
||||
// stop the bike system
|
||||
bikeSystem.stop();
|
||||
thread.terminate();
|
||||
|
||||
tr_info("Threads have stopped");
|
||||
|
||||
TEST_ASSERT_UINT64_WITHIN(
|
||||
kDeltaUs,
|
||||
taskPeriods[advembsof::TaskLogger::kTemperatureTaskIndex].count(),
|
||||
bikeSystem.getTaskLogger()
|
||||
.getPeriod(advembsof::TaskLogger::kTemperatureTaskIndex)
|
||||
.count());
|
||||
TEST_ASSERT_UINT64_WITHIN(
|
||||
kDeltaUs,
|
||||
taskPeriods[advembsof::TaskLogger::kDisplayTask1Index].count(),
|
||||
bikeSystem.getTaskLogger()
|
||||
.getPeriod(advembsof::TaskLogger::kDisplayTask1Index)
|
||||
.count());
|
||||
}
|
||||
|
||||
// test_reset_multi_tasking_bike_system handler function
|
||||
Timer timer;
|
||||
static std::chrono::microseconds resetTime = std::chrono::microseconds::zero();
|
||||
static EventFlags eventFlags;
|
||||
static constexpr uint32_t kResetEventFlag = (1UL << 0);
|
||||
static void resetCallback() {
|
||||
resetTime = timer.elapsed_time();
|
||||
eventFlags.set(kResetEventFlag);
|
||||
}
|
||||
|
||||
static void test_reset_multi_tasking_bike_system() {
|
||||
tr_info("test reset multi tasking bike system");
|
||||
// create the BikeSystem instance
|
||||
multi_tasking::BikeSystem bikeSystem;
|
||||
|
||||
// run the bike system in a separate thread
|
||||
Thread thread;
|
||||
osStatus status =
|
||||
thread.start(callback(&bikeSystem, &multi_tasking::BikeSystem::start));
|
||||
|
||||
if (status != osOK) {
|
||||
tr_error("Thread bike system is not OK !");
|
||||
}
|
||||
|
||||
tr_info("Bike system has started");
|
||||
|
||||
// let the bike system run for 2 secs
|
||||
ThisThread::sleep_for(2s);
|
||||
|
||||
// test reset on BikeSystem
|
||||
bikeSystem.getSpeedometer().setOnResetCallback(resetCallback);
|
||||
|
||||
// start the timer instance
|
||||
timer.start();
|
||||
|
||||
// check for reset response time
|
||||
constexpr uint8_t kNbrOfResets = 10;
|
||||
std::chrono::microseconds lastResponseTime = std::chrono::microseconds::zero();
|
||||
for (uint8_t i = 0; i < kNbrOfResets; i++) {
|
||||
tr_info("Reset test N°%d", i);
|
||||
// take time before reset
|
||||
auto startTime = timer.elapsed_time();
|
||||
|
||||
// reset the BikeSystem
|
||||
bikeSystem.onReset();
|
||||
|
||||
// wait for resetCallback to be called
|
||||
eventFlags.wait_all(kResetEventFlag);
|
||||
|
||||
// get the response time and check it
|
||||
auto responseTime = resetTime - startTime;
|
||||
|
||||
// cppcheck generates an internal error with 20us
|
||||
constexpr std::chrono::microseconds kMaxExpectedResponseTime(20);
|
||||
|
||||
tr_info("Reset task: response time is %lld usecs\n, expected : %lld",
|
||||
responseTime.count(),
|
||||
kMaxExpectedResponseTime.count());
|
||||
|
||||
TEST_ASSERT_TRUE(responseTime.count() <= kMaxExpectedResponseTime.count());
|
||||
|
||||
// jitter of 20us is accepted
|
||||
constexpr uint64_t kDeltaUs = 3;
|
||||
constexpr std::chrono::microseconds kMaxExpectedJitter(2);
|
||||
if (i > 0) {
|
||||
auto jitter = responseTime - lastResponseTime;
|
||||
tr_info("Reset task: jitter is %lld usecs\n", std::abs(jitter.count()));
|
||||
TEST_ASSERT_UINT64_WITHIN(
|
||||
kDeltaUs, kMaxExpectedJitter.count(), std::abs(jitter.count()));
|
||||
}
|
||||
lastResponseTime = responseTime;
|
||||
|
||||
// let the bike system run for 2 secs
|
||||
ThisThread::sleep_for(2s);
|
||||
}
|
||||
|
||||
// stop the bike system
|
||||
bikeSystem.stop();
|
||||
thread.terminate();
|
||||
tr_info("Threads have stopped");
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
Timer timerGear;
|
||||
static std::chrono::microseconds gearTime = std::chrono::microseconds::zero();
|
||||
static EventFlags eventGearFlags;
|
||||
static constexpr uint32_t kGearEventFlag = (1UL << 0);
|
||||
constexpr uint8_t kNbrOfGear = 9;
|
||||
static void onGearChange() {
|
||||
gearTime = timerGear.elapsed_time();
|
||||
eventGearFlags.set(kGearEventFlag);
|
||||
tr_info("Gear changed");
|
||||
}
|
||||
|
||||
static void gearTest(Callback<void()> gearChange) {
|
||||
// std::chrono::microseconds lastResponseTime = std::chrono::microseconds::zero();
|
||||
|
||||
for (uint8_t i = 1; i < kNbrOfGear; i++) {
|
||||
tr_info("Gear test N°%d", i);
|
||||
// take time before increase the gear
|
||||
auto startTime = timerGear.elapsed_time();
|
||||
|
||||
// change the gear with the callback
|
||||
tr_info("Change gear");
|
||||
gearChange();
|
||||
|
||||
// wait flag
|
||||
eventGearFlags.wait_all(kGearEventFlag);
|
||||
|
||||
// get the response time and check it
|
||||
auto responseTime = gearTime - startTime;
|
||||
constexpr std::chrono::microseconds kMaxExpectedResponseTime(100000);
|
||||
|
||||
tr_info("Change gear task: response time is %lld usecs\n, expected : %lld",
|
||||
responseTime.count(),
|
||||
kMaxExpectedResponseTime.count());
|
||||
|
||||
TEST_ASSERT_TRUE(responseTime.count() <= kMaxExpectedResponseTime.count());
|
||||
|
||||
// jitter of 20us is accepted
|
||||
// constexpr uint64_t kDeltaUs = 2000;
|
||||
// constexpr std::chrono::microseconds kMaxExpectedJitter(4000);
|
||||
// if (i > 1) {
|
||||
// auto jitter = responseTime - lastResponseTime;
|
||||
// tr_info("Gear task: jitter is %lld usecs\n", std::abs(jitter.count()));
|
||||
// TEST_ASSERT_UINT64_WITHIN(
|
||||
// kDeltaUs, kMaxExpectedJitter.count(), std::abs(jitter.count()));
|
||||
// }
|
||||
// lastResponseTime = responseTime;
|
||||
|
||||
// let the bike system run for 2 secs
|
||||
ThisThread::sleep_for(2s);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_gear_multi_tasking_bike_system() {
|
||||
tr_info("test reset multi tasking bike system");
|
||||
// create the BikeSystem instance
|
||||
multi_tasking::BikeSystem bikeSystem;
|
||||
|
||||
// run the bike system in a separate thread
|
||||
Thread thread;
|
||||
osStatus status =
|
||||
thread.start(callback(&bikeSystem, &multi_tasking::BikeSystem::start));
|
||||
|
||||
if (status != osOK) {
|
||||
tr_error("Thread bike system is not OK !");
|
||||
}
|
||||
|
||||
tr_info("Bike system has started");
|
||||
|
||||
// let the bike system run for 2 secs
|
||||
ThisThread::sleep_for(2s);
|
||||
|
||||
// get the gear device to call onUp and onDown functions
|
||||
multi_tasking::GearDevice& gearDevice = bikeSystem.getGearDevice();
|
||||
|
||||
// set callback for response time measuring
|
||||
bikeSystem.setCallbackGearChage(onGearChange);
|
||||
|
||||
// start the timer instance
|
||||
timerGear.start();
|
||||
|
||||
// test timing incresing the gear
|
||||
tr_info("Test incresing gear");
|
||||
gearTest(callback(&gearDevice, &multi_tasking::GearDevice::onUp));
|
||||
|
||||
// test timing decreasing the gear
|
||||
tr_info("Test decreasing gear");
|
||||
gearTest(callback(&gearDevice, &multi_tasking::GearDevice::onDown));
|
||||
|
||||
// stop the bike system
|
||||
bikeSystem.stop();
|
||||
thread.terminate();
|
||||
tr_info("Threads have stopped");
|
||||
timerGear.stop();
|
||||
}
|
||||
|
||||
static status_t greentea_setup(const size_t number_of_cases) {
|
||||
// Here, we specify the timeout (60s) and the host test (a built-in host test
|
||||
// or the name of our Python file)
|
||||
@ -152,7 +389,10 @@ static status_t greentea_setup(const size_t number_of_cases) {
|
||||
static Case cases[] = {
|
||||
Case("test bike system", test_bike_system),
|
||||
Case("test bike system with event queue", test_bike_system_event_queue),
|
||||
Case("test bike system with event handling", test_bike_system_with_event),
|
||||
Case("test bike system with event", test_bike_system_with_event),
|
||||
Case("test multi-tasking bike system", test_multi_tasking_bike_system),
|
||||
Case("test reset multi-tasking bike system", test_reset_multi_tasking_bike_system),
|
||||
Case("test gear multi-tasking bike system", test_gear_multi_tasking_bike_system),
|
||||
};
|
||||
|
||||
static Specification specification(greentea_setup, cases);
|
||||
@ -160,4 +400,9 @@ static Specification specification(greentea_setup, cases);
|
||||
}; // namespace v1
|
||||
}; // namespace utest
|
||||
|
||||
int main() { return !utest::v1::Harness::run(utest::v1::specification); }
|
||||
int main() {
|
||||
#if defined(MBED_CONF_MBED_TRACE_ENABLE)
|
||||
mbed_trace_init();
|
||||
#endif
|
||||
return !utest::v1::Harness::run(utest::v1::specification);
|
||||
}
|
||||
|
Reference in New Issue
Block a user