Skip to content

Commit

Permalink
feat: completed file based testing
Browse files Browse the repository at this point in the history
  • Loading branch information
charliekush committed Jul 19, 2024
1 parent 77a64d1 commit 985663e
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/cli/flog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const FLOG_Message_t FLOG_Message[] = {
{FLOG_ICM_FAIL, "ICM Fail"},

{FLOG_RIDE_INIT_TIMEOUT, "Ride init Timeout"},
{FLOG_SCHEDULER_FAILED, "Scheduler failed"},
{FLOG_SCHEDULER_DELAY_EXCEEDED,"Ensemble skipped"},

{FLOG_UPLOAD_NO_UPLOAD, "Upload - No Upload Flag set"},
{FLOG_UPL_BATT_LOW, "Upload Battery low"},
Expand Down
1 change: 1 addition & 0 deletions src/cli/flog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef enum FLOG_CODE_

FLOG_RIDE_INIT_TIMEOUT=0x0401,
FLOG_SCHEDULER_FAILED=0x0402,
FLOG_SCHEDULER_DELAY_EXCEEDED=0x0403,

FLOG_UPLOAD_NO_UPLOAD =0x0501,
FLOG_UPL_BATT_LOW =0x0502,
Expand Down
40 changes: 29 additions & 11 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ensembles.hpp"
#include "scheduler.hpp"
#include "consts.hpp"
#include <cli/flog.hpp>
#include <string.h>


Expand All @@ -36,8 +37,6 @@ void SCH_initializeSchedule(DeploymentSchedule_t* pDeployment,
pDeployment->state.deploymentStartTime = startTime;
pDeployment->state.firstRunTime = UINT32_MAX;
pDeployment->state.nMeasurements = UINT32_MAX;
pDeployment->state.nMeasurements = UINT32_MAX;

pDeployment->init(pDeployment);
pDeployment++;
}
Expand Down Expand Up @@ -82,29 +81,36 @@ uint32_t SCH_getNextEvent(DeploymentSchedule_t* scheduleTable,
{
// Reference to current event in the schedule
DeploymentSchedule_t& currentEvent = scheduleTable[idx];
// Reference to current event in the schedule
StateInformation& s = scheduleTable[idx].state;
// Reset the next run time for the current event
currentEvent.state.nextRunTime = UINT32_MAX;
s.nextRunTime = UINT32_MAX;
// Variable to store the calculated start time of the next event.
uint32_t nextStartTime;
// Calculate first start time after delay.
uint32_t firstStartTime = currentEvent.state.deploymentStartTime +
uint32_t firstStartTime = s.deploymentStartTime +
currentEvent.ensembleDelay;
if ( s.firstRunTime != UINT32_MAX)
{
firstStartTime = s.firstRunTime;
}


// check if first event
if (currentTime <= firstStartTime)
{
s.firstRunTime = firstStartTime;
if (firstStartTime < minNextTime &&
!SCH_willOverlap(scheduleTable,
idx, currentTime, firstStartTime))
{
minNextTime = firstStartTime;
nextEvent = &currentEvent;

}
continue;
}
// Calculate intended count
uint32_t intendedCount = (currentTime - firstStartTime - 1) /
uint32_t intendedCount = (currentTime - s.firstRunTime - 1) /
currentEvent.ensembleInterval + 1;
uint32_t lastInterval = firstStartTime + (intendedCount - 1) *
currentEvent.ensembleInterval;
Expand All @@ -115,10 +121,10 @@ uint32_t SCH_getNextEvent(DeploymentSchedule_t* scheduleTable,


// check if measurement is late
if ((currentEvent.state.lastMeasurementTime < lastInterval) ||
((currentEvent.state.lastMeasurementTime == firstStartTime) &&
if ((s.lastMeasurementTime < lastInterval) ||
((s.lastMeasurementTime == firstStartTime) &&
(firstStartTime < currentTime) &&
(currentEvent.state.measurementCount == 0)))
(s.measurementCount == 0)))
{

if (currentTime + currentEvent.maxDuration > nextInterval)
Expand All @@ -137,11 +143,20 @@ uint32_t SCH_getNextEvent(DeploymentSchedule_t* scheduleTable,

if (!SCH_willOverlap(scheduleTable, idx, currentTime, nextStartTime))
{
currentEvent.state.nextRunTime = nextStartTime;
s.nextRunTime = nextStartTime;
if (nextStartTime < minNextTime)
{
nextEvent = &currentEvent;
minNextTime = nextStartTime;
if (nextStartTime - s.lastMeasurementTime >
currentEvent.maxDelay)
{
s.firstRunTime = minNextTime;
FLOG_AddError(FLOG_SCHEDULER_DELAY_EXCEEDED,
s.measurementCount);
SF_OSAL_printf("Task %s skipped at time %zu" __NL__ ,
currentEvent.taskName,currentTime);
}
}
}

Expand All @@ -156,7 +171,10 @@ uint32_t SCH_getNextEvent(DeploymentSchedule_t* scheduleTable,
else
{


if (nextEvent->state.firstRunTime == UINT32_MAX)
{
nextEvent->state.firstRunTime = minNextTime;
}
// Sets ensemble variable lastMeasurementTime to next time it will run
nextEvent->state.lastMeasurementTime = minNextTime;
// Incraments ensemble variable lastMeasurementTime measurementCount
Expand Down
43 changes: 18 additions & 25 deletions tests/file_google_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
public:
static void SetUpTestSuite()
{
files = std::make_unique<FileWriter>("expected_file_tests.log",
"actual_file_tests.log");
files = std::make_unique<FileWriter>("outputs/expected_file_tests.log",
"outputs/actual_file_tests.log");

}
static void TearDownTestSuite()
Expand Down Expand Up @@ -111,6 +111,8 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>

nextEvent = nullptr; // ensures that first call to scheduler is correct
nextEventTime = 0; // time handling

setTime(0);

}

Expand All @@ -120,8 +122,10 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
*/
void TearDown() override
{

size_t pos = testName.find_last_of("/");

files->writeTest(testName,expected,actual);
files->writeTest(testName.substr(pos+1),expected,actual);
}
void runNextEvent()
{
Expand All @@ -144,7 +148,7 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
void runTestFile(std::string filename)
{
input = parseInputFile(filename);
std::cout << input.serialize() << "\n";

setTime(input.start);
deploymentSchedule.clear();
DeploymentSchedule_t e;
Expand Down Expand Up @@ -189,8 +193,6 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
break;
}
}
ASSERT_TRUE(input.expectedValues.size() > 0)
<< "Not enough expected values were provided!";
if (input.expectedValues.size() > 0)
{
TestLog exp = input.expectedValues.front();
Expand All @@ -200,7 +202,7 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
}
else
{
runNextEvent();
return;
}

}
Expand Down Expand Up @@ -255,19 +257,18 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
iss >> out.end;
}
else if (currentSection == "ENSEMBLES") {
//A|2000|200
//B|2000|400|1000

std::string name;

uint32_t interval, duration, maxDelay;
std::getline(iss, name, '|');
std::cout << "name: " << name <<"\n";

iss >> interval;
std::cout << "interval: " << interval <<"\n";


iss.ignore(1, '|');
iss >> duration;
std::cout << "duration: " << duration <<"\n";

char checkChar = iss.peek();
if (checkChar == '|') {
iss.ignore(1, '|');
Expand Down Expand Up @@ -310,19 +311,11 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
TestLog exp(expectedTaskName.c_str(), expectedStart,
exepectedEnd);
out.expectedValues.push_back(exp);
for (const auto& ensemble : out.expectedValues) {
std::cout << "Expected Name: " << ensemble.name << ", start: " << ensemble.start << ", end: " << ensemble.end << "\n";
}

std::cout << "\n";
}
}

for (const auto& ensemble : out.ensembles) {
std::cout << "Task Name: " << ensemble.taskName << ", Interval: " << ensemble.interval << ", Duration: " << ensemble.duration << ", Delay: " << ensemble.delay << "\n";
}
for (const auto& ensemble : out.expectedValues) {
std::cout << "Expected Name: " << ensemble.name << ", start: " << ensemble.start << ", end: " << ensemble.end << "\n";
}

return out;
}
Expand Down Expand Up @@ -376,7 +369,7 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
uint32_t actualEnd = millis();

appendExpectedFile(expectedTaskName, expectedStart, expectedEnd);
appendActualFile(nextEvent->taskName, nextEventTime, millis());
appendActualFile(nextEvent->taskName, actualStart, actualEnd);

ASSERT_EQ(expectedTaskName, nextEvent->taskName) << failMessage.str();
ASSERT_EQ(expectedStart, actualStart) << failMessage.str();
Expand All @@ -396,7 +389,7 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
std::unique_ptr<FileWriter> SchedulerTestsFromFiles::files;


TEST_P(SchedulerTestsFromFiles, ReadsFileCorrectly) {
TEST_P(SchedulerTestsFromFiles, RunTestFile) {
std::string filename = GetParam();

runTestFile(filename);
Expand Down Expand Up @@ -425,10 +418,10 @@ INSTANTIATE_TEST_SUITE_P(
std::string name = info.param;
// Remove directory part for cleaner test names
size_t last_slash_idx = name.find_last_of("\\/");
if (std::string::npos != last_slash_idx) {
if (std::string::npos != last_slash_idx)
{
name.erase(0, last_slash_idx + 1);
}

std::replace(name.begin(), name.end(), '.', '_');
return name;
}
Expand Down
23 changes: 14 additions & 9 deletions tests/fixed_google_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <filesystem>
#include <sstream>
#include <memory>

#include "flog.hpp"


class SchedulerFixedTests : public ::testing::Test
Expand Down Expand Up @@ -51,8 +51,9 @@ class SchedulerFixedTests : public ::testing::Test

static void SetUpTestSuite()
{
files = std::make_unique<FileWriter>("expected_fixed_tests.log",
"actual_fixed_tests.log");
files = std::make_unique<FileWriter>("outputs/expected_fixed_tests.log",
"outputs/actual_fixed_tests.log");
FLOG_Initialize();
}
static void TearDownTestSuite()
{
Expand Down Expand Up @@ -473,7 +474,7 @@ TEST_F(SchedulerFixedTests, SingleEvent_SecondEndAfterThirdStart)
}

/**
* @brief Seccond iteration ends when third iteration woas supposed to end time
* @brief Seccond iteration ends when third iteration was supposed to end time
* task end delayed past when second event is supposed to start, second
* event would now start when third run would start,
* so second event is skipped
Expand Down Expand Up @@ -624,27 +625,31 @@ TEST_F(SchedulerFixedTests, DelayAndSkip)

runNextEvent();
runNextEvent();
addTime(900); //delay
runNextEvent();

runNextEvent();
runNextEvent();
addTime(900); //delay
ASSERT_TRUE(SCH_willOverlap(deploymentSchedule.data(), 2,
millis(), millis()));
SCH_getNextEvent(deploymentSchedule.data(), &nextEvent,
&nextEventTime, millis());
runAndCheckEvent("A", 2000, 2400);
runAndCheckEvent("A", 4000, 4400);
SCH_getNextEvent(deploymentSchedule.data(), &nextEvent,
&nextEventTime, millis());
runAndCheckEvent("B", 2400, 2600);
runAndCheckEvent("B", 4400, 4600);


SCH_getNextEvent(deploymentSchedule.data(), &nextEvent,
&nextEventTime, millis());//C runs
runAndCheckEvent("C", 2600, 3200);
runAndCheckEvent("C", 4600, 5200);
SCH_getNextEvent(deploymentSchedule.data(), &nextEvent,
&nextEventTime, millis());
runAndCheckEvent("A", 4000, 4400);
runAndCheckEvent("A", 6000, 6400);

// check for cascades
checkIdeal(10000);
FLOG_DisplayLog();


}
Expand Down
19 changes: 19 additions & 0 deletions tests/inputs/delay.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
START #only one number
0
END #only one number
10000
ENSEMBLES #taskname|interval|duration or taskname|interval|duration|maxDelay
A|2000|400
B|2000|200
C|2000|600
DELAYS #0 indexed: "taskname|iteration|delay" or "taskname|iteration|delay|before" (after implied)
B|1|900
EXPECTED #"taskname|start|end"
A|0|400
B|400|600
C|600|1200
A|2000|2400
B|2400|3500
A|4000|4400
B|4400|4600
C|4600|5200
4 changes: 2 additions & 2 deletions tests/overlap_google_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class SchedulerOverlapTests : public ::testing::Test

static void SetUpTestSuite()
{
files = std::make_unique<FileWriter>("expected_overlap_tests.log",
"actual_overlap_tests.log");
files = std::make_unique<FileWriter>("outputs/expected_overlap_tests.log",
"outputs/actual_overlap_tests.log");
}
static void TearDownTestSuite()
{
Expand Down
3 changes: 2 additions & 1 deletion tests/run_gtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

cmake .. -Bbuild/
make -Cbuild/
./build/googletests
mkdir -p outputs
./build/googletests



#create graphs
Expand Down
Loading

0 comments on commit 985663e

Please sign in to comment.