C/C++ API Reference
Loading...
Searching...
No Matches
test_packet_channel.h
1// Copyright 2025 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#pragma once
15
16#include <iterator>
17
18#include "pw_allocator/allocator.h"
19#include "pw_assert/assert.h"
20#include "pw_async2/waker.h"
21#include "pw_channel/packet_channel.h"
22#include "pw_containers/dynamic_deque.h"
23#include "pw_containers/dynamic_vector.h"
24#include "pw_span/span.h"
25
26namespace pw::channel {
27
29
31template <typename Packet>
33 : public Implement<PacketReaderWriter<Packet>> {
34 public:
35 explicit constexpr TestPacketReaderWriter(allocator::Allocator& allocator)
36 : read_queue_(allocator), staged_(allocator), written_(allocator) {}
37
39 span<const Packet> written_packets() const { return written_; }
40
42 void EnqueueReadPacket(Packet&& packet) {
43 read_queue_.push_back(std::move(packet));
44 std::move(read_waker_).Wake();
45 }
46
47 private:
48 using AnyPacketChannel<Packet>::write_waker;
49
50 async2::PollResult<Packet> DoPendRead(async2::Context& cx) override {
51 if (read_queue_.empty()) {
53 cx, read_waker_, "TestPacketReaderWriter::DoPendRead");
54 return async2::Pending();
55 }
57 async2::Ready(std::move(read_queue_.front()));
58 read_queue_.pop_front();
59 return result;
60 }
61
62 async2::Poll<Status> DoPendReadyToWrite(async2::Context& cx,
63 size_t count) override {
64 // The staged_ queue capacity represents a write reservation.
65 if (staged_.capacity() != 0u) {
67 cx, write_waker(), "TestPacketReaderWriter::DoPendReadyToWrite");
68 return async2::Pending();
69 }
70 staged_.reserve_exact(
71 static_cast<typename decltype(staged_)::size_type>(count));
72 return async2::Ready(OkStatus());
73 }
74
75 void DoStageWrite(Packet&& packet) override {
76 PW_ASSERT(staged_.size() < staged_.capacity());
77 staged_.push_back(std::move(packet));
78 }
79
80 async2::Poll<> DoPendWrite(async2::Context&) override {
81 FlushStaged();
82 return async2::Ready();
83 }
84
85 async2::Poll<Status> DoPendClose(async2::Context&) override {
86 FlushStaged();
87 return async2::Ready(OkStatus());
88 }
89
90 void FlushStaged() {
91 // TODO: b/424613355 - use `insert` instead of repeated `push_back`
92 auto it = std::make_move_iterator(staged_.begin());
93 const auto end = std::make_move_iterator(staged_.end());
94 while (it != end) {
95 written_.push_back(std::move(*it++));
96 }
97 staged_.clear();
98 staged_.shrink_to_fit();
99 std::move(write_waker()).Wake();
100 }
101
102 async2::Waker read_waker_;
103 DynamicDeque<Packet> read_queue_;
104 DynamicDeque<Packet> staged_;
105 DynamicVector<Packet> written_;
106};
107
108} // namespace pw::channel
Definition: allocator.h:36
Definition: context.h:55
Definition: poll.h:60
Definition: packet_channel.h:222
Definition: channel.h:583
pw::channel::PacketReadWriter implementation for testing use.
Definition: test_packet_channel.h:33
constexpr size_type size() const noexcept
Returns the number of elements in the deque.
Definition: generic_deque.h:64
constexpr size_type capacity() const noexcept
Returns the maximum number of elements in the deque.
Definition: generic_deque.h:69
Definition: span_impl.h:235
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
#define PW_ASYNC_STORE_WAKER(context, waker_or_queue_out, wait_reason_string)
Definition: waker.h:60
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
void EnqueueReadPacket(Packet &&packet)
Enqueues packets to be returned from future PendRead calls.
Definition: test_packet_channel.h:42
span< const Packet > written_packets() const
Returns all packets that have been written to this packet channel.
Definition: test_packet_channel.h:39
void reserve_exact(size_type new_capacity)
Increases capacity() to exactly new_capacity. Crashes on failure.
Definition: dynamic_deque.h:167
void shrink_to_fit()
Attempts to reduce capacity() to size(). Not guaranteed to succeed.
Definition: dynamic_deque.h:261
void push_back(const value_type &value)
Definition: dynamic_vector.h:260
constexpr Status OkStatus()
Definition: status.h:297