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
15 #include "pw_async2/pend_func_task.h"
16
17 #include <optional>
18 #include <utility>
19
20 #include "pw_async2/dispatcher.h"
21 #include "pw_function/function.h"
22 #include "pw_unit_test/framework.h"
23
24 namespace {
25
26 using ::pw::Function;
27 using ::pw::async2::Context;
28 using ::pw::async2::Dispatcher;
29 using ::pw::async2::PendFuncTask;
30 using ::pw::async2::Pending;
31 using ::pw::async2::Poll;
32 using ::pw::async2::Ready;
33 using ::pw::async2::Waker;
34
TEST(PendFuncTask,PendDelegatesToFunc)35 TEST(PendFuncTask, PendDelegatesToFunc) {
36 Dispatcher dispatcher;
37
38 Waker waker;
39 int poll_count = 0;
40 bool allow_completion = false;
41
42 PendFuncTask func_task([&](Context& cx) -> Poll<> {
43 ++poll_count;
44 if (allow_completion) {
45 return Ready();
46 }
47 PW_ASYNC_STORE_WAKER(cx, waker, "func_task is waiting for waker");
48 return Pending();
49 });
50
51 dispatcher.Post(func_task);
52
53 EXPECT_EQ(poll_count, 0);
54 EXPECT_EQ(dispatcher.RunUntilStalled(), Pending());
55 EXPECT_EQ(poll_count, 1);
56
57 // Unwoken task is not polled.
58 EXPECT_EQ(dispatcher.RunUntilStalled(), Pending());
59 EXPECT_EQ(poll_count, 1);
60
61 std::move(waker).Wake();
62 allow_completion = true;
63 EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
64 EXPECT_EQ(poll_count, 2);
65 }
66
TEST(PendFuncTask,HoldsCallableByDefault)67 TEST(PendFuncTask, HoldsCallableByDefault) {
68 auto callable = [](Context&) -> Poll<> { return Ready(); };
69 PendFuncTask func_task(std::move(callable));
70 static_assert(std::is_same<decltype(func_task),
71 PendFuncTask<decltype(callable)>>::value);
72 }
73
TEST(PendFuncTask,HoldsPwFunctionWithEmptyTypeList)74 TEST(PendFuncTask, HoldsPwFunctionWithEmptyTypeList) {
75 PendFuncTask<> func_task([](Context&) -> Poll<> { return Ready(); });
76 static_assert(std::is_same<decltype(func_task),
77 PendFuncTask<Function<Poll<>(Context&)>>>::value);
78 }
79
ReturnsReady(Context &)80 Poll<> ReturnsReady(Context&) { return Ready(); }
81
TEST(PendFuncTask,TestTemplateDeductionAndSize)82 TEST(PendFuncTask, TestTemplateDeductionAndSize) {
83 // A PendFuncTask with an unspecified Func template parameter will default
84 // to pw::Function. This allows the same container to hold a variety of
85 // different callables, but it may either reserve extra inline storage or
86 // dynamically allocate momeory, depending on how pw::Function is configured.
87 std::optional<PendFuncTask<>> a;
88 a.emplace([](Context&) -> Poll<> { return Ready(); });
89 a.emplace(&ReturnsReady);
90 static_assert(sizeof(decltype(a)::value_type::CallableType) ==
91 sizeof(Function<Poll<>(Context&)>));
92
93 // When constructing a PendFuncTask directly from a callable, CTAD will match
94 // the Func template parameter to that of the callable. This has the
95 // benefit of reducing the amount of storage needed vs that of a pw::Function.
96 //
97 // A lambda without any captures doesn't require any storage.
98 auto b = PendFuncTask([](Context&) -> Poll<> { return Ready(); });
99 static_assert(sizeof(decltype(b)::CallableType) <= 1);
100
101 // A lambda with captures requires storage to hold the captures.
102 int scratch = 6;
103 auto c = PendFuncTask(
104 [&scratch](Context&) -> Poll<> { return scratch ? Ready() : Pending(); });
105 static_assert(sizeof(decltype(c)::CallableType) == sizeof(&scratch));
106
107 // A raw function pointer just needs storage for the pointer value.
108 auto d = PendFuncTask(&ReturnsReady);
109 static_assert(sizeof(decltype(d)::CallableType) == sizeof(&ReturnsReady));
110 }
111
112 } // namespace
113