Skip to content

Commit

Permalink
add test and fix bug caught by test
Browse files Browse the repository at this point in the history
  • Loading branch information
m-m-adams committed Jan 18, 2025
1 parent 77d4bef commit 893b6ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/OSLikeStuff/task_scheduler/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct Task {
[[nodiscard]] bool isReady(Time currentTime) const {
return state == State::READY && isReleased(currentTime) && resourcesAvailable();
};
[[nodiscard]] bool isRunnable() const { return state == State::READY; }
[[nodiscard]] bool isRunnable() const { return state == State::READY && resourcesAvailable(); }
[[nodiscard]] bool isReleased(Time currentTime) const {
return currentTime - lastFinishTime > schedule.backOffPeriod;
}
Expand Down
1 change: 0 additions & 1 deletion src/OSLikeStuff/task_scheduler/task_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "io/debug/log.h"
#include "resource_checker.h"
#include <algorithm>
#include <iostream>

#if !IN_UNIT_TESTS
#include "memory/general_memory_allocator.h"
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/scheduler_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ void yield_2ms() {
yield([]() { return getTimerValueSeconds(0) > started + Time(0.002); });
}

void yield_2ms_with_lock() {
mock().actualCall("yield_2ms");
started = getTimerValueSeconds(0);
usbLock = true;
yield([]() { return getTimerValueSeconds(0) > started + Time(0.002); });
usbLock = false;
}

TEST_GROUP(Scheduler){

void setup(){taskManager = TaskManager();
Expand Down Expand Up @@ -234,5 +242,17 @@ TEST(Scheduler, overSchedule) {

mock().checkExpectations();
};
TEST(Scheduler, yield_with_lock) {
mock().clear();
// will be locked out by the usb lock
mock().expectNCalls(0, "sleep_50ns");
mock().expectNCalls(1, "yield_2ms");
// every 1ms sleep for 50ns and 10ns
addRepeatingTask(sleep_50ns, 10, 0.0001, 0.0001, 0.0001, "sleep_50ns", USB);

addOnceTask(yield_2ms_with_lock, 2, 0, "sleep 2ms", USB);
// run the scheduler for 10ms
taskManager.start(0.002);
mock().checkExpectations();
};
} // namespace

0 comments on commit 893b6ca

Please sign in to comment.