xref: /aosp_15_r20/external/pigweed/pw_multibuf/allocator_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1*61c4878aSAndroid Build Coastguard Worker // Copyright 2024 The Pigweed Authors
2*61c4878aSAndroid Build Coastguard Worker //
3*61c4878aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4*61c4878aSAndroid Build Coastguard Worker // use this file except in compliance with the License. You may obtain a copy of
5*61c4878aSAndroid Build Coastguard Worker // the License at
6*61c4878aSAndroid Build Coastguard Worker //
7*61c4878aSAndroid Build Coastguard Worker //     https://www.apache.org/licenses/LICENSE-2.0
8*61c4878aSAndroid Build Coastguard Worker //
9*61c4878aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*61c4878aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11*61c4878aSAndroid Build Coastguard Worker // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12*61c4878aSAndroid Build Coastguard Worker // License for the specific language governing permissions and limitations under
13*61c4878aSAndroid Build Coastguard Worker // the License.
14*61c4878aSAndroid Build Coastguard Worker 
15*61c4878aSAndroid Build Coastguard Worker #include "pw_multibuf/allocator.h"
16*61c4878aSAndroid Build Coastguard Worker 
17*61c4878aSAndroid Build Coastguard Worker #include "gtest/gtest.h"
18*61c4878aSAndroid Build Coastguard Worker #include "pw_async2/dispatcher.h"
19*61c4878aSAndroid Build Coastguard Worker #include "pw_async2/poll.h"
20*61c4878aSAndroid Build Coastguard Worker 
21*61c4878aSAndroid Build Coastguard Worker namespace pw::multibuf {
22*61c4878aSAndroid Build Coastguard Worker namespace {
23*61c4878aSAndroid Build Coastguard Worker 
24*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Context;
25*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Dispatcher;
26*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Pending;
27*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Poll;
28*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Ready;
29*61c4878aSAndroid Build Coastguard Worker using ::pw::async2::Task;
30*61c4878aSAndroid Build Coastguard Worker using ::pw::multibuf::ContiguityRequirement;
31*61c4878aSAndroid Build Coastguard Worker using ::pw::multibuf::kAllowDiscontiguous;
32*61c4878aSAndroid Build Coastguard Worker 
33*61c4878aSAndroid Build Coastguard Worker struct AllocateExpectation {
34*61c4878aSAndroid Build Coastguard Worker   size_t min_size;
35*61c4878aSAndroid Build Coastguard Worker   size_t desired_size;
36*61c4878aSAndroid Build Coastguard Worker   ContiguityRequirement contiguous;
37*61c4878aSAndroid Build Coastguard Worker   pw::Result<MultiBuf> result;
38*61c4878aSAndroid Build Coastguard Worker };
39*61c4878aSAndroid Build Coastguard Worker 
40*61c4878aSAndroid Build Coastguard Worker class MockMultiBufAllocator : public MultiBufAllocator {
41*61c4878aSAndroid Build Coastguard Worker  public:
~MockMultiBufAllocator()42*61c4878aSAndroid Build Coastguard Worker   ~MockMultiBufAllocator() override {
43*61c4878aSAndroid Build Coastguard Worker     // All expectations should have been met and removed.
44*61c4878aSAndroid Build Coastguard Worker     EXPECT_EQ(expected_allocate_, std::nullopt);
45*61c4878aSAndroid Build Coastguard Worker   }
46*61c4878aSAndroid Build Coastguard Worker 
ExpectAllocateAndReturn(size_t min_size,size_t desired_size,ContiguityRequirement contiguous,pw::Result<MultiBuf> result)47*61c4878aSAndroid Build Coastguard Worker   void ExpectAllocateAndReturn(size_t min_size,
48*61c4878aSAndroid Build Coastguard Worker                                size_t desired_size,
49*61c4878aSAndroid Build Coastguard Worker                                ContiguityRequirement contiguous,
50*61c4878aSAndroid Build Coastguard Worker                                pw::Result<MultiBuf> result) {
51*61c4878aSAndroid Build Coastguard Worker     // Multiple simultaneous expectations are not supported.
52*61c4878aSAndroid Build Coastguard Worker     ASSERT_FALSE(expected_allocate_.has_value());
53*61c4878aSAndroid Build Coastguard Worker     expected_allocate_ = {
54*61c4878aSAndroid Build Coastguard Worker         min_size, desired_size, contiguous, std::move(result)};
55*61c4878aSAndroid Build Coastguard Worker   }
56*61c4878aSAndroid Build Coastguard Worker 
57*61c4878aSAndroid Build Coastguard Worker   using MultiBufAllocator::MoreMemoryAvailable;
58*61c4878aSAndroid Build Coastguard Worker 
59*61c4878aSAndroid Build Coastguard Worker  private:
DoAllocate(size_t min_size,size_t desired_size,ContiguityRequirement contiguous)60*61c4878aSAndroid Build Coastguard Worker   pw::Result<MultiBuf> DoAllocate(size_t min_size,
61*61c4878aSAndroid Build Coastguard Worker                                   size_t desired_size,
62*61c4878aSAndroid Build Coastguard Worker                                   ContiguityRequirement contiguous) final {
63*61c4878aSAndroid Build Coastguard Worker     EXPECT_NE(expected_allocate_, std::nullopt);
64*61c4878aSAndroid Build Coastguard Worker     if (!expected_allocate_.has_value()) {
65*61c4878aSAndroid Build Coastguard Worker       return Status::FailedPrecondition();
66*61c4878aSAndroid Build Coastguard Worker     }
67*61c4878aSAndroid Build Coastguard Worker     AllocateExpectation expected = std::move(*expected_allocate_);
68*61c4878aSAndroid Build Coastguard Worker     expected_allocate_ = std::nullopt;
69*61c4878aSAndroid Build Coastguard Worker     EXPECT_EQ(min_size, expected.min_size);
70*61c4878aSAndroid Build Coastguard Worker     EXPECT_EQ(desired_size, expected.desired_size);
71*61c4878aSAndroid Build Coastguard Worker     EXPECT_EQ(contiguous, expected.contiguous);
72*61c4878aSAndroid Build Coastguard Worker     return std::move(expected.result);
73*61c4878aSAndroid Build Coastguard Worker   }
74*61c4878aSAndroid Build Coastguard Worker 
75*61c4878aSAndroid Build Coastguard Worker   std::optional<AllocateExpectation> expected_allocate_;
76*61c4878aSAndroid Build Coastguard Worker };
77*61c4878aSAndroid Build Coastguard Worker 
78*61c4878aSAndroid Build Coastguard Worker class AllocateTask : public Task {
79*61c4878aSAndroid Build Coastguard Worker  public:
AllocateTask(MultiBufAllocationFuture && future)80*61c4878aSAndroid Build Coastguard Worker   AllocateTask(MultiBufAllocationFuture&& future)
81*61c4878aSAndroid Build Coastguard Worker       : future_(std::move(future)), last_result_(Pending()) {}
82*61c4878aSAndroid Build Coastguard Worker 
83*61c4878aSAndroid Build Coastguard Worker   MultiBufAllocationFuture future_;
84*61c4878aSAndroid Build Coastguard Worker   Poll<std::optional<MultiBuf>> last_result_;
85*61c4878aSAndroid Build Coastguard Worker 
86*61c4878aSAndroid Build Coastguard Worker  private:
DoPend(Context & cx)87*61c4878aSAndroid Build Coastguard Worker   Poll<> DoPend(Context& cx) override {
88*61c4878aSAndroid Build Coastguard Worker     last_result_ = future_.Pend(cx);
89*61c4878aSAndroid Build Coastguard Worker     if (last_result_.IsReady()) {
90*61c4878aSAndroid Build Coastguard Worker       return Ready();
91*61c4878aSAndroid Build Coastguard Worker     }
92*61c4878aSAndroid Build Coastguard Worker     return Pending();
93*61c4878aSAndroid Build Coastguard Worker   }
94*61c4878aSAndroid Build Coastguard Worker };
95*61c4878aSAndroid Build Coastguard Worker 
TEST(MultiBufAllocator,AllocateAsyncReturnsImmediatelyAvailableAllocation)96*61c4878aSAndroid Build Coastguard Worker TEST(MultiBufAllocator, AllocateAsyncReturnsImmediatelyAvailableAllocation) {
97*61c4878aSAndroid Build Coastguard Worker   MockMultiBufAllocator alloc;
98*61c4878aSAndroid Build Coastguard Worker   AllocateTask task(alloc.AllocateAsync(44, 33));
99*61c4878aSAndroid Build Coastguard Worker   alloc.ExpectAllocateAndReturn(44, 33, kAllowDiscontiguous, MultiBuf());
100*61c4878aSAndroid Build Coastguard Worker 
101*61c4878aSAndroid Build Coastguard Worker   Dispatcher dispatcher;
102*61c4878aSAndroid Build Coastguard Worker   dispatcher.Post(task);
103*61c4878aSAndroid Build Coastguard Worker   EXPECT_EQ(dispatcher.RunUntilStalled(), Ready());
104*61c4878aSAndroid Build Coastguard Worker 
105*61c4878aSAndroid Build Coastguard Worker   ASSERT_TRUE(task.last_result_.IsReady());
106*61c4878aSAndroid Build Coastguard Worker   ASSERT_TRUE(task.last_result_->has_value());
107*61c4878aSAndroid Build Coastguard Worker }
108*61c4878aSAndroid Build Coastguard Worker 
TEST(MultiBufAllocator,AllocateAsyncWillNotPollUntilMoreMemoryAvailable)109*61c4878aSAndroid Build Coastguard Worker TEST(MultiBufAllocator, AllocateAsyncWillNotPollUntilMoreMemoryAvailable) {
110*61c4878aSAndroid Build Coastguard Worker   MockMultiBufAllocator alloc;
111*61c4878aSAndroid Build Coastguard Worker   AllocateTask task(alloc.AllocateAsync(44, 33));
112*61c4878aSAndroid Build Coastguard Worker   Dispatcher dispatcher;
113*61c4878aSAndroid Build Coastguard Worker   dispatcher.Post(task);
114*61c4878aSAndroid Build Coastguard Worker 
115*61c4878aSAndroid Build Coastguard Worker   // First attempt will return `ResourceExhausted` to signal temporary OOM.
116*61c4878aSAndroid Build Coastguard Worker   alloc.ExpectAllocateAndReturn(
117*61c4878aSAndroid Build Coastguard Worker       44, 33, kAllowDiscontiguous, Status::ResourceExhausted());
118*61c4878aSAndroid Build Coastguard Worker   EXPECT_TRUE(dispatcher.RunUntilStalled().IsPending());
119*61c4878aSAndroid Build Coastguard Worker   EXPECT_TRUE(task.last_result_.IsPending());
120*61c4878aSAndroid Build Coastguard Worker 
121*61c4878aSAndroid Build Coastguard Worker   // Re-running the dispatcher should not poll the pending task since it has
122*61c4878aSAndroid Build Coastguard Worker   // not been awoken. `AllocateAndReturn` should *not* be called.
123*61c4878aSAndroid Build Coastguard Worker   EXPECT_TRUE(dispatcher.RunUntilStalled().IsPending());
124*61c4878aSAndroid Build Coastguard Worker 
125*61c4878aSAndroid Build Coastguard Worker   // Insufficient memory should not awaken the task.
126*61c4878aSAndroid Build Coastguard Worker   alloc.MoreMemoryAvailable(30, 30);
127*61c4878aSAndroid Build Coastguard Worker   EXPECT_TRUE(dispatcher.RunUntilStalled().IsPending());
128*61c4878aSAndroid Build Coastguard Worker 
129*61c4878aSAndroid Build Coastguard Worker   // Sufficient memory will awaken and return the memory
130*61c4878aSAndroid Build Coastguard Worker   alloc.MoreMemoryAvailable(50, 50);
131*61c4878aSAndroid Build Coastguard Worker   alloc.ExpectAllocateAndReturn(44, 33, kAllowDiscontiguous, MultiBuf());
132*61c4878aSAndroid Build Coastguard Worker   EXPECT_TRUE(dispatcher.RunUntilStalled().IsReady());
133*61c4878aSAndroid Build Coastguard Worker }
134*61c4878aSAndroid Build Coastguard Worker 
135*61c4878aSAndroid Build Coastguard Worker }  // namespace
136*61c4878aSAndroid Build Coastguard Worker }  // namespace pw::multibuf
137