| // Copyright 2024 The Pigweed Authors |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| // use this file except in compliance with the License. You may obtain a copy of |
| // the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| // License for the specific language governing permissions and limitations under |
| // the License. |
| #pragma once |
| |
| #include <cstddef> |
| |
| #include "modules/worker/worker.h" |
| #include "pw_function/function.h" |
| #include "pw_log/log.h" |
| #include "pw_thread/test_thread_context.h" |
| #include "pw_thread/thread.h" |
| #include "pw_unit_test/framework.h" |
| #include "pw_work_queue/work_queue.h" |
| |
| namespace sense { |
| namespace internal { |
| |
| /// A worker which delegates to a work queue running on a dedicated test |
| /// thread. Callers should use ``TestWorker`` instead. |
| class GenericTestWorker : public Worker { |
| public: |
| GenericTestWorker(pw::work_queue::WorkQueue& work_queue); |
| GenericTestWorker(const GenericTestWorker&) = delete; |
| GenericTestWorker& operator=(const GenericTestWorker&) = delete; |
| GenericTestWorker(GenericTestWorker&&) = delete; |
| GenericTestWorker& operator=(GenericTestWorker&&) = delete; |
| |
| void Start(); |
| |
| void RunOnce(pw::Function<void()>&& work) final; |
| |
| // Stops the work queue. This method MUST be called before leaving the test |
| // body. Otherwise, the work queue may reference objects that have gone out of |
| // scope. |
| void Stop(); |
| |
| protected: |
| ~GenericTestWorker(); |
| |
| private: |
| pw::work_queue::WorkQueue* work_queue_ = nullptr; |
| pw::thread::test::TestThreadContext context_; |
| pw::thread::Thread work_thread_; |
| }; |
| |
| } // namespace internal |
| |
| /// A worker which delegates to a work queue running on a dedicated test |
| /// thread. |
| template <size_t kBufferSize = 10> |
| class TestWorker final : public internal::GenericTestWorker { |
| public: |
| TestWorker() : GenericTestWorker(work_queue_) { Start(); } |
| |
| private: |
| pw::work_queue::WorkQueueWithBuffer<kBufferSize> work_queue_; |
| }; |
| |
| } // namespace sense |