C/C++ API Reference
Loading...
Searching...
No Matches
integration_testing.h
1// Copyright 2021 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 <cstdint>
17
18#include "pw_function/function.h"
19#include "pw_rpc/client.h"
20#include "pw_status/status.h"
21
22namespace pw::rpc::integration_test {
23
25
26// The RPC channel for integration test RPCs.
27inline constexpr uint32_t kChannelId = 1;
28
29// An injectable pipe interface that may manipulate packets before they're sent
30// to the final destination.
31//
32// ``ChannelManipulator``s allow application-specific packet handling to be
33// injected into the packet processing pipeline for an ingress or egress
34// channel-like pathway. This is particularly useful for integration testing
35// resilience to things like packet loss on a usually-reliable transport. RPC
36// server integrations may provide an opportunity to inject a
37// ``ChannelManipulator`` for this use case.
38//
39// A ``ChannelManipulator`` should not set send_packet_, as the consumer of a
40// ``ChannelManipulator`` will use ``send_packet`` to insert the provided
41// ``ChannelManipulator`` into a packet processing path.
43 public:
44 // The expected function signature of the send callback used by a
45 // ChannelManipulator to forward packets to the final destination.
46 //
47 // The only argument is a byte span containing the RPC packet that should
48 // be sent.
49 //
50 // Returns:
51 // OK - Packet successfully sent.
52 // Other - Failed to send packet.
53 using SendCallback = Function<Status(span<const std::byte>)>;
54
56 : send_packet_(
58 virtual ~ChannelManipulator() {}
59
60 // Sets the true send callback that a ChannelManipulator will use to forward
61 // packets to the final destination.
62 //
63 // This should not be used by a ChannelManipulator. The consumer of a
64 // ChannelManipulator will set the send callback.
65 void set_send_packet(SendCallback&& send) { send_packet_ = std::move(send); }
66
67 // Processes an incoming packet before optionally sending it.
68 //
69 // Implementations of this method may send the processed packet, multiple
70 // packets, or no packets at all via the registered `send_packet()`
71 // handler.
72 virtual Status ProcessAndSend(span<const std::byte> packet) = 0;
73
74 protected:
75 Status send_packet(span<const std::byte> payload) {
76 return send_packet_(payload);
77 }
78
79 private:
81};
82
83void SetEgressChannelManipulator(ChannelManipulator* new_channel_manipulator);
84
85void SetIngressChannelManipulator(ChannelManipulator* new_channel_manipulator);
86
87// Returns the global RPC client for integration test use.
88Client& client();
89
90// Configure options for the socket associated with the client.
91int SetClientSockOpt(int level,
92 int optname,
93 const void* optval,
94 unsigned int optlen);
95
96// Initializes logging and the global RPC client for integration testing. Starts
97// a background thread that processes incoming.
98Status InitializeClient(int argc,
99 char* argv[],
100 const char* usage_args = "PORT");
101
102Status InitializeClient(int port);
103
104// Terminates the client, joining the RPC dispatch thread.
105void TerminateClient();
106
108
109} // namespace pw::rpc::integration_test
Definition: status.h:109
static constexpr Status Unimplemented()
Operation isn’t implemented or supported.
Definition: status.h:177
Definition: client.h:28
Definition: integration_testing.h:42
Definition: span_impl.h:235
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73