1 // Copyright 2024 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_channel/stream_channel.h"
16
17 #include <algorithm>
18 #include <array>
19
20 #include "pw_async2/pend_func_task.h"
21 #include "pw_bytes/suffix.h"
22 #include "pw_multibuf/simple_allocator_for_test.h"
23 #include "pw_status/status.h"
24 #include "pw_stream/mpsc_stream.h"
25 #include "pw_thread/test_thread_context.h"
26 #include "pw_unit_test/framework.h"
27
28 namespace {
29
30 using ::pw::async2::Context;
31 using ::pw::async2::PendFuncTask;
32 using ::pw::async2::Pending;
33 using ::pw::async2::Poll;
34 using ::pw::async2::Ready;
35 using ::pw::multibuf::MultiBuf;
36 using ::pw::multibuf::test::SimpleAllocatorForTest;
37 using ::pw::operator""_b;
38
39 template <typename ActualIterable, typename ExpectedIterable>
ExpectElementsEqual(const ActualIterable & actual,const ExpectedIterable & expected)40 void ExpectElementsEqual(const ActualIterable& actual,
41 const ExpectedIterable& expected) {
42 auto actual_iter = actual.begin();
43 auto expected_iter = expected.begin();
44 for (; expected_iter != expected.end(); ++actual_iter, ++expected_iter) {
45 ASSERT_NE(actual_iter, actual.end());
46 EXPECT_EQ(*actual_iter, *expected_iter);
47 }
48 }
49
50 template <typename ActualIterable, typename T>
ExpectElementsEqual(const ActualIterable & actual,std::initializer_list<T> expected)51 void ExpectElementsEqual(const ActualIterable& actual,
52 std::initializer_list<T> expected) {
53 ExpectElementsEqual<ActualIterable, std::initializer_list<T>>(actual,
54 expected);
55 }
56
57 struct LiveForeverTestData {
LiveForeverTestData__anon9af3b3a00111::LiveForeverTestData58 LiveForeverTestData() {
59 pw::stream::CreateMpscStream(channel_input_reader, channel_input_writer);
60 pw::stream::CreateMpscStream(channel_output_reader, channel_output_writer);
61 }
62 pw::stream::BufferedMpscReader<512> channel_input_reader;
63 pw::stream::MpscWriter channel_input_writer;
64 pw::stream::BufferedMpscReader<512> channel_output_reader;
65 pw::stream::MpscWriter channel_output_writer;
66 SimpleAllocatorForTest<> allocator;
67 pw::thread::test::TestThreadContext read_thread_cx;
68 pw::thread::test::TestThreadContext write_thread_cx;
69 };
70
TEST(StreamChannel,ReadsAndWritesData)71 TEST(StreamChannel, ReadsAndWritesData) {
72 static pw::NoDestructor<LiveForeverTestData> test_data;
73 static pw::NoDestructor<pw::channel::StreamChannel> stream_channel(
74 test_data->allocator,
75 test_data->channel_input_reader,
76 test_data->read_thread_cx.options(),
77 test_data->channel_output_writer,
78 test_data->write_thread_cx.options());
79
80 PendFuncTask read_task([&](Context& cx) -> Poll<> {
81 auto read = stream_channel->PendRead(cx);
82 if (read.IsPending()) {
83 return Pending();
84 }
85 EXPECT_EQ(read->status(), pw::OkStatus());
86 if (read->ok()) {
87 ExpectElementsEqual(**read, {1_b, 2_b, 3_b});
88 }
89 return Ready();
90 });
91
92 MultiBuf to_send = test_data->allocator.BufWith({4_b, 5_b, 6_b});
93 PendFuncTask write_task([&](Context& cx) -> Poll<> {
94 if (stream_channel->PendReadyToWrite(cx).IsPending()) {
95 return Pending();
96 }
97 PW_TEST_EXPECT_OK(stream_channel->StageWrite(std::move(to_send)));
98 return Ready();
99 });
100
101 pw::async2::Dispatcher dispatcher;
102 dispatcher.Post(write_task);
103 dispatcher.Post(read_task);
104
105 EXPECT_EQ(Pending(), dispatcher.RunUntilStalled());
106 std::array<const std::byte, 3> data_to_send({1_b, 2_b, 3_b});
107 ASSERT_EQ(pw::OkStatus(),
108 test_data->channel_input_writer.Write(data_to_send));
109 dispatcher.RunToCompletion();
110 }
111
112 } // namespace
113