• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_TEST_CORE_TRANSPORT_UTIL_MOCK_PROMISE_ENDPOINT_H
16 #define GRPC_TEST_CORE_TRANSPORT_UTIL_MOCK_PROMISE_ENDPOINT_H
17 
18 #include <grpc/event_engine/event_engine.h>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "src/core/lib/event_engine/tcp_socket_utils.h"
23 #include "src/core/lib/transport/promise_endpoint.h"
24 
25 namespace grpc_core {
26 namespace chaotic_good {
27 namespace testing {
28 
29 class MockEndpoint
30     : public grpc_event_engine::experimental::EventEngine::Endpoint {
31  public:
32   MOCK_METHOD(
33       bool, Read,
34       (absl::AnyInvocable<void(absl::Status)> on_read,
35        grpc_event_engine::experimental::SliceBuffer* buffer,
36        const grpc_event_engine::experimental::EventEngine::Endpoint::ReadArgs*
37            args),
38       (override));
39 
40   MOCK_METHOD(
41       bool, Write,
42       (absl::AnyInvocable<void(absl::Status)> on_writable,
43        grpc_event_engine::experimental::SliceBuffer* data,
44        const grpc_event_engine::experimental::EventEngine::Endpoint::WriteArgs*
45            args),
46       (override));
47 
48   MOCK_METHOD(
49       const grpc_event_engine::experimental::EventEngine::ResolvedAddress&,
50       GetPeerAddress, (), (const, override));
51   MOCK_METHOD(
52       const grpc_event_engine::experimental::EventEngine::ResolvedAddress&,
53       GetLocalAddress, (), (const, override));
54 };
55 
56 struct MockPromiseEndpoint {
MockPromiseEndpointMockPromiseEndpoint57   explicit MockPromiseEndpoint(int port) {
58     if (GRPC_TRACE_FLAG_ENABLED(chaotic_good)) {
59       EXPECT_CALL(*endpoint, GetPeerAddress)
60           .WillRepeatedly(
61               [peer_address =
62                    std::make_shared<grpc_event_engine::experimental::
63                                         EventEngine::ResolvedAddress>(
64                        grpc_event_engine::experimental::URIToResolvedAddress(
65                            absl::StrCat("ipv4:127.0.0.1:", port))
66                            .value())]()
67                   -> const grpc_event_engine::experimental::EventEngine::
68                       ResolvedAddress& { return *peer_address; });
69     }
70   }
71   ::testing::StrictMock<MockEndpoint>* endpoint =
72       new ::testing::StrictMock<MockEndpoint>();
73   PromiseEndpoint promise_endpoint = PromiseEndpoint(
74       std::unique_ptr<::testing::StrictMock<MockEndpoint>>(endpoint),
75       SliceBuffer());
76   ::testing::Sequence read_sequence;
77   ::testing::Sequence write_sequence;
78   void ExpectRead(
79       std::initializer_list<grpc_event_engine::experimental::Slice> slices_init,
80       grpc_event_engine::experimental::EventEngine* schedule_on_event_engine);
81   void ExpectWrite(
82       std::initializer_list<grpc_event_engine::experimental::Slice> slices,
83       grpc_event_engine::experimental::EventEngine* schedule_on_event_engine);
84   void CaptureWrites(
85       SliceBuffer& writes,
86       grpc_event_engine::experimental::EventEngine* schedule_on_event_engine);
87 };
88 
89 }  // namespace testing
90 }  // namespace chaotic_good
91 }  // namespace grpc_core
92 
93 #endif  // GRPC_TEST_CORE_TRANSPORT_UTIL_MOCK_PROMISE_ENDPOINT_H
94