Ver código fonte

KBF-37 fix Task running state

Bence Balint 2 anos atrás
pai
commit
06677ff509
3 arquivos alterados com 13 adições e 4 exclusões
  1. 2 2
      include/kbf/task.h
  2. 10 2
      src/task.cpp
  3. 1 0
      test/test_task.cpp

+ 2 - 2
include/kbf/task.h

@@ -53,7 +53,7 @@ namespace kbf::task {
          * @brief Returns whether the task is running or not.
          * @return true if running, false otherwise
          */
-        [[nodiscard]] bool running() const { return _running; }
+        [[nodiscard]] bool running() const { return running_; }
 
         /**
          * @brief Stops the task.
@@ -90,7 +90,7 @@ namespace kbf::task {
 
         TaskHandle_t handle{};
 
-        bool _running = false;
+        bool running_ = false;
 
         class TaskList {
             static constexpr const char *const TAG = "kbf::task::TaskList";

+ 10 - 2
src/task.cpp

@@ -46,17 +46,25 @@ Task::~Task() {
     ESP_LOGD(TAG, "%s()", __func__);
 
     auto task = static_cast<Task *>(arg);
-    task->_running = true;
+    task->running_ = true;
     task->run(task->arg);
+    ESP_LOGI(TAG, "task (%d, %s) finished", task->id, task->name.c_str());
 
     // TODO call onFinish
-    task->_running = false;
+    task->running_ = false;
     TaskList::remove(task->id);
     vTaskDelete(nullptr);
 }
 
 void Task::stop() {
+    if (!running_) {
+        ESP_LOGW(TAG, "%s(%d): not running", __func__, id);
+        return;
+    }
+
     ESP_LOGI(TAG, "%s(%d)", __func__, id);
+
     TaskList::remove(id);
     vTaskDelete(handle);
+    running_ = false;
 }

+ 1 - 0
test/test_task.cpp

@@ -110,6 +110,7 @@ TEST_CASE("Task", "[kbf_task]") {
 
     // stop runner; no more updates
     runnerTask->stop();
+    TEST_ASSERT_FALSE(runnerTask->running());
     expected += KBF_TASK_TEST_RUNNER_INC; // runner went another cycle while we were sleeping
     for (int i = 0; i < KBF_TASK_TEST_CYCLES; i++) {
         TEST_ASSERT_EQUAL(expected, counter);