test-scheduler.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // test_scheduler.cpp
  2. #include <gtest/gtest.h>
  3. #include "Arduino.h"
  4. #include "TaskScheduler.h"
  5. // Define the global test output vector
  6. std::vector<std::string> test_output;
  7. // Test callback functions
  8. void task1_callback() {
  9. test_output.push_back("Task1 executed");
  10. std::cout << "Task1 executed at " << millis() << "ms" << std::endl;
  11. }
  12. void task2_callback() {
  13. test_output.push_back("Task2 executed");
  14. std::cout << "Task2 executed at " << millis() << "ms" << std::endl;
  15. }
  16. void task3_callback() {
  17. test_output.push_back("Task3 executed");
  18. std::cout << "Task3 executed at " << millis() << "ms" << std::endl;
  19. }
  20. void repeating_callback() {
  21. static int counter = 0;
  22. counter++;
  23. test_output.push_back("Repeating task #" + std::to_string(counter));
  24. std::cout << "Repeating task #" << counter << " executed at " << millis() << "ms" << std::endl;
  25. }
  26. class SchedulerTest : public ::testing::Test {
  27. protected:
  28. void SetUp() override {
  29. clearTestOutput();
  30. // Reset time by creating new static start point
  31. millis(); // Initialize timing
  32. }
  33. void TearDown() override {
  34. clearTestOutput();
  35. }
  36. // Helper to run scheduler until condition is met or timeout
  37. bool runSchedulerUntil(Scheduler& ts, std::function<bool()> condition, unsigned long timeout_ms = 1000) {
  38. return waitForCondition([&]() {
  39. ts.execute();
  40. return condition();
  41. }, timeout_ms);
  42. }
  43. };
  44. TEST_F(SchedulerTest, BasicSchedulerCreation) {
  45. Scheduler ts;
  46. EXPECT_TRUE(true); // Scheduler creation should not throw
  47. }
  48. TEST_F(SchedulerTest, SchedulerInitialState) {
  49. Scheduler ts;
  50. // Execute empty scheduler - should not crash
  51. ts.execute();
  52. EXPECT_EQ(getTestOutputCount(), 0);
  53. }
  54. TEST_F(SchedulerTest, SingleTaskExecution) {
  55. Scheduler ts;
  56. // Create a task that runs once after 100ms
  57. Task task1(100, 1, &task1_callback, &ts, true);
  58. // Run scheduler until task executes
  59. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  60. EXPECT_TRUE(success) << "Task did not execute within timeout";
  61. EXPECT_EQ(getTestOutputCount(), 1);
  62. EXPECT_EQ(getTestOutput(0), "Task1 executed");
  63. }
  64. TEST_F(SchedulerTest, MultipleTaskExecution) {
  65. Scheduler ts;
  66. // Create multiple tasks with different intervals
  67. Task task1(50, 1, &task1_callback, &ts, true); // Run once after 50ms
  68. Task task2(100, 1, &task2_callback, &ts, true); // Run once after 100ms
  69. Task task3(150, 1, &task3_callback, &ts, true); // Run once after 150ms
  70. // Run scheduler until all tasks execute
  71. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 3; });
  72. EXPECT_TRUE(success) << "Not all tasks executed within timeout";
  73. EXPECT_EQ(getTestOutputCount(), 3);
  74. EXPECT_EQ(getTestOutput(0), "Task1 executed");
  75. EXPECT_EQ(getTestOutput(1), "Task2 executed");
  76. EXPECT_EQ(getTestOutput(2), "Task3 executed");
  77. }
  78. TEST_F(SchedulerTest, RepeatingTaskExecution) {
  79. Scheduler ts;
  80. // Create a task that runs 3 times with 80ms interval
  81. Task repeating_task(80, 3, &repeating_callback, &ts, true);
  82. // Run scheduler until task executes 3 times
  83. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 3; }, 1500);
  84. EXPECT_TRUE(success) << "Repeating task did not complete within timeout";
  85. EXPECT_EQ(getTestOutputCount(), 3);
  86. EXPECT_EQ(getTestOutput(0), "Repeating task #1");
  87. EXPECT_EQ(getTestOutput(1), "Repeating task #2");
  88. EXPECT_EQ(getTestOutput(2), "Repeating task #3");
  89. }
  90. TEST_F(SchedulerTest, InfiniteRepeatingTask) {
  91. Scheduler ts;
  92. // Create a task that runs indefinitely with 50ms interval
  93. Task infinite_task(50, TASK_FOREVER, &repeating_callback, &ts, true);
  94. // Run for a specific time and count executions
  95. unsigned long start_time = millis();
  96. while (millis() - start_time < 250) { // Run for 250ms
  97. ts.execute();
  98. delay(10);
  99. }
  100. // Should have executed approximately 250/50 = 5 times (allowing for timing variance)
  101. EXPECT_GE(getTestOutputCount(), 3);
  102. EXPECT_LE(getTestOutputCount(), 7);
  103. }
  104. TEST_F(SchedulerTest, TaskEnableDisable) {
  105. Scheduler ts;
  106. // Create a disabled task
  107. Task task1(100, 1, &task1_callback, &ts, false);
  108. // Run scheduler - task should not execute (it's disabled)
  109. delay(150);
  110. ts.execute();
  111. EXPECT_EQ(getTestOutputCount(), 0);
  112. // Now enable the task
  113. task1.enable();
  114. // Run scheduler - task should execute now
  115. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  116. EXPECT_TRUE(success) << "Task did not execute after being enabled";
  117. EXPECT_EQ(getTestOutputCount(), 1);
  118. EXPECT_EQ(getTestOutput(0), "Task1 executed");
  119. }
  120. TEST_F(SchedulerTest, TaskDisableDuringExecution) {
  121. Scheduler ts;
  122. // Create a repeating task
  123. Task repeating_task(60, TASK_FOREVER, &repeating_callback, &ts, true);
  124. // Let it run a few times
  125. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  126. EXPECT_TRUE(success);
  127. size_t executions_before_disable = getTestOutputCount();
  128. // Disable the task
  129. repeating_task.disable();
  130. // Continue running scheduler
  131. delay(200);
  132. for (int i = 0; i < 10; i++) {
  133. ts.execute();
  134. delay(20);
  135. }
  136. // Should not have executed any more times
  137. EXPECT_EQ(getTestOutputCount(), executions_before_disable);
  138. }
  139. TEST_F(SchedulerTest, SchedulerWithNoTasks) {
  140. Scheduler ts;
  141. // Execute scheduler with no tasks multiple times
  142. for (int i = 0; i < 100; i++) {
  143. ts.execute();
  144. delay(1);
  145. }
  146. EXPECT_EQ(getTestOutputCount(), 0);
  147. }
  148. TEST_F(SchedulerTest, TaskExecutionOrder) {
  149. Scheduler ts;
  150. // Create tasks that should execute in specific order
  151. Task task_late(200, 1, &task3_callback, &ts, true); // Latest
  152. Task task_early(50, 1, &task1_callback, &ts, true); // Earliest
  153. Task task_mid(100, 1, &task2_callback, &ts, true); // Middle
  154. // Run until all execute
  155. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 3; });
  156. EXPECT_TRUE(success);
  157. EXPECT_EQ(getTestOutputCount(), 3);
  158. // Tasks should execute in chronological order regardless of creation order
  159. EXPECT_EQ(getTestOutput(0), "Task1 executed"); // First (50ms)
  160. EXPECT_EQ(getTestOutput(1), "Task2 executed"); // Second (100ms)
  161. EXPECT_EQ(getTestOutput(2), "Task3 executed"); // Third (200ms)
  162. }
  163. TEST_F(SchedulerTest, SchedulerHandlesLargeNumberOfTasks) {
  164. Scheduler ts;
  165. std::vector<std::unique_ptr<Task>> tasks;
  166. // Create many tasks with similar timing
  167. for (int i = 0; i < 10; i++) {
  168. auto task = std::make_unique<Task>(100 + i * 10, 1, &task1_callback, &ts, true);
  169. tasks.push_back(std::move(task));
  170. }
  171. // Run until all tasks execute
  172. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 10; }, 2000);
  173. EXPECT_TRUE(success) << "Not all tasks executed within timeout";
  174. EXPECT_EQ(getTestOutputCount(), 10);
  175. }
  176. int main(int argc, char **argv) {
  177. ::testing::InitGoogleTest(&argc, argv);
  178. return RUN_ALL_TESTS();
  179. }