Taylor Cramer | 096c152 | 2023-07-25 18:44:16 +0000 | [diff] [blame] | 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 <memory> |
| 16 | #include <optional> |
| 17 | |
| 18 | #include "pw_async/function_dispatcher.h" |
| 19 | #include "pw_async/heap_dispatcher.h" |
| 20 | #include "pw_async_basic/dispatcher.h" |
| 21 | #include "pw_async_bench/callback_impl.h" |
| 22 | #include "pw_thread/thread.h" |
| 23 | #include "pw_thread_stl/options.h" |
| 24 | |
| 25 | int main() { |
| 26 | pw::async::BasicDispatcher basic_dispatcher; |
| 27 | pw::thread::Thread work_thread(pw::thread::stl::Options(), basic_dispatcher); |
| 28 | // `HeapDispatcher` is needed so that we can post callbacks without managing |
| 29 | // `Task` object lifetimes within the class that makes an asynchronous call. |
| 30 | pw::async::HeapDispatcher heap_dispatcher(basic_dispatcher); |
| 31 | pw::async_bench::RpcSystem rpc_system(heap_dispatcher); |
| 32 | |
| 33 | const char* ECHO_VALUE = "some value"; |
| 34 | pw::async_bench::EchoRequest request{ECHO_VALUE}; |
| 35 | std::optional<pw::Result<pw::async_bench::EchoResponse>> result_storage( |
| 36 | std::nullopt); |
| 37 | |
| 38 | pw::async_bench::RemoteEcho remote(rpc_system); |
| 39 | pw::async_bench::ProxyEchoImpl impl(remote); |
| 40 | pw::async_bench::PostEcho( |
| 41 | rpc_system, impl, std::move(request), result_storage); |
| 42 | basic_dispatcher.RunUntilIdle(); |
| 43 | |
| 44 | PW_ASSERT(result_storage.has_value()); |
| 45 | PW_ASSERT(result_storage->ok()); |
| 46 | PW_ASSERT(result_storage->value().value == ECHO_VALUE); |
| 47 | return 0; |
| 48 | } |