test-scheduler-basic-thorough-no-lambda.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992
  1. // test-scheduler-basic-thorough-no-lambda.cpp - Comprehensive TaskScheduler unit tests without lambda functions
  2. // This file contains comprehensive tests for basic TaskScheduler functionality
  3. // without using lambda functions, ensuring compatibility with all Arduino platforms
  4. //
  5. // =====================================================================================
  6. // COMPREHENSIVE TASKSCHEDULER TEST PLAN AND COVERAGE MATRIX (NO LAMBDA VERSION)
  7. // =====================================================================================
  8. //
  9. // PURPOSE: Validate all basic TaskScheduler methods and patterns without compile options
  10. // APPROACH: Traditional function pointers and callback functions for maximum compatibility
  11. // SCOPE: Core functionality excluding advanced features requiring compile-time directives
  12. //
  13. // COVERAGE MATRIX:
  14. // ================
  15. //
  16. // 1. TASK CONSTRUCTOR TESTS
  17. // ├── Default Constructor
  18. // │ └── Task() - creates inert task with safe defaults
  19. // ├── Parameterized Constructor
  20. // │ └── Task(interval, iterations, callback, scheduler, enable) - full configuration
  21. // ├── Lifecycle Constructor
  22. // │ └── Task(..., onEnable, onDisable) - with lifecycle callbacks
  23. // └── Auto-enabled Constructor
  24. // └── Task(..., enable=true) - immediate activation patterns
  25. //
  26. // 2. TASK INFORMATION METHODS
  27. // ├── State Queries
  28. // │ ├── isEnabled() - current activation state
  29. // │ ├── getInterval() - execution timing interval
  30. // │ ├── getIterations() - remaining execution count
  31. // │ └── getRunCounter() - total executions completed
  32. // └── Lifecycle Tracking
  33. // └── Information accuracy across task lifecycle stages
  34. //
  35. // 3. TASK CONTROL METHODS
  36. // ├── Basic Control
  37. // │ ├── enable() - activate task for execution
  38. // │ ├── disable() - deactivate task execution
  39. // │ └── enableIfNot() - conditional enabling
  40. // ├── Restart Operations
  41. // │ ├── restart() - reset iteration count and re-enable
  42. // │ └── restartDelayed(delay) - restart with initial delay
  43. // └── State Management
  44. // └── Control method interactions and state consistency
  45. //
  46. // 4. TASK TIMING METHODS
  47. // ├── Execution Control
  48. // │ ├── delay(ms) - postpone next execution
  49. // │ ├── forceNextIteration() - immediate execution trigger
  50. // │ └── enableDelayed(delay) - delayed activation
  51. // └── Timing Behavior
  52. // └── Interval and delay interactions with scheduler
  53. //
  54. // 5. TASK CONFIGURATION METHODS
  55. // ├── Parameter Configuration
  56. // │ ├── set(interval, iterations, callback, onEnable, onDisable) - complete reconfiguration
  57. // │ ├── setInterval(interval) - dynamic timing changes
  58. // │ └── setIterations(iterations) - dynamic repeat count
  59. // └── Callback Management
  60. // ├── setCallback(callback) - change execution function
  61. // ├── setOnEnable(callback) - change enable callback
  62. // └── setOnDisable(callback) - change disable callback
  63. //
  64. // 6. TASK ITERATION STATE METHODS
  65. // ├── Execution Context
  66. // │ ├── isFirstIteration() - detect initial execution
  67. // │ └── isLastIteration() - detect final execution
  68. // └── Context-Aware Programming
  69. // └── Conditional logic based on iteration position
  70. //
  71. // 7. TASK CALLBACK SWITCHING METHODS
  72. // ├── State Machine Support
  73. // │ ├── yield(callback) - permanent callback change
  74. // │ └── yieldOnce(callback) - single execution callback
  75. // └── Multi-Phase Processing
  76. // └── Sequential callback execution patterns
  77. //
  78. // 8. TASK LIFECYCLE CALLBACKS
  79. // ├── Enable Events
  80. // │ ├── onEnable() callbacks - activation event handling
  81. // │ └── Conditional enabling via return values
  82. // └── Disable Events
  83. // └── onDisable() callbacks - deactivation event handling
  84. //
  85. // 9. SCHEDULER CONSTRUCTOR AND MANAGEMENT
  86. // ├── Basic Operations
  87. // │ ├── Scheduler() - empty scheduler creation
  88. // │ ├── execute() - task execution cycle
  89. // │ └── init() - scheduler reinitialization
  90. // └── Robustness
  91. // └── Safe operation with no tasks or empty states
  92. //
  93. // 10. SCHEDULER TASK MANAGEMENT
  94. // ├── Manual Task Control
  95. // │ ├── addTask(task) - explicit task registration
  96. // │ └── deleteTask(task) - explicit task removal
  97. // └── Dynamic Management
  98. // └── Runtime task addition and removal patterns
  99. //
  100. // 11. SCHEDULER EXECUTION CONTROL
  101. // ├── Execution Monitoring
  102. // │ ├── execute() return values - idle detection
  103. // │ ├── enableAll() - bulk task activation
  104. // │ └── disableAll() - bulk task deactivation
  105. // └── System Control
  106. // └── Coordinated task state management
  107. //
  108. // 12. SCHEDULER TIME QUERIES
  109. // ├── Timing Information
  110. // │ └── timeUntilNextIteration(task) - predictive timing
  111. // └── Scheduling Coordination
  112. // └── Task timing coordination and synchronization
  113. //
  114. // 13. SCHEDULER TASK ACCESS
  115. // ├── Context Information
  116. // │ └── getCurrentTask() - callback context identification
  117. // └── Self-Reference Patterns
  118. // └── Task self-modification and introspection
  119. //
  120. // 14. SCHEDULER TIMING CONTROL
  121. // ├── Global Control
  122. // │ └── startNow() - immediate execution trigger
  123. // └── Synchronized Operations
  124. // └── Coordinated task restart and execution
  125. //
  126. // 15. INTEGRATION TESTS
  127. // ├── Complete Lifecycle
  128. // │ └── Full task lifecycle with all features
  129. // └── Multi-Task Coordination
  130. // └── Complex scenarios with multiple concurrent tasks
  131. //
  132. // 16. EDGE CASES AND ERROR HANDLING
  133. // ├── Boundary Conditions
  134. // │ ├── Zero iterations - invalid execution counts
  135. // │ ├── Infinite iterations - TASK_FOREVER handling
  136. // │ └── Null callbacks - robustness testing
  137. // └── Defensive Programming
  138. // └── Safe handling of invalid inputs and states
  139. //
  140. // =====================================================================================
  141. #include <gtest/gtest.h>
  142. #include "Arduino.h"
  143. #include "TaskScheduler.h"
  144. // Global test state - used to capture and verify callback executions
  145. std::vector<std::string> test_output;
  146. int callback_counter = 0;
  147. bool onEnable_called = false;
  148. bool onDisable_called = false;
  149. // Test callback functions (no lambda functions)
  150. /**
  151. * @brief Basic callback function for general testing
  152. *
  153. * Simple callback that increments counter and records execution.
  154. * Used for fundamental task execution verification.
  155. */
  156. void basic_callback() {
  157. callback_counter++;
  158. test_output.push_back("basic_callback");
  159. std::cout << "Basic callback executed at " << millis() << "ms" << std::endl;
  160. }
  161. /**
  162. * @brief First test callback for multi-callback scenarios
  163. *
  164. * Used to distinguish between different callbacks in multi-task tests.
  165. */
  166. void callback_1() {
  167. callback_counter++;
  168. test_output.push_back("callback_1");
  169. }
  170. /**
  171. * @brief Second test callback for multi-callback scenarios
  172. *
  173. * Used in coordination tests and callback switching verification.
  174. */
  175. void callback_2() {
  176. callback_counter++;
  177. test_output.push_back("callback_2");
  178. }
  179. /**
  180. * @brief Third test callback for complex scenarios
  181. *
  182. * Additional callback for testing multiple task coordination.
  183. */
  184. void callback_3() {
  185. callback_counter++;
  186. test_output.push_back("callback_3");
  187. }
  188. /**
  189. * @brief Multi-step callback for yield testing - step 1
  190. *
  191. * First phase of multi-step processing for callback switching tests.
  192. */
  193. void multi_step_callback_1() {
  194. test_output.push_back("step_1");
  195. }
  196. /**
  197. * @brief Multi-step callback for yield testing - step 2
  198. *
  199. * Second phase of multi-step processing for callback switching tests.
  200. */
  201. void multi_step_callback_2() {
  202. test_output.push_back("step_2");
  203. }
  204. /**
  205. * @brief Multi-step callback for yield testing - step 3
  206. *
  207. * Third phase of multi-step processing for callback switching tests.
  208. */
  209. void multi_step_callback_3() {
  210. test_output.push_back("step_3");
  211. }
  212. /**
  213. * @brief OnEnable lifecycle callback that allows enabling
  214. *
  215. * Returns true to allow task enabling. Used for testing
  216. * normal lifecycle callback behavior.
  217. */
  218. bool test_onEnable() {
  219. onEnable_called = true;
  220. test_output.push_back("onEnable_called");
  221. return true;
  222. }
  223. /**
  224. * @brief OnEnable lifecycle callback that prevents enabling
  225. *
  226. * Returns false to prevent task enabling. Used for testing
  227. * conditional enabling behavior.
  228. */
  229. bool test_onEnable_false() {
  230. onEnable_called = true;
  231. test_output.push_back("onEnable_called_false");
  232. return false;
  233. }
  234. /**
  235. * @brief OnDisable lifecycle callback
  236. *
  237. * Called when task is disabled. Used for testing
  238. * lifecycle callback behavior.
  239. */
  240. void test_onDisable() {
  241. onDisable_called = true;
  242. test_output.push_back("onDisable_called");
  243. }
  244. /**
  245. * @brief Repeating callback for iteration testing
  246. *
  247. * Uses static counter to track execution number, enabling verification
  248. * of proper iteration counting and repeated execution behavior.
  249. */
  250. void repeating_callback() {
  251. static int counter = 0;
  252. counter++;
  253. test_output.push_back("Repeating task #" + std::to_string(counter));
  254. std::cout << "Repeating task #" << counter << " executed at " << millis() << "ms" << std::endl;
  255. }
  256. /**
  257. * @brief Callback for yield switching tests
  258. *
  259. * Records execution and prepares for yield switching behavior.
  260. */
  261. void yield_test_callback() {
  262. test_output.push_back("yield_original");
  263. }
  264. /**
  265. * @brief Callback for yield-once testing
  266. *
  267. * Records execution for yield-once verification.
  268. */
  269. void yield_once_callback() {
  270. test_output.push_back("yield_once_executed");
  271. }
  272. // Test fixture for comprehensive scheduler testing
  273. class SchedulerThoroughTest : public ::testing::Test {
  274. protected:
  275. void SetUp() override {
  276. clearTestOutput();
  277. callback_counter = 0;
  278. onEnable_called = false;
  279. onDisable_called = false;
  280. millis(); // Initialize timing
  281. }
  282. void TearDown() override {
  283. clearTestOutput();
  284. callback_counter = 0;
  285. onEnable_called = false;
  286. onDisable_called = false;
  287. }
  288. bool runSchedulerUntil(Scheduler& ts, std::function<bool()> condition, unsigned long timeout_ms = 1000) {
  289. return waitForCondition([&]() {
  290. ts.execute();
  291. return condition();
  292. }, timeout_ms);
  293. }
  294. };
  295. // ================== TASK CONSTRUCTOR TESTS ==================
  296. /**
  297. * @brief Test Task default constructor behavior
  298. *
  299. * TESTS: Task()
  300. *
  301. * PURPOSE: Verify that a Task created with the default constructor initializes
  302. * all properties to safe default values and is in a predictable state.
  303. *
  304. * EXPECTATIONS:
  305. * - Task should be disabled by default (safety)
  306. * - Interval should be 0 (no automatic execution)
  307. * - Iterations should be 0 (won't execute)
  308. * - RunCounter should be 0 (hasn't executed yet)
  309. *
  310. * IMPORTANCE: Default constructor must create a safe, inert task that won't
  311. * execute unexpectedly. This is critical for safe initialization patterns.
  312. */
  313. TEST_F(SchedulerThoroughTest, TaskDefaultConstructor) {
  314. Task task;
  315. // Verify task is in safe, inert state after default construction
  316. EXPECT_FALSE(task.isEnabled()); // Should not execute without explicit enable
  317. EXPECT_EQ(task.getInterval(), 0); // No automatic timing
  318. EXPECT_EQ(task.getIterations(), 0); // Won't execute without iterations set
  319. EXPECT_EQ(task.getRunCounter(), 0); // No executions yet
  320. }
  321. /**
  322. * @brief Test Task parameterized constructor with all options
  323. *
  324. * TESTS: Task(interval, iterations, callback, scheduler, enable)
  325. *
  326. * PURPOSE: Verify that the full constructor properly sets all task parameters
  327. * and correctly associates the task with a scheduler.
  328. *
  329. * PARAMETERS TESTED:
  330. * - interval: 1000ms (1 second execution interval)
  331. * - iterations: 5 (task will execute 5 times then auto-disable)
  332. * - callback: basic_callback function pointer
  333. * - scheduler: reference to scheduler for automatic addition
  334. * - enable: false (task starts disabled for manual control)
  335. *
  336. * EXPECTATIONS:
  337. * - All parameters should be set exactly as specified
  338. * - Task should be disabled initially (enable=false)
  339. * - RunCounter starts at 0 (no executions yet)
  340. * - Task should be automatically added to scheduler's chain
  341. *
  342. * IMPORTANCE: This tests the most common Task creation pattern and ensures
  343. * all parameters are correctly stored and accessible.
  344. */
  345. TEST_F(SchedulerThoroughTest, TaskParameterizedConstructor) {
  346. Scheduler ts;
  347. Task task(1000, 5, &basic_callback, &ts, false);
  348. // Verify all constructor parameters were set correctly
  349. EXPECT_FALSE(task.isEnabled()); // enable=false parameter
  350. EXPECT_EQ(task.getInterval(), 1000); // 1000ms interval parameter
  351. EXPECT_EQ(task.getIterations(), 5); // 5 iterations parameter
  352. EXPECT_EQ(task.getRunCounter(), 0); // No executions yet
  353. // Note: callback and scheduler assignment tested in execution tests
  354. }
  355. /**
  356. * @brief Test Task constructor with OnEnable/OnDisable callbacks
  357. *
  358. * TESTS: Task(interval, iterations, callback, scheduler, enable, onEnable, onDisable)
  359. *
  360. * PURPOSE: Verify that the full constructor with lifecycle callbacks correctly
  361. * stores callback pointers without invoking them during construction.
  362. *
  363. * LIFECYCLE CALLBACKS:
  364. * - onEnable: Called when task is enabled (should return bool)
  365. * - onDisable: Called when task is disabled
  366. *
  367. * EXPECTATIONS:
  368. * - All basic parameters set correctly
  369. * - Callback pointers stored but not invoked during construction
  370. * - Global callback flags remain false (not called yet)
  371. * - Task ready for enable/disable lifecycle events
  372. *
  373. * IMPORTANCE: Validates that lifecycle callbacks are properly registered
  374. * without premature invocation, ensuring controlled task lifecycle management.
  375. */
  376. TEST_F(SchedulerThoroughTest, TaskConstructorWithOnEnableOnDisable) {
  377. Scheduler ts;
  378. Task task(500, 3, &basic_callback, &ts, false, &test_onEnable, &test_onDisable);
  379. // Verify basic parameters are set correctly
  380. EXPECT_FALSE(task.isEnabled());
  381. EXPECT_EQ(task.getInterval(), 500);
  382. EXPECT_EQ(task.getIterations(), 3);
  383. // Verify lifecycle callbacks are registered but not yet called
  384. EXPECT_FALSE(onEnable_called); // onEnable not called during construction
  385. EXPECT_FALSE(onDisable_called); // onDisable not called during construction
  386. }
  387. /**
  388. * @brief Test Task constructor with automatic enable
  389. *
  390. * TESTS: Task(..., enable=true) + immediate execution behavior
  391. *
  392. * PURPOSE: Verify that tasks created with enable=true are immediately ready
  393. * for execution and will run on the first scheduler pass.
  394. *
  395. * AUTO-ENABLE BEHAVIOR:
  396. * - Task becomes enabled immediately during construction
  397. * - Task is scheduled for immediate execution (no delay)
  398. * - First scheduler pass should execute the task
  399. *
  400. * EXPECTATIONS:
  401. * - Task should be enabled after construction
  402. * - Task should execute successfully on first scheduler run
  403. * - Callback counter should increment correctly
  404. *
  405. * IMPORTANCE: Tests the convenience constructor that creates immediately
  406. * active tasks, commonly used for initialization or startup tasks.
  407. */
  408. TEST_F(SchedulerThoroughTest, TaskConstructorAutoEnabled) {
  409. Scheduler ts;
  410. Task task(100, 1, &basic_callback, &ts, true); // enable=true
  411. // Verify task is enabled immediately after construction
  412. EXPECT_TRUE(task.isEnabled());
  413. // Verify task executes on first scheduler pass
  414. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  415. EXPECT_TRUE(success); // Execution should succeed
  416. EXPECT_EQ(callback_counter, 1); // Should execute exactly once
  417. }
  418. // ================== TASK INFORMATION METHODS TESTS ==================
  419. /**
  420. * @brief Test all Task information getter methods through task lifecycle
  421. *
  422. * TESTS: isEnabled(), getInterval(), getIterations(), getRunCounter()
  423. *
  424. * PURPOSE: Verify that information methods return correct values at different
  425. * stages of task lifecycle and properly track state changes.
  426. *
  427. * METHODS TESTED:
  428. * - isEnabled(): Returns current enabled/disabled state
  429. * - getInterval(): Returns execution interval in milliseconds
  430. * - getIterations(): Returns remaining iterations (decrements after each run)
  431. * - getRunCounter(): Returns total executions since last enable (increments)
  432. *
  433. * LIFECYCLE STAGES TESTED:
  434. * 1. Initial state (after construction)
  435. * 2. After enabling (before execution)
  436. * 3. After first execution (state changes)
  437. *
  438. * EXPECTATIONS:
  439. * - Initial: disabled, interval=2000, iterations=10, runCounter=0
  440. * - After enable: enabled, same interval/iterations, runCounter still 0
  441. * - After execution: enabled, same interval, iterations=9, runCounter=1
  442. *
  443. * IMPORTANCE: These methods are fundamental for task monitoring and debugging.
  444. * Accurate state reporting is critical for application logic and diagnostics.
  445. */
  446. TEST_F(SchedulerThoroughTest, TaskInformationMethods) {
  447. Scheduler ts;
  448. Task task(2000, 10, &basic_callback, &ts, false);
  449. // Test initial state immediately after construction
  450. EXPECT_FALSE(task.isEnabled()); // Should be disabled (enable=false)
  451. EXPECT_EQ(task.getInterval(), 2000); // Should match constructor parameter
  452. EXPECT_EQ(task.getIterations(), 10); // Should match constructor parameter
  453. EXPECT_EQ(task.getRunCounter(), 0); // No executions yet
  454. // Test state after enabling (but before execution)
  455. task.enable();
  456. EXPECT_TRUE(task.isEnabled()); // Should now be enabled
  457. EXPECT_EQ(task.getRunCounter(), 0); // Still 0 before first execution
  458. // Interval and iterations should remain unchanged
  459. // Test state after first execution
  460. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  461. EXPECT_TRUE(success);
  462. EXPECT_EQ(task.getRunCounter(), 1); // Should increment to 1
  463. EXPECT_EQ(task.getIterations(), 9); // Should decrement to 9
  464. // Task should still be enabled and interval unchanged
  465. }
  466. // ================== TASK CONTROL METHODS TESTS ==================
  467. /**
  468. * @brief Test Task enable() and disable() methods with lifecycle callbacks
  469. *
  470. * TESTS: enable(), disable(), isEnabled()
  471. *
  472. * PURPOSE: Verify that enable/disable methods correctly change task state,
  473. * trigger appropriate lifecycle callbacks, and properly control task execution.
  474. *
  475. * CONTROL METHODS TESTED:
  476. * - enable(): Activates task for execution, triggers onEnable callback
  477. * - disable(): Deactivates task, stops execution, triggers onDisable callback
  478. * - isEnabled(): Returns current activation state
  479. *
  480. * LIFECYCLE CALLBACK BEHAVIOR:
  481. * - onEnable(): Called when task transitions from disabled to enabled
  482. * - onDisable(): Called when task transitions from enabled to disabled
  483. * - Callbacks allow custom logic during state transitions
  484. *
  485. * TEST SCENARIOS:
  486. * 1. Enable disabled task (should trigger onEnable)
  487. * 2. Check execution capability (enabled task should execute)
  488. * 3. Disable enabled task (should trigger onDisable, stop execution)
  489. * 4. Verify no execution after disable
  490. *
  491. * EXPECTATIONS:
  492. * - State changes should be immediate and accurate
  493. * - Lifecycle callbacks should be invoked exactly once per transition
  494. * - Execution behavior should match enabled state
  495. *
  496. * IMPORTANCE: Enable/disable is the primary method for dynamic task control.
  497. * This functionality is essential for responsive, event-driven applications.
  498. */
  499. TEST_F(SchedulerThoroughTest, TaskEnableDisable) {
  500. Scheduler ts;
  501. Task task(100, 3, &basic_callback, &ts, false);
  502. // Test enable
  503. EXPECT_FALSE(task.isEnabled());
  504. task.enable();
  505. EXPECT_TRUE(task.isEnabled());
  506. // Test disable
  507. bool prev_state = task.disable();
  508. EXPECT_TRUE(prev_state); // Was enabled
  509. EXPECT_FALSE(task.isEnabled());
  510. // Test disable when already disabled
  511. prev_state = task.disable();
  512. EXPECT_FALSE(prev_state); // Was disabled
  513. EXPECT_FALSE(task.isEnabled());
  514. }
  515. /**
  516. * @brief Test Task enableIfNot() conditional enable method
  517. *
  518. * TESTS: enableIfNot()
  519. *
  520. * PURPOSE: Verify that enableIfNot() provides safe conditional enabling,
  521. * avoiding redundant state changes and providing status feedback.
  522. *
  523. * METHOD BEHAVIOR:
  524. * - enableIfNot(): Enables task only if currently disabled
  525. * - Returns previous enabled state (false = was disabled, true = was enabled)
  526. * - Prevents redundant onEnable callback triggers
  527. * - Idempotent operation (safe to call multiple times)
  528. *
  529. * TEST SCENARIOS:
  530. * 1. Call on disabled task (should enable and return false)
  531. * 2. Call on enabled task (should remain enabled and return true)
  532. *
  533. * EXPECTATIONS:
  534. * - First call: task becomes enabled, returns false (was disabled)
  535. * - Second call: task remains enabled, returns true (was already enabled)
  536. * - No side effects from redundant calls
  537. *
  538. * IMPORTANCE: Conditional enabling prevents unnecessary state transitions
  539. * and provides application feedback about previous state, useful for
  540. * toggle operations and preventing callback storms.
  541. */
  542. TEST_F(SchedulerThoroughTest, TaskEnableIfNot) {
  543. Scheduler ts;
  544. Task task(100, 1, &basic_callback, &ts, false);
  545. // Enable when disabled
  546. bool was_enabled = task.enableIfNot();
  547. EXPECT_FALSE(was_enabled); // Was disabled
  548. EXPECT_TRUE(task.isEnabled());
  549. // Try to enable when already enabled
  550. was_enabled = task.enableIfNot();
  551. EXPECT_TRUE(was_enabled); // Was already enabled
  552. EXPECT_TRUE(task.isEnabled());
  553. }
  554. /**
  555. * @brief Test Task restart() method for resetting task state
  556. *
  557. * TESTS: restart()
  558. *
  559. * PURPOSE: Verify that restart() properly resets task execution state
  560. * while maintaining configuration, allowing tasks to run their full
  561. * iteration cycle again.
  562. *
  563. * METHOD BEHAVIOR:
  564. * - restart(): Resets iteration counter to original value
  565. * - Resets run counter to 0
  566. * - Maintains enabled state
  567. * - Preserves interval and callback configuration
  568. * - Schedules immediate execution (no delay)
  569. *
  570. * TEST SCENARIO:
  571. * 1. Create task with 3 iterations
  572. * 2. Let it execute once (iterations should decrement to 2)
  573. * 3. Call restart() (should reset iterations to 3)
  574. * 4. Verify state is properly reset
  575. *
  576. * EXPECTATIONS:
  577. * - After first execution: getIterations() returns 2
  578. * - After restart(): getIterations() returns original value (3)
  579. * - Task remains enabled throughout
  580. * - Ready for immediate re-execution
  581. *
  582. * IMPORTANCE: Restart functionality enables task recycling and repetitive
  583. * workflows without recreating task objects, essential for state machines
  584. * and cyclic operations.
  585. */
  586. TEST_F(SchedulerThoroughTest, TaskRestart) {
  587. Scheduler ts;
  588. Task task(100, 3, &basic_callback, &ts, true);
  589. // Let it run once
  590. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  591. EXPECT_TRUE(success);
  592. EXPECT_EQ(task.getIterations(), 2); // Should be decremented
  593. // Restart should reset iterations
  594. task.restart();
  595. EXPECT_EQ(task.getIterations(), 3); // Reset to original
  596. EXPECT_TRUE(task.isEnabled());
  597. }
  598. /**
  599. * @brief Test Task restartDelayed() method for delayed task reset
  600. *
  601. * TESTS: restartDelayed(delay)
  602. *
  603. * PURPOSE: Verify that restartDelayed() resets task state like restart()
  604. * but introduces a specified delay before first execution, useful for
  605. * timed restart scenarios and spacing between task cycles.
  606. *
  607. * METHOD BEHAVIOR:
  608. * - restartDelayed(delay): Combines restart() with initial delay
  609. * - Resets iteration and run counters like restart()
  610. * - Schedules first execution after specified delay
  611. * - Subsequent executions follow normal interval timing
  612. * - Task remains enabled but dormant during delay period
  613. *
  614. * TEST SCENARIO:
  615. * 1. Execute task once to modify its state
  616. * 2. Call restartDelayed(200ms)
  617. * 3. Verify no immediate execution (delay period)
  618. * 4. Verify execution occurs after delay expires
  619. *
  620. * EXPECTATIONS:
  621. * - Immediate period: no execution despite scheduler calls
  622. * - After delay: task executes normally with reset state
  623. * - Callback counter increments only after delay period
  624. *
  625. * IMPORTANCE: Delayed restart enables controlled timing gaps between
  626. * task cycles, essential for rate limiting and synchronized multi-task
  627. * restart scenarios.
  628. */
  629. TEST_F(SchedulerThoroughTest, TaskRestartDelayed) {
  630. Scheduler ts;
  631. Task task(50, 2, &basic_callback, &ts, true);
  632. // Let it run once
  633. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  634. EXPECT_TRUE(success);
  635. int count_before_restart = callback_counter;
  636. task.restartDelayed(200); // Restart with 200ms delay
  637. // Should not execute immediately
  638. delay(50);
  639. ts.execute();
  640. EXPECT_EQ(callback_counter, count_before_restart);
  641. // Should execute after delay
  642. delay(200);
  643. success = runSchedulerUntil(ts, [count_before_restart]() {
  644. return callback_counter > count_before_restart;
  645. });
  646. EXPECT_TRUE(success);
  647. }
  648. // ================== TASK TIMING METHODS TESTS ==================
  649. /**
  650. * @brief Test Task delay() method for postponing next execution
  651. *
  652. * TESTS: delay(milliseconds)
  653. *
  654. * PURPOSE: Verify that delay() correctly postpones the next scheduled
  655. * execution by the specified amount, without affecting the task's
  656. * normal interval timing for subsequent executions.
  657. *
  658. * METHOD BEHAVIOR:
  659. * - delay(ms): Postpones next execution by specified milliseconds
  660. * - Affects only the next execution, not the interval permanently
  661. * - Task remains enabled during delay period
  662. * - After delayed execution, normal interval timing resumes
  663. * - Can be called multiple times to accumulate delays
  664. *
  665. * TEST SCENARIO:
  666. * 1. Execute task once to establish baseline
  667. * 2. Call delay(150ms) to postpone next execution
  668. * 3. Verify no execution during delay period (50ms < 150ms)
  669. * 4. Verify execution occurs after delay expires
  670. *
  671. * EXPECTATIONS:
  672. * - During delay: callback counter unchanged despite scheduler calls
  673. * - After delay: task executes and counter increments
  674. * - Subsequent executions follow normal interval
  675. *
  676. * IMPORTANCE: Dynamic delay allows responsive timing adjustments
  677. * based on runtime conditions, essential for adaptive scheduling
  678. * and event-driven timing modifications.
  679. */
  680. TEST_F(SchedulerThoroughTest, TaskDelay) {
  681. Scheduler ts;
  682. Task task(50, 5, &basic_callback, &ts, true);
  683. // Let it run once
  684. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  685. EXPECT_TRUE(success);
  686. int count_before_delay = callback_counter;
  687. task.delay(150); // Delay next execution
  688. // Should not execute immediately
  689. delay(50);
  690. ts.execute();
  691. EXPECT_EQ(callback_counter, count_before_delay);
  692. // Should execute after delay
  693. delay(150);
  694. success = runSchedulerUntil(ts, [count_before_delay]() {
  695. return callback_counter > count_before_delay;
  696. });
  697. EXPECT_TRUE(success);
  698. }
  699. /**
  700. * @brief Test Task forceNextIteration() method for immediate execution
  701. *
  702. * TESTS: forceNextIteration()
  703. *
  704. * PURPOSE: Verify that forceNextIteration() bypasses normal interval timing
  705. * and schedules the task for immediate execution on the next scheduler pass,
  706. * useful for triggering urgent or event-driven task execution.
  707. *
  708. * METHOD BEHAVIOR:
  709. * - forceNextIteration(): Marks task for immediate execution
  710. * - Bypasses remaining interval wait time
  711. * - Does not affect subsequent interval timing
  712. * - Works only if task is enabled
  713. * - Executes on very next scheduler.execute() call
  714. *
  715. * TEST SCENARIO:
  716. * 1. Create task with long interval (1000ms) to prevent natural execution
  717. * 2. Let it execute once (starts long interval timer)
  718. * 3. Call forceNextIteration() during interval wait
  719. * 4. Verify immediate execution on next scheduler pass
  720. *
  721. * EXPECTATIONS:
  722. * - Without force: task would wait full 1000ms interval
  723. * - With force: task executes immediately despite interval
  724. * - Callback counter increments immediately after force
  725. *
  726. * IMPORTANCE: Force execution enables responsive event handling and
  727. * priority task execution, essential for interrupt-driven scenarios
  728. * and urgent task processing.
  729. */
  730. TEST_F(SchedulerThoroughTest, TaskForceNextIteration) {
  731. Scheduler ts;
  732. Task task(1000, 3, &basic_callback, &ts, true); // Long interval
  733. // Execute immediately due to enable
  734. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  735. EXPECT_TRUE(success);
  736. int count_before_force = callback_counter;
  737. task.forceNextIteration();
  738. // Should execute immediately on next scheduler pass
  739. success = runSchedulerUntil(ts, [count_before_force]() {
  740. return callback_counter > count_before_force;
  741. });
  742. EXPECT_TRUE(success);
  743. }
  744. /**
  745. * @brief Test Task enableDelayed() method for delayed activation
  746. *
  747. * TESTS: enableDelayed(delay)
  748. *
  749. * PURPOSE: Verify that enableDelayed() enables a task but delays its
  750. * first execution by the specified amount, useful for coordinated
  751. * task startup and avoiding immediate execution.
  752. *
  753. * METHOD BEHAVIOR:
  754. * - enableDelayed(ms): Enables task but delays first execution
  755. * - Task becomes enabled immediately (isEnabled() returns true)
  756. * - First execution waits for specified delay period
  757. * - After first execution, normal interval timing applies
  758. * - Different from enable() which executes immediately
  759. *
  760. * TEST SCENARIO:
  761. * 1. Create disabled task
  762. * 2. Call enableDelayed(200ms)
  763. * 3. Verify task is enabled but doesn't execute immediately
  764. * 4. Verify execution occurs after delay period
  765. *
  766. * EXPECTATIONS:
  767. * - Task enabled: isEnabled() returns true immediately
  768. * - During delay: no execution despite scheduler calls
  769. * - After delay: task executes normally
  770. *
  771. * IMPORTANCE: Delayed enabling allows coordinated task startup
  772. * sequences and prevents initial execution conflicts, essential
  773. * for synchronized multi-task systems.
  774. */
  775. TEST_F(SchedulerThoroughTest, TaskEnableDelayed) {
  776. Scheduler ts;
  777. Task task(100, 1, &basic_callback, &ts, false);
  778. task.enableDelayed(200);
  779. EXPECT_TRUE(task.isEnabled());
  780. // Should not execute immediately
  781. delay(50);
  782. ts.execute();
  783. EXPECT_EQ(callback_counter, 0);
  784. // Should execute after delay
  785. delay(200);
  786. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  787. EXPECT_TRUE(success);
  788. }
  789. // ================== TASK CONFIGURATION METHODS TESTS ==================
  790. /**
  791. * @brief Test Task set() method for complete task reconfiguration
  792. *
  793. * TESTS: set(interval, iterations, callback, onEnable, onDisable)
  794. *
  795. * PURPOSE: Verify that set() completely reconfigures a task with new
  796. * parameters, allowing task objects to be reused with different
  797. * configurations without recreation.
  798. *
  799. * METHOD BEHAVIOR:
  800. * - set(): Replaces all task configuration parameters
  801. * - Updates interval, iterations, callback, and lifecycle callbacks
  802. * - Does not change enabled state (task remains in current state)
  803. * - Resets internal counters and timing
  804. * - Allows complete task repurposing
  805. *
  806. * TEST SCENARIO:
  807. * 1. Create default task (empty configuration)
  808. * 2. Use set() to configure with specific parameters
  809. * 3. Verify all parameters were set correctly
  810. * 4. Verify task remains disabled (set() doesn't enable)
  811. *
  812. * EXPECTATIONS:
  813. * - All set parameters should be retrievable via getter methods
  814. * - Task should remain disabled (set() doesn't enable)
  815. * - Task ready for enable() to start with new configuration
  816. *
  817. * IMPORTANCE: Set method enables task object reuse and dynamic
  818. * reconfiguration, essential for flexible task management and
  819. * memory-efficient applications.
  820. */
  821. TEST_F(SchedulerThoroughTest, TaskSetMethod) {
  822. Scheduler ts;
  823. Task task;
  824. task.set(300, 7, &basic_callback, &test_onEnable, &test_onDisable);
  825. EXPECT_EQ(task.getInterval(), 300);
  826. EXPECT_EQ(task.getIterations(), 7);
  827. EXPECT_FALSE(task.isEnabled());
  828. }
  829. /**
  830. * @brief Test Task setInterval() method for dynamic timing changes
  831. *
  832. * TESTS: setInterval(newInterval), getInterval()
  833. *
  834. * PURPOSE: Verify that setInterval() dynamically changes task execution
  835. * timing and that changes take effect for subsequent executions,
  836. * enabling adaptive and responsive timing control.
  837. *
  838. * METHOD BEHAVIOR:
  839. * - setInterval(ms): Changes execution interval for future executions
  840. * - Does not affect current timing if task is mid-interval
  841. * - New interval applies to next scheduled execution
  842. * - Can be called multiple times to adjust timing dynamically
  843. * - Allows real-time timing optimization
  844. *
  845. * TEST SCENARIO:
  846. * 1. Create task with initial 100ms interval
  847. * 2. Change interval to 500ms and verify getter
  848. * 3. Let task execute once with new timing
  849. * 4. Change interval to 200ms during execution
  850. * 5. Verify next execution uses new 200ms interval
  851. *
  852. * EXPECTATIONS:
  853. * - getInterval() returns updated value immediately
  854. * - Execution timing reflects new interval settings
  855. * - Dynamic changes affect subsequent executions
  856. *
  857. * IMPORTANCE: Dynamic interval adjustment enables adaptive systems
  858. * that respond to load, priority, or environmental changes in real-time,
  859. * essential for responsive and efficient applications.
  860. */
  861. TEST_F(SchedulerThoroughTest, TaskSetInterval) {
  862. Scheduler ts;
  863. Task task(100, 2, &basic_callback, &ts, true);
  864. task.setInterval(500);
  865. EXPECT_EQ(task.getInterval(), 500);
  866. // Interval change should affect timing
  867. unsigned long start_time = millis();
  868. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  869. EXPECT_TRUE(success);
  870. task.setInterval(200);
  871. int count_before = callback_counter;
  872. success = runSchedulerUntil(ts, [count_before]() {
  873. return callback_counter > count_before;
  874. }, 300);
  875. EXPECT_TRUE(success);
  876. }
  877. /**
  878. * @brief Test Task setIterations() method for dynamic repetition control
  879. *
  880. * TESTS: setIterations(newIterations), getIterations()
  881. *
  882. * PURPOSE: Verify that setIterations() dynamically changes the number of
  883. * remaining executions and that tasks properly auto-disable when reaching
  884. * zero iterations, enabling flexible execution count management.
  885. *
  886. * METHOD BEHAVIOR:
  887. * - setIterations(count): Sets number of remaining executions
  888. * - Count decrements with each execution
  889. * - Task auto-disables when iterations reach zero
  890. * - Can extend or reduce remaining executions dynamically
  891. * - TASK_FOREVER can be set for infinite execution
  892. *
  893. * TEST SCENARIO:
  894. * 1. Create task with initial 2 iterations
  895. * 2. Change to 5 iterations before execution
  896. * 3. Let task run complete cycle (5 executions)
  897. * 4. Verify task auto-disables after completing all iterations
  898. *
  899. * EXPECTATIONS:
  900. * - getIterations() returns updated count
  901. * - Task executes exactly 5 times (new iteration count)
  902. * - Task automatically disables after final iteration
  903. * - Callback counter reaches exactly 5
  904. *
  905. * IMPORTANCE: Dynamic iteration control allows adaptive execution
  906. * cycles based on runtime conditions, essential for conditional
  907. * processing and resource-conscious applications.
  908. */
  909. TEST_F(SchedulerThoroughTest, TaskSetIterations) {
  910. Scheduler ts;
  911. Task task(100, 2, &basic_callback, &ts, true);
  912. task.setIterations(5);
  913. EXPECT_EQ(task.getIterations(), 5);
  914. // Should run 5 times
  915. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 5; });
  916. EXPECT_TRUE(success);
  917. EXPECT_EQ(callback_counter, 5);
  918. EXPECT_FALSE(task.isEnabled()); // Should auto-disable after iterations
  919. }
  920. /**
  921. * @brief Test Task callback switching methods for dynamic behavior
  922. *
  923. * TESTS: setCallback(), setOnEnable(), setOnDisable()
  924. *
  925. * PURPOSE: Verify that callback methods can be dynamically changed during
  926. * task lifetime, enabling flexible behavior modification and state-dependent
  927. * functionality without task recreation.
  928. *
  929. * CALLBACK TYPES TESTED:
  930. * - setCallback(): Changes main execution callback function
  931. * - setOnEnable(): Changes lifecycle callback for enable events
  932. * - setOnDisable(): Changes lifecycle callback for disable events
  933. *
  934. * METHOD BEHAVIOR:
  935. * - Callbacks can be changed while task is running
  936. * - New callbacks take effect immediately
  937. * - Previous callbacks are completely replaced
  938. * - Lifecycle callbacks trigger during state transitions
  939. *
  940. * TEST SCENARIO:
  941. * 1. Create task with callback_1 function
  942. * 2. Switch to callback_2 and verify execution uses new callback
  943. * 3. Set lifecycle callbacks and verify they trigger during state changes
  944. *
  945. * EXPECTATIONS:
  946. * - Execution produces output from new callback (callback_2)
  947. * - Enable transition triggers onEnable callback
  948. * - Disable transition triggers onDisable callback
  949. * - All callback switches take effect immediately
  950. *
  951. * IMPORTANCE: Dynamic callback switching enables state machines,
  952. * adaptive behavior, and multi-phase task processing without
  953. * complex conditional logic in callbacks.
  954. */
  955. TEST_F(SchedulerThoroughTest, TaskSetCallbacks) {
  956. Scheduler ts;
  957. Task task(100, 2, &callback_1, &ts, false);
  958. // Test setCallback
  959. task.setCallback(&callback_2);
  960. task.enable();
  961. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  962. EXPECT_TRUE(success);
  963. EXPECT_EQ(getTestOutput(0), "callback_2");
  964. // Test setOnEnable and setOnDisable
  965. task.setOnEnable(&test_onEnable);
  966. task.setOnDisable(&test_onDisable);
  967. task.disable();
  968. task.enable();
  969. EXPECT_TRUE(onEnable_called);
  970. task.disable();
  971. EXPECT_TRUE(onDisable_called);
  972. }
  973. // ================== TASK ITERATION STATE TESTS ==================
  974. /**
  975. * @brief Test Task iteration state query methods for execution context
  976. *
  977. * TESTS: isFirstIteration(), isLastIteration()
  978. *
  979. * PURPOSE: Verify that iteration state query methods correctly identify
  980. * the execution context within a task's iteration cycle, enabling
  981. * conditional logic based on iteration position.
  982. *
  983. * STATE METHODS TESTED:
  984. * - isFirstIteration(): Returns true only during the first execution
  985. * - isLastIteration(): Returns true only during the final execution
  986. * - Both provide context for conditional callback behavior
  987. *
  988. * TEST SCENARIO:
  989. * 1. Create task with 3 iterations
  990. * 2. Execute first iteration (implicit due to enable=true)
  991. * 3. Use modified callback to check states during iterations 2 and 3
  992. * 4. Verify correct state reporting for middle and final iterations
  993. *
  994. * EXPECTATIONS:
  995. * - First iteration: isFirstIteration()=true, isLastIteration()=false
  996. * - Middle iteration: isFirstIteration()=false, isLastIteration()=false
  997. * - Final iteration: isFirstIteration()=false, isLastIteration()=true
  998. *
  999. * IMPORTANCE: Iteration state queries enable initialization and cleanup
  1000. * logic within callbacks, essential for resource management and
  1001. * conditional processing based on execution phase.
  1002. */
  1003. TEST_F(SchedulerThoroughTest, TaskIterationState) {
  1004. Scheduler ts;
  1005. Task task(100, 3, &basic_callback, &ts, true);
  1006. // First iteration
  1007. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  1008. EXPECT_TRUE(success);
  1009. // Note: Testing iteration state requires access during callback execution
  1010. // This test verifies the task completes its iterations properly
  1011. success = runSchedulerUntil(ts, []() { return callback_counter >= 3; });
  1012. EXPECT_TRUE(success);
  1013. EXPECT_EQ(callback_counter, 3);
  1014. EXPECT_FALSE(task.isEnabled()); // Should auto-disable after 3 iterations
  1015. }
  1016. // ================== TASK CALLBACK SWITCHING TESTS ==================
  1017. /**
  1018. * @brief Test Task yield() method for dynamic callback switching
  1019. *
  1020. * TESTS: yield(newCallback)
  1021. *
  1022. * PURPOSE: Verify that yield() permanently switches the task's callback
  1023. * function to a new function, enabling state machine behavior and
  1024. * multi-phase task processing within a single task object.
  1025. *
  1026. * METHOD BEHAVIOR:
  1027. * - yield(callback): Permanently changes task's callback function
  1028. * - Switch takes effect immediately
  1029. * - All subsequent executions use the new callback
  1030. * - Original callback is completely replaced
  1031. * - Enables state machine and multi-step processing patterns
  1032. *
  1033. * TEST SCENARIO:
  1034. * 1. Create task with multi_step_callback_1
  1035. * 2. In callback, switch to multi_step_callback_2 via yield
  1036. * 3. Verify first execution produces "step_1" output
  1037. * 4. Verify second execution produces "step_2" output (new callback)
  1038. *
  1039. * EXPECTATIONS:
  1040. * - First execution: "step_1" output from original callback
  1041. * - Yield call: switches to multi_step_callback_2
  1042. * - Second execution: "step_2" output from new callback
  1043. * - All future executions use new callback
  1044. *
  1045. * IMPORTANCE: Yield enables complex state machines and multi-phase
  1046. * processing within single tasks, essential for sequential operations
  1047. * and adaptive task behavior.
  1048. */
  1049. TEST_F(SchedulerThoroughTest, TaskYield) {
  1050. Scheduler ts;
  1051. // Create a global task pointer for the callback to access
  1052. static Task* yield_task = nullptr;
  1053. // Callback that will yield to another callback
  1054. auto yield_callback = []() {
  1055. test_output.push_back("step_1");
  1056. if (yield_task) {
  1057. yield_task->yield(&multi_step_callback_2);
  1058. }
  1059. };
  1060. Task task(200, 3, yield_callback, &ts, true);
  1061. yield_task = &task;
  1062. // First execution should produce step_1 and yield
  1063. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  1064. EXPECT_TRUE(success);
  1065. EXPECT_EQ(getTestOutput(0), "step_1");
  1066. // Second execution should produce step_2 from yielded callback
  1067. success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1068. EXPECT_TRUE(success);
  1069. EXPECT_EQ(getTestOutput(1), "step_2");
  1070. yield_task = nullptr; // Clean up
  1071. }
  1072. /**
  1073. * @brief Test Task yieldOnce() method for single callback switch
  1074. *
  1075. * TESTS: yieldOnce(newCallback)
  1076. *
  1077. * PURPOSE: Verify that yieldOnce() switches to a new callback for exactly
  1078. * one execution, then automatically disables the task, enabling one-time
  1079. * completion or finalization logic.
  1080. *
  1081. * METHOD BEHAVIOR:
  1082. * - yieldOnce(callback): Switches to new callback for one execution only
  1083. * - New callback executes exactly once on next scheduler pass
  1084. * - Task automatically disables after the single execution
  1085. * - Original callback is not restored (task becomes inactive)
  1086. * - Useful for completion/cleanup logic and one-shot operations
  1087. *
  1088. * TEST SCENARIO:
  1089. * 1. Create task with 5 iterations and initial callback
  1090. * 2. In first execution, call yieldOnce() to switch to completion callback
  1091. * 3. Verify first execution produces "step_1" output
  1092. * 4. Verify second execution produces "step_2" output
  1093. * 5. Verify task automatically disables after yieldOnce execution
  1094. *
  1095. * EXPECTATIONS:
  1096. * - First execution: "step_1" from original callback with yieldOnce call
  1097. * - Second execution: "step_2" from yielded callback
  1098. * - After second execution: task becomes disabled automatically
  1099. * - No further executions occur
  1100. *
  1101. * IMPORTANCE: YieldOnce enables one-time finalization, cleanup, and
  1102. * completion logic, essential for graceful task termination and
  1103. * resource cleanup patterns.
  1104. */
  1105. TEST_F(SchedulerThoroughTest, TaskYieldOnce) {
  1106. Scheduler ts;
  1107. static Task* yield_once_task = nullptr;
  1108. auto yield_once_callback = []() {
  1109. test_output.push_back("step_1");
  1110. if (yield_once_task) {
  1111. yield_once_task->yieldOnce(&multi_step_callback_2);
  1112. }
  1113. };
  1114. Task task(100, 5, yield_once_callback, &ts, true);
  1115. yield_once_task = &task;
  1116. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  1117. EXPECT_TRUE(success);
  1118. // Should execute step 2 once then disable
  1119. success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1120. EXPECT_TRUE(success);
  1121. EXPECT_EQ(getTestOutput(1), "step_2");
  1122. EXPECT_FALSE(task.isEnabled()); // Should be disabled after yieldOnce
  1123. yield_once_task = nullptr; // Clean up
  1124. }
  1125. // ================== TASK ONENABLE/ONDISABLE TESTS ==================
  1126. /**
  1127. * @brief Test Task OnEnable/OnDisable lifecycle callback behavior
  1128. *
  1129. * TESTS: onEnable callback, onDisable callback, lifecycle triggers
  1130. *
  1131. * PURPOSE: Verify that lifecycle callbacks are properly invoked during
  1132. * task state transitions, enabling initialization and cleanup logic
  1133. * to execute at appropriate times.
  1134. *
  1135. * LIFECYCLE CALLBACK BEHAVIOR:
  1136. * - onEnable(): Called when task transitions from disabled to enabled
  1137. * - onDisable(): Called when task transitions from enabled to disabled
  1138. * - Callbacks execute synchronously during state change
  1139. * - Enable callback can prevent enabling by returning false
  1140. * - Disable callback is informational (no return value)
  1141. *
  1142. * TEST SCENARIO:
  1143. * 1. Create task with lifecycle callbacks but start disabled
  1144. * 2. Enable task and verify onEnable callback is invoked
  1145. * 3. Disable task and verify onDisable callback is invoked
  1146. * 4. Verify callbacks are triggered exactly once per transition
  1147. *
  1148. * EXPECTATIONS:
  1149. * - OnEnable called during enable() transition
  1150. * - OnDisable called during disable() transition
  1151. * - Global callback flags properly set
  1152. * - Task state changes successful
  1153. *
  1154. * IMPORTANCE: Lifecycle callbacks enable proper resource management,
  1155. * initialization, and cleanup, essential for robust task lifecycle
  1156. * management and preventing resource leaks.
  1157. */
  1158. TEST_F(SchedulerThoroughTest, TaskOnEnableOnDisable) {
  1159. Scheduler ts;
  1160. Task task(100, 2, &basic_callback, &ts, false, &test_onEnable, &test_onDisable);
  1161. // Test OnEnable
  1162. task.enable();
  1163. EXPECT_TRUE(onEnable_called);
  1164. EXPECT_TRUE(task.isEnabled());
  1165. // Reset and test OnDisable
  1166. onEnable_called = false;
  1167. task.disable();
  1168. EXPECT_TRUE(onDisable_called);
  1169. EXPECT_FALSE(task.isEnabled());
  1170. }
  1171. /**
  1172. * @brief Test Task OnEnable callback returning false to prevent enabling
  1173. *
  1174. * TESTS: onEnable callback return value, conditional enabling
  1175. *
  1176. * PURPOSE: Verify that when an onEnable callback returns false, the task
  1177. * remains disabled, providing a mechanism for conditional enabling based
  1178. * on runtime conditions or resource availability.
  1179. *
  1180. * CONDITIONAL ENABLING BEHAVIOR:
  1181. * - onEnable(): Must return boolean (true = allow enable, false = prevent)
  1182. * - When returns false: task remains disabled despite enable() call
  1183. * - When returns true: task becomes enabled normally
  1184. * - Callback is always invoked (for logging/monitoring purposes)
  1185. * - Enables conditional logic for resource-dependent tasks
  1186. *
  1187. * TEST SCENARIO:
  1188. * 1. Create task with onEnable callback that returns false
  1189. * 2. Call enable() on the task
  1190. * 3. Verify onEnable callback was invoked
  1191. * 4. Verify task remains disabled (enable() call rejected)
  1192. *
  1193. * EXPECTATIONS:
  1194. * - OnEnable callback is called (onEnable_called flag set)
  1195. * - Task remains disabled (isEnabled() returns false)
  1196. * - Enable attempt is gracefully rejected
  1197. *
  1198. * IMPORTANCE: Conditional enabling prevents tasks from starting when
  1199. * prerequisites aren't met, essential for resource-dependent operations
  1200. * and safety-critical system states.
  1201. */
  1202. TEST_F(SchedulerThoroughTest, TaskOnEnableReturnsFalse) {
  1203. Scheduler ts;
  1204. Task task(100, 1, &basic_callback, &ts, false, &test_onEnable_false, &test_onDisable);
  1205. // OnEnable returns false, task should remain disabled
  1206. task.enable();
  1207. EXPECT_TRUE(onEnable_called);
  1208. EXPECT_FALSE(task.isEnabled()); // Should remain disabled
  1209. }
  1210. // ================== SCHEDULER CONSTRUCTOR AND INIT TESTS ==================
  1211. /**
  1212. * @brief Test Scheduler constructor and basic operation
  1213. *
  1214. * TESTS: Scheduler(), execute() with no tasks
  1215. *
  1216. * PURPOSE: Verify that a Scheduler object can be constructed successfully
  1217. * and can safely execute even when no tasks are registered, demonstrating
  1218. * robustness in edge cases.
  1219. *
  1220. * CONSTRUCTOR BEHAVIOR:
  1221. * - Scheduler(): Creates empty scheduler with no tasks
  1222. * - execute(): Safely handles empty task list
  1223. * - No exceptions or crashes in degenerate cases
  1224. * - Ready to accept tasks via Task constructor registration
  1225. *
  1226. * TEST SCENARIO:
  1227. * 1. Create empty scheduler
  1228. * 2. Call execute() on empty scheduler
  1229. * 3. Verify no output generated (no tasks to execute)
  1230. * 4. Verify no crashes or exceptions
  1231. *
  1232. * EXPECTATIONS:
  1233. * - Constructor succeeds without exceptions
  1234. * - Execute runs safely with no tasks
  1235. * - No test output produced (counter remains 0)
  1236. * - Scheduler ready for task registration
  1237. *
  1238. * IMPORTANCE: Empty scheduler robustness ensures safe operation during
  1239. * initialization phases and prevents crashes in edge cases, essential
  1240. * for reliable system startup sequences.
  1241. */
  1242. TEST_F(SchedulerThoroughTest, SchedulerConstructor) {
  1243. Scheduler ts;
  1244. // Should be able to execute without tasks
  1245. ts.execute();
  1246. EXPECT_EQ(getTestOutputCount(), 0);
  1247. }
  1248. /**
  1249. * @brief Test Scheduler init() method for reinitialization
  1250. *
  1251. * TESTS: init()
  1252. *
  1253. * PURPOSE: Verify that init() properly reinitializes the scheduler
  1254. * state while preserving task registrations, enabling scheduler
  1255. * reset without losing configured tasks.
  1256. *
  1257. * INIT METHOD BEHAVIOR:
  1258. * - init(): Reinitializes internal scheduler state
  1259. * - Preserves registered tasks and their configurations
  1260. * - Resets timing and execution state
  1261. * - Tasks remain functional after reinitialization
  1262. * - Useful for system restart scenarios
  1263. *
  1264. * TEST SCENARIO:
  1265. * 1. Create scheduler with one registered task
  1266. * 2. Call init() to reinitialize scheduler
  1267. * 3. Verify task still executes properly after init
  1268. * 4. Confirm scheduler functionality is preserved
  1269. *
  1270. * EXPECTATIONS:
  1271. * - Init() completes without errors
  1272. * - Task remains registered and functional
  1273. * - Task executes successfully after reinitialization
  1274. * - Callback counter increments as expected
  1275. *
  1276. * IMPORTANCE: Scheduler reinitialization enables system restart
  1277. * scenarios and state recovery while preserving task configurations,
  1278. * essential for robust and recoverable applications.
  1279. */
  1280. TEST_F(SchedulerThoroughTest, SchedulerInit) {
  1281. Scheduler ts;
  1282. Task task(100, 1, &basic_callback, &ts, true);
  1283. ts.init(); // Should reinitialize
  1284. // Task should still work after init
  1285. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 1; });
  1286. EXPECT_TRUE(success);
  1287. }
  1288. // ================== SCHEDULER TASK MANAGEMENT TESTS ==================
  1289. /**
  1290. * @brief Test Scheduler addTask() and deleteTask() methods for manual task management
  1291. *
  1292. * TESTS: addTask(task), deleteTask(task)
  1293. *
  1294. * PURPOSE: Verify that tasks can be manually added to and removed from
  1295. * schedulers, enabling dynamic task management and scheduler composition
  1296. * patterns beyond automatic constructor registration.
  1297. *
  1298. * TASK MANAGEMENT BEHAVIOR:
  1299. * - addTask(): Manually registers task with scheduler
  1300. * - deleteTask(): Removes task from scheduler's management
  1301. * - Tasks can be added without using scheduler parameter in constructor
  1302. * - Deleted tasks are no longer executed by scheduler
  1303. * - Remaining tasks continue normal operation
  1304. *
  1305. * TEST SCENARIO:
  1306. * 1. Create tasks without scheduler assignment (nullptr)
  1307. * 2. Manually add both tasks to scheduler
  1308. * 3. Enable and execute both tasks
  1309. * 4. Delete one task from scheduler
  1310. * 5. Verify only remaining task executes
  1311. *
  1312. * EXPECTATIONS:
  1313. * - Both tasks execute when added to scheduler
  1314. * - After deletion, only non-deleted task executes
  1315. * - Deleted task becomes unmanaged (won't execute)
  1316. * - Scheduler continues operating with remaining tasks
  1317. *
  1318. * IMPORTANCE: Manual task management enables dynamic scheduler composition,
  1319. * conditional task registration, and runtime task lifecycle management,
  1320. * essential for flexible and adaptive applications.
  1321. */
  1322. TEST_F(SchedulerThoroughTest, SchedulerAddDeleteTask) {
  1323. Scheduler ts;
  1324. Task task1(100, 1, &callback_1, nullptr, false);
  1325. Task task2(150, 1, &callback_2, nullptr, false);
  1326. // Add tasks manually
  1327. ts.addTask(task1);
  1328. ts.addTask(task2);
  1329. task1.enable();
  1330. task2.enable();
  1331. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1332. EXPECT_TRUE(success);
  1333. // Delete task1
  1334. ts.deleteTask(task1);
  1335. // Only task2 should be manageable now
  1336. clearTestOutput();
  1337. task2.restart();
  1338. success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  1339. EXPECT_TRUE(success);
  1340. EXPECT_EQ(getTestOutput(0), "callback_2");
  1341. }
  1342. // ================== SCHEDULER EXECUTION CONTROL TESTS ==================
  1343. /**
  1344. * @brief Test Scheduler execute() method return value for idle detection
  1345. *
  1346. * TESTS: execute() return value, idle state detection
  1347. *
  1348. * PURPOSE: Verify that execute() correctly returns idle status, enabling
  1349. * applications to detect when no tasks are ready for execution and
  1350. * implement power management or alternative processing.
  1351. *
  1352. * EXECUTE RETURN VALUE BEHAVIOR:
  1353. * - execute(): Returns false when tasks executed, true when idle
  1354. * - Idle = no tasks ready for execution at current time
  1355. * - Non-idle = one or more tasks executed during call
  1356. * - Enables power management and conditional processing
  1357. * - Critical for battery-powered and real-time applications
  1358. *
  1359. * TEST SCENARIO:
  1360. * 1. Create two tasks with different intervals (100ms, 150ms)
  1361. * 2. First execute() should be non-idle (immediate execution)
  1362. * 3. Run until both tasks complete
  1363. * 4. Later execute() should be idle (no tasks ready)
  1364. *
  1365. * EXPECTATIONS:
  1366. * - First execute(): returns false (tasks executed)
  1367. * - Both tasks produce expected output
  1368. * - After completion: execute() returns true (idle)
  1369. *
  1370. * IMPORTANCE: Idle detection enables efficient resource utilization,
  1371. * power management, and responsive application behavior, essential
  1372. * for embedded and battery-powered systems.
  1373. */
  1374. TEST_F(SchedulerThoroughTest, SchedulerExecute) {
  1375. Scheduler ts;
  1376. Task task1(100, 1, &callback_1, &ts, true);
  1377. Task task2(150, 1, &callback_2, &ts, true);
  1378. bool idle = ts.execute();
  1379. // First execute should run tasks immediately, so not idle
  1380. EXPECT_FALSE(idle);
  1381. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1382. EXPECT_TRUE(success);
  1383. // After tasks complete, should be idle
  1384. delay(200);
  1385. idle = ts.execute();
  1386. EXPECT_TRUE(idle);
  1387. }
  1388. /**
  1389. * @brief Test Scheduler enableAll() and disableAll() methods for bulk control
  1390. *
  1391. * TESTS: enableAll(), disableAll()
  1392. *
  1393. * PURPOSE: Verify that enableAll() and disableAll() methods correctly
  1394. * change the state of all tasks managed by the scheduler, enabling
  1395. * coordinated system-wide task control.
  1396. *
  1397. * BULK CONTROL BEHAVIOR:
  1398. * - enableAll(): Enables every task registered with scheduler
  1399. * - disableAll(): Disables every task registered with scheduler
  1400. * - Affects all tasks regardless of current state
  1401. * - Triggers onEnable/onDisable callbacks for each task
  1402. * - Enables system-wide start/stop functionality
  1403. *
  1404. * TEST SCENARIO:
  1405. * 1. Create three tasks in disabled state
  1406. * 2. Call enableAll() and verify all tasks become enabled
  1407. * 3. Call disableAll() and verify all tasks become disabled
  1408. * 4. Confirm state changes affect all registered tasks
  1409. *
  1410. * EXPECTATIONS:
  1411. * - EnableAll(): all tasks report isEnabled() = true
  1412. * - DisableAll(): all tasks report isEnabled() = false
  1413. * - State changes are consistent across all tasks
  1414. * - Bulk operations affect entire task set
  1415. *
  1416. * IMPORTANCE: Bulk enable/disable enables system-wide control patterns,
  1417. * emergency stops, coordinated startup/shutdown, and mode switching,
  1418. * essential for complex multi-task systems.
  1419. */
  1420. TEST_F(SchedulerThoroughTest, SchedulerEnableDisableAll) {
  1421. Scheduler ts;
  1422. Task task1(100, 1, &callback_1, &ts, false);
  1423. Task task2(150, 1, &callback_2, &ts, false);
  1424. Task task3(200, 1, &callback_3, &ts, false);
  1425. // Enable all
  1426. ts.enableAll();
  1427. EXPECT_TRUE(task1.isEnabled());
  1428. EXPECT_TRUE(task2.isEnabled());
  1429. EXPECT_TRUE(task3.isEnabled());
  1430. // Disable all
  1431. ts.disableAll();
  1432. EXPECT_FALSE(task1.isEnabled());
  1433. EXPECT_FALSE(task2.isEnabled());
  1434. EXPECT_FALSE(task3.isEnabled());
  1435. }
  1436. // ================== SCHEDULER TIME QUERY TESTS ==================
  1437. /**
  1438. * @brief Test Scheduler timeUntilNextIteration() method for timing queries
  1439. *
  1440. * TESTS: timeUntilNextIteration(task)
  1441. *
  1442. * PURPOSE: Verify that timeUntilNextIteration() accurately reports the
  1443. * remaining time before a task's next scheduled execution, enabling
  1444. * predictive scheduling and timing-based application logic.
  1445. *
  1446. * TIME QUERY BEHAVIOR:
  1447. * - timeUntilNextIteration(): Returns milliseconds until next execution
  1448. * - Returns -1 for disabled tasks (not scheduled)
  1449. * - Returns positive value for enabled tasks with pending execution
  1450. * - Value decreases as time passes toward execution
  1451. * - Enables predictive timing and coordination
  1452. *
  1453. * TEST SCENARIO:
  1454. * 1. Check disabled task (should return -1)
  1455. * 2. Enable task with 500ms delay
  1456. * 3. Verify time remaining is close to 500ms
  1457. * 4. Wait 200ms and verify time decreased appropriately
  1458. *
  1459. * EXPECTATIONS:
  1460. * - Disabled task: returns -1
  1461. * - Initially delayed task: returns ~500ms
  1462. * - After 200ms delay: returns ~300ms
  1463. * - Timing accuracy within reasonable bounds
  1464. *
  1465. * IMPORTANCE: Time queries enable predictive scheduling, coordination
  1466. * between tasks, and timing-based application logic, essential for
  1467. * real-time and synchronized operations.
  1468. */
  1469. TEST_F(SchedulerThoroughTest, SchedulerTimeUntilNextIteration) {
  1470. Scheduler ts;
  1471. Task task(1000, 1, &basic_callback, &ts, false);
  1472. // Disabled task should return -1
  1473. long time_until = ts.timeUntilNextIteration(task);
  1474. EXPECT_EQ(time_until, -1);
  1475. // Enabled task with delay
  1476. task.enableDelayed(500);
  1477. time_until = ts.timeUntilNextIteration(task);
  1478. EXPECT_GT(time_until, 400); // Should be close to 500ms
  1479. EXPECT_LE(time_until, 500);
  1480. // After some time passes
  1481. delay(200);
  1482. time_until = ts.timeUntilNextIteration(task);
  1483. EXPECT_GT(time_until, 200); // Should be around 300ms
  1484. EXPECT_LE(time_until, 300);
  1485. }
  1486. // ================== SCHEDULER TASK ACCESS TESTS ==================
  1487. /**
  1488. * @brief Test Scheduler getCurrentTask() method for callback context identification
  1489. *
  1490. * TESTS: getCurrentTask()
  1491. *
  1492. * PURPOSE: Verify that getCurrentTask() correctly returns a pointer to the
  1493. * currently executing task when called from within a task callback,
  1494. * enabling self-referential task operations and context awareness.
  1495. *
  1496. * CONTEXT ACCESS BEHAVIOR:
  1497. * - getCurrentTask(): Returns pointer to currently executing task
  1498. * - Only valid when called from within task callback
  1499. * - Returns nullptr when called outside task execution context
  1500. * - Enables task self-modification and context-aware operations
  1501. * - Critical for advanced task patterns and introspection
  1502. *
  1503. * TEST SCENARIO:
  1504. * 1. Create task with callback that calls getCurrentTask()
  1505. * 2. Execute task and capture the returned task pointer
  1506. * 3. Verify returned pointer matches the actual task object
  1507. * 4. Confirm context identification works correctly
  1508. *
  1509. * EXPECTATIONS:
  1510. * - getCurrentTask() returns valid pointer during execution
  1511. * - Returned pointer equals address of actual task object
  1512. * - Context identification is accurate and reliable
  1513. *
  1514. * IMPORTANCE: Current task access enables advanced patterns like
  1515. * task self-modification, recursive operations, and context-aware
  1516. * callback behavior, essential for sophisticated task orchestration.
  1517. */
  1518. TEST_F(SchedulerThoroughTest, SchedulerCurrentTask) {
  1519. Scheduler ts;
  1520. Task* current_task_ptr = nullptr;
  1521. Task* task_address = nullptr;
  1522. // Create callback that captures getCurrentTask()
  1523. auto current_task_callback = [&ts, &current_task_ptr]() {
  1524. current_task_ptr = ts.getCurrentTask();
  1525. test_output.push_back("got_current_task");
  1526. };
  1527. Task task(100, 1, current_task_callback, &ts, true);
  1528. task_address = &task;
  1529. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 1; });
  1530. EXPECT_TRUE(success);
  1531. EXPECT_EQ(current_task_ptr, task_address);
  1532. }
  1533. // ================== SCHEDULER TIMING CONTROL TESTS ==================
  1534. /**
  1535. * @brief Test Scheduler startNow() method for immediate execution trigger
  1536. *
  1537. * TESTS: startNow()
  1538. *
  1539. * PURPOSE: Verify that startNow() forces all enabled tasks to execute
  1540. * immediately on the next scheduler pass, regardless of their current
  1541. * timing state, enabling synchronized restart and emergency execution.
  1542. *
  1543. * START NOW BEHAVIOR:
  1544. * - startNow(): Forces immediate execution of all enabled tasks
  1545. * - Bypasses all interval and delay timing
  1546. * - Affects all tasks managed by scheduler
  1547. * - Useful for synchronized restart and emergency execution
  1548. * - Tasks resume normal timing after startNow execution
  1549. *
  1550. * TEST SCENARIO:
  1551. * 1. Create tasks with long intervals that would normally delay execution
  1552. * 2. Let them execute once, then restart with delays
  1553. * 3. Call startNow() to bypass delays
  1554. * 4. Verify all tasks execute immediately despite delays
  1555. *
  1556. * EXPECTATIONS:
  1557. * - Initial execution: tasks run due to enable()
  1558. * - After restart+delay: tasks would normally wait
  1559. * - After startNow(): all tasks execute immediately
  1560. * - All expected outputs are produced
  1561. *
  1562. * IMPORTANCE: StartNow enables synchronized system restart, emergency
  1563. * execution, and coordinated task triggering, essential for real-time
  1564. * systems and emergency response scenarios.
  1565. */
  1566. TEST_F(SchedulerThoroughTest, SchedulerStartNow) {
  1567. Scheduler ts;
  1568. Task task1(1000, 1, &callback_1, &ts, true); // Long interval
  1569. Task task2(2000, 1, &callback_2, &ts, true); // Even longer interval
  1570. // Tasks should execute immediately due to enable(), let them
  1571. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1572. EXPECT_TRUE(success);
  1573. clearTestOutput();
  1574. // Restart tasks with long intervals
  1575. task1.restart();
  1576. task2.restart();
  1577. task1.delay(1000); // Delay them
  1578. task2.delay(2000);
  1579. // startNow should make them execute immediately
  1580. ts.startNow();
  1581. success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 2; });
  1582. EXPECT_TRUE(success);
  1583. }
  1584. // ================== INTEGRATION TESTS ==================
  1585. /**
  1586. * @brief Integration test for complete task lifecycle with all features
  1587. *
  1588. * TESTS: Full task lifecycle including enable, execute, auto-disable, restart
  1589. *
  1590. * PURPOSE: Verify that all task features work together correctly in a
  1591. * realistic usage scenario, testing the integration of multiple methods
  1592. * and lifecycle events in sequence.
  1593. *
  1594. * LIFECYCLE INTEGRATION TESTED:
  1595. * - Initial state verification (disabled, zero counters)
  1596. * - Enable with onEnable callback triggering
  1597. * - Multiple executions with proper counter tracking
  1598. * - Auto-disable after completing all iterations
  1599. * - OnDisable callback triggering after completion
  1600. * - Restart functionality with state reset
  1601. *
  1602. * TEST SCENARIO:
  1603. * 1. Create task with 3 iterations and lifecycle callbacks
  1604. * 2. Verify initial disabled state
  1605. * 3. Enable and verify onEnable callback
  1606. * 4. Execute all 3 iterations and verify counters
  1607. * 5. Verify auto-disable and onDisable callback
  1608. * 6. Restart and verify reset state and re-enable
  1609. *
  1610. * EXPECTATIONS:
  1611. * - All lifecycle callbacks triggered at correct times
  1612. * - Execution and iteration counters track correctly
  1613. * - Auto-disable occurs after completing iterations
  1614. * - Restart properly resets state and re-enables
  1615. *
  1616. * IMPORTANCE: Integration testing validates that all features work
  1617. * together as designed, ensuring reliable operation in real applications
  1618. * with complex task lifecycle requirements.
  1619. */
  1620. TEST_F(SchedulerThoroughTest, ComplexTaskLifecycle) {
  1621. Scheduler ts;
  1622. Task task(200, 3, &basic_callback, &ts, false, &test_onEnable, &test_onDisable);
  1623. // Full lifecycle test
  1624. EXPECT_FALSE(task.isEnabled());
  1625. EXPECT_EQ(task.getRunCounter(), 0);
  1626. // Enable and run
  1627. task.enable();
  1628. EXPECT_TRUE(onEnable_called);
  1629. EXPECT_TRUE(task.isEnabled());
  1630. // Run all iterations
  1631. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 3; });
  1632. EXPECT_TRUE(success);
  1633. EXPECT_EQ(callback_counter, 3);
  1634. EXPECT_EQ(task.getRunCounter(), 3);
  1635. EXPECT_FALSE(task.isEnabled()); // Auto-disabled after iterations
  1636. EXPECT_TRUE(onDisable_called);
  1637. // Restart and verify
  1638. onEnable_called = false;
  1639. onDisable_called = false;
  1640. task.restart();
  1641. EXPECT_TRUE(onEnable_called);
  1642. EXPECT_TRUE(task.isEnabled());
  1643. EXPECT_EQ(task.getIterations(), 3); // Reset
  1644. }
  1645. /**
  1646. * @brief Integration test for multiple concurrent tasks with different timing
  1647. *
  1648. * TESTS: Multiple tasks with different enable timing and execution patterns
  1649. *
  1650. * PURPOSE: Verify that the scheduler correctly manages multiple concurrent
  1651. * tasks with different timing characteristics, ensuring proper execution
  1652. * order and no interference between tasks.
  1653. *
  1654. * MULTI-TASK INTEGRATION TESTED:
  1655. * - Three tasks with different intervals (100ms, 150ms, 200ms)
  1656. * - Different enable timing: immediate, 50ms delay, 100ms delay
  1657. * - Each task executes 2 iterations (6 total executions)
  1658. * - Proper execution ordering based on timing
  1659. * - No task interference or missed executions
  1660. *
  1661. * TEST SCENARIO:
  1662. * 1. Create three tasks with different intervals and 2 iterations each
  1663. * 2. Enable task1 immediately, task2 after 50ms, task3 after 100ms
  1664. * 3. Run until all 6 executions complete (2 per task)
  1665. * 4. Verify first execution follows enable timing order
  1666. * 5. Confirm all tasks complete their iterations
  1667. *
  1668. * EXPECTATIONS:
  1669. * - All 6 executions complete successfully
  1670. * - First execution is from task1 (immediate enable)
  1671. * - Total execution count reaches 6
  1672. * - No tasks interfere with each other
  1673. *
  1674. * IMPORTANCE: Multi-task integration validates scheduler's core
  1675. * functionality under realistic concurrent workloads, ensuring
  1676. * reliable operation in complex applications.
  1677. */
  1678. TEST_F(SchedulerThoroughTest, MultipleTasksInteraction) {
  1679. Scheduler ts;
  1680. Task task1(100, 2, &callback_1, &ts, false);
  1681. Task task2(150, 2, &callback_2, &ts, false);
  1682. Task task3(200, 2, &callback_3, &ts, false);
  1683. // Enable with different timings
  1684. task1.enable();
  1685. task2.enableDelayed(50);
  1686. task3.enableDelayed(100);
  1687. // All should execute their iterations
  1688. bool success = runSchedulerUntil(ts, []() { return getTestOutputCount() >= 6; });
  1689. EXPECT_TRUE(success);
  1690. // Verify execution order (first executions should follow timing)
  1691. EXPECT_EQ(getTestOutput(0), "callback_1"); // Immediate
  1692. // Subsequent order may vary due to intervals
  1693. }
  1694. // ================== EDGE CASES AND ERROR HANDLING ==================
  1695. /**
  1696. * @brief Edge case test for task with zero iterations
  1697. *
  1698. * TESTS: Task behavior with 0 iterations
  1699. *
  1700. * PURPOSE: Verify that tasks created with zero iterations behave correctly
  1701. * by not executing and remaining disabled, ensuring safe handling of
  1702. * degenerate iteration counts.
  1703. *
  1704. * ZERO ITERATIONS BEHAVIOR:
  1705. * - Task with 0 iterations should never execute
  1706. * - Task should be automatically disabled (no valid executions)
  1707. * - Scheduler should safely handle such tasks without errors
  1708. * - Useful for conditional task creation patterns
  1709. *
  1710. * TEST SCENARIO:
  1711. * 1. Create task with 0 iterations but enabled=true
  1712. * 2. Run scheduler multiple times
  1713. * 3. Verify no executions occur
  1714. * 4. Verify task becomes/remains disabled
  1715. *
  1716. * EXPECTATIONS:
  1717. * - Callback counter remains 0 (no executions)
  1718. * - Task is disabled (invalid iteration count)
  1719. * - No errors or crashes occur
  1720. *
  1721. * IMPORTANCE: Zero iteration handling prevents invalid execution
  1722. * states and enables conditional task creation patterns, essential
  1723. * for robust edge case handling.
  1724. */
  1725. TEST_F(SchedulerThoroughTest, TaskZeroIterations) {
  1726. Scheduler ts;
  1727. Task task(100, 0, &basic_callback, &ts, true);
  1728. // Should not execute with 0 iterations
  1729. delay(200);
  1730. ts.execute();
  1731. EXPECT_EQ(callback_counter, 0);
  1732. EXPECT_FALSE(task.isEnabled()); // Should be disabled
  1733. }
  1734. /**
  1735. * @brief Edge case test for task with infinite iterations (TASK_FOREVER)
  1736. *
  1737. * TESTS: Task behavior with TASK_FOREVER iterations
  1738. *
  1739. * PURPOSE: Verify that tasks configured with TASK_FOREVER execute
  1740. * indefinitely without auto-disabling, maintaining consistent behavior
  1741. * for infinite execution scenarios.
  1742. *
  1743. * INFINITE ITERATIONS BEHAVIOR:
  1744. * - TASK_FOREVER (-1) indicates infinite iterations
  1745. * - Task never auto-disables due to iteration count
  1746. * - getIterations() continues returning TASK_FOREVER
  1747. * - Executions continue until manually disabled
  1748. * - Essential for background and monitoring tasks
  1749. *
  1750. * TEST SCENARIO:
  1751. * 1. Create task with TASK_FOREVER iterations
  1752. * 2. Run scheduler until multiple executions occur
  1753. * 3. Verify task remains enabled throughout
  1754. * 4. Verify getIterations() still returns TASK_FOREVER
  1755. *
  1756. * EXPECTATIONS:
  1757. * - Task executes at least 5 times within timeout
  1758. * - Task remains enabled after multiple executions
  1759. * - getIterations() continues returning TASK_FOREVER
  1760. * - No auto-disable occurs
  1761. *
  1762. * IMPORTANCE: Infinite iteration support enables background tasks,
  1763. * monitoring loops, and continuous processing, essential for
  1764. * long-running and service-oriented applications.
  1765. */
  1766. TEST_F(SchedulerThoroughTest, TaskInfiniteIterations) {
  1767. Scheduler ts;
  1768. Task task(50, TASK_FOREVER, &basic_callback, &ts, true);
  1769. // Should keep running indefinitely
  1770. bool success = runSchedulerUntil(ts, []() { return callback_counter >= 5; }, 400);
  1771. EXPECT_TRUE(success);
  1772. EXPECT_TRUE(task.isEnabled()); // Should still be enabled
  1773. EXPECT_EQ(task.getIterations(), TASK_FOREVER); // Should remain -1
  1774. }
  1775. /**
  1776. * @brief Edge case test for task with null callback pointer
  1777. *
  1778. * TESTS: Task behavior with nullptr callback
  1779. *
  1780. * PURPOSE: Verify that tasks created with null callback pointers handle
  1781. * execution gracefully without crashing, demonstrating robustness in
  1782. * edge cases and enabling placeholder task patterns.
  1783. *
  1784. * NULL CALLBACK BEHAVIOR:
  1785. * - Task with nullptr callback should not crash during execution
  1786. * - Task lifecycle continues normally (timing, iterations, etc.)
  1787. * - No callback code executes (safe no-op behavior)
  1788. * - Enables placeholder and template task patterns
  1789. * - Demonstrates scheduler robustness
  1790. *
  1791. * TEST SCENARIO:
  1792. * 1. Create task with nullptr callback but valid parameters
  1793. * 2. Run scheduler and let task execute
  1794. * 3. Verify no crashes or exceptions occur
  1795. * 4. Verify no callback-specific effects occur
  1796. *
  1797. * EXPECTATIONS:
  1798. * - No crashes or exceptions during execution
  1799. * - Callback counter remains 0 (no callback executed)
  1800. * - Task lifecycle proceeds normally
  1801. * - Scheduler continues operating safely
  1802. *
  1803. * IMPORTANCE: Null callback handling ensures scheduler robustness
  1804. * and enables placeholder task patterns, essential for template
  1805. * systems and defensive programming practices.
  1806. */
  1807. TEST_F(SchedulerThoroughTest, TaskNullCallback) {
  1808. Scheduler ts;
  1809. Task task(100, 3, nullptr, &ts, true);
  1810. // Should not crash with null callback
  1811. delay(200);
  1812. ts.execute();
  1813. EXPECT_EQ(callback_counter, 0); // No callback executed
  1814. // Task should still run through its lifecycle
  1815. }
  1816. /**
  1817. * @brief Main test runner function
  1818. *
  1819. * Initializes Google Test framework and runs all registered test cases.
  1820. * Called by the test execution environment to start the testing process.
  1821. * Returns 0 for success, non-zero for test failures.
  1822. *
  1823. * @param argc Command line argument count
  1824. * @param argv Command line argument values
  1825. * @return Test execution status (0 = success)
  1826. */
  1827. int main(int argc, char **argv) {
  1828. ::testing::InitGoogleTest(&argc, argv);
  1829. return RUN_ALL_TESTS();
  1830. }