1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_async/heap_dispatcher.h"
15
16 #include "pw_async/fake_dispatcher_fixture.h"
17 #include "pw_unit_test/framework.h"
18
19 using namespace std::chrono_literals;
20
21 namespace pw::async {
22 namespace {
23
24 using HeapDispatcherTest = test::FakeDispatcherFixture;
25
26 class DestructionChecker {
27 public:
DestructionChecker(bool * flag)28 DestructionChecker(bool* flag) : flag_(flag) {}
DestructionChecker(DestructionChecker && other)29 DestructionChecker(DestructionChecker&& other) {
30 flag_ = other.flag_;
31 other.flag_ = nullptr;
32 }
~DestructionChecker()33 ~DestructionChecker() {
34 if (flag_) {
35 *flag_ = true;
36 }
37 }
38
39 private:
40 bool* flag_;
41 };
42
TEST_F(HeapDispatcherTest,RunUntilIdleRunsPostedTask)43 TEST_F(HeapDispatcherTest, RunUntilIdleRunsPostedTask) {
44 HeapDispatcher heap_dispatcher(dispatcher());
45
46 int count = 0;
47 Status status = heap_dispatcher.Post(
48 [&count](Context& /*ctx*/, Status /*status*/) { ++count; });
49 EXPECT_TRUE(status.ok());
50 ASSERT_EQ(count, 0);
51 RunUntilIdle();
52 ASSERT_EQ(count, 1);
53 }
54
TEST_F(HeapDispatcherTest,TaskFunctionIsDestroyedAfterBeingCalled)55 TEST_F(HeapDispatcherTest, TaskFunctionIsDestroyedAfterBeingCalled) {
56 HeapDispatcher heap_dispatcher(dispatcher());
57
58 // Test that the lambda is destroyed after being called.
59 bool flag = false;
60 Status status =
61 heap_dispatcher.Post([checker = DestructionChecker(&flag)](
62 Context& /*ctx*/, Status /*status*/) {});
63 EXPECT_TRUE(status.ok());
64 EXPECT_FALSE(flag);
65 RunUntilIdle();
66 EXPECT_TRUE(flag);
67 }
68
69 } // namespace
70 } // namespace pw::async
71