Debian Codegraph @

test/core/util/mock_endpoint.cc

raw
1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/core/util/mock_endpoint.h"
20
21 #include "absl/status/status.h"
22 #include "absl/strings/string_view.h"
23
24 #include <grpc/slice_buffer.h>
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/sync.h>
27
28 #include "src/core/lib/gprpp/debug_location.h"
29 #include "src/core/lib/iomgr/closure.h"
30 #include "src/core/lib/iomgr/error.h"
31 #include "src/core/lib/iomgr/exec_ctx.h"
32 #include "src/core/lib/iomgr/iomgr_fwd.h"
33 #include "src/core/lib/iomgr/sockaddr.h"
34
35 typedef struct mock_endpoint {
36 grpc_endpoint base;
37 gpr_mu mu;
38 void (*on_write)(grpc_slice slice);
39 grpc_slice_buffer read_buffer;
40 grpc_slice_buffer* on_read_out;
41 grpc_closure* on_read;
42 } mock_endpoint;
43
44 static void me_read(grpc_endpoint* ep, grpc_slice_buffer* slices,
45 grpc_closure* cb, bool /*urgent*/,
46 int /*min_progress_size*/) {
47 mock_endpoint* m = reinterpret_cast<mock_endpoint*>(ep);
48 gpr_mu_lock(&m->mu);
49 if (m->read_buffer.count > 0) {
50 grpc_slice_buffer_swap(&m->read_buffer, slices);
51 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, absl::OkStatus());
52 } else {
53 m->on_read = cb;
54 m->on_read_out = slices;
55 }
56 gpr_mu_unlock(&m->mu);
57 }
58
59 static void me_write(grpc_endpoint* ep, grpc_slice_buffer* slices,
60 grpc_closure* cb, void* /*arg*/, int /*max_frame_size*/) {
61 mock_endpoint* m = reinterpret_cast<mock_endpoint*>(ep);
62 for (size_t i = 0; i < slices->count; i++) {
63 m->on_write(slices->slices[i]);
64 }
65 grpc_core::ExecCtx::Run(DEBUG_LOCATION, cb, absl::OkStatus());
66 }
67
68 static void me_add_to_pollset(grpc_endpoint* /*ep*/,
69 grpc_pollset* /*pollset*/) {}
70
71 static void me_add_to_pollset_set(grpc_endpoint* /*ep*/,
72 grpc_pollset_set* /*pollset*/) {}
73
74 static void me_delete_from_pollset_set(grpc_endpoint* /*ep*/,
75 grpc_pollset_set* /*pollset*/) {}
76
77 static void me_shutdown(grpc_endpoint* ep, grpc_error_handle why) {
78 mock_endpoint* m = reinterpret_cast<mock_endpoint*>(ep);
79 gpr_mu_lock(&m->mu);
80 if (m->on_read) {
81 grpc_core::ExecCtx::Run(
82 DEBUG_LOCATION, m->on_read,
83 GRPC_ERROR_CREATE_REFERENCING("Endpoint Shutdown", &why, 1));
84 m->on_read = nullptr;
85 }
86 gpr_mu_unlock(&m->mu);
87 }
88
89 static void me_destroy(grpc_endpoint* ep) {
90 mock_endpoint* m = reinterpret_cast<mock_endpoint*>(ep);
91 grpc_slice_buffer_destroy(&m->read_buffer);
92 gpr_mu_destroy(&m->mu);
93 gpr_free(m);
94 }
95
96 static absl::string_view me_get_peer(grpc_endpoint* /*ep*/) {
97 return "fake:mock_endpoint";
98 }
99
100 static absl::string_view me_get_local_address(grpc_endpoint* /*ep*/) {
101 return "fake:mock_endpoint";
102 }
103
104 static int me_get_fd(grpc_endpoint* /*ep*/) { return -1; }
105
106 static bool me_can_track_err(grpc_endpoint* /*ep*/) { return false; }
107
108 static const grpc_endpoint_vtable vtable = {me_read,
109 me_write,
110 me_add_to_pollset,
111 me_add_to_pollset_set,
112 me_delete_from_pollset_set,
113 me_shutdown,
114 me_destroy,
115 me_get_peer,
116 me_get_local_address,
117 me_get_fd,
118 me_can_track_err};
119
120 grpc_endpoint* grpc_mock_endpoint_create(void (*on_write)(grpc_slice slice)) {
121 mock_endpoint* m = static_cast<mock_endpoint*>(gpr_malloc(sizeof(*m)));
122 m->base.vtable = &vtable;
123 grpc_slice_buffer_init(&m->read_buffer);
124 gpr_mu_init(&m->mu);
125 m->on_write = on_write;
126 m->on_read = nullptr;
127 return &m->base;
128 }
129
130 void grpc_mock_endpoint_put_read(grpc_endpoint* ep, grpc_slice slice) {
131 mock_endpoint* m = reinterpret_cast<mock_endpoint*>(ep);
132 gpr_mu_lock(&m->mu);
133 if (m->on_read != nullptr) {
134 grpc_slice_buffer_add(m->on_read_out, slice);
135 grpc_core::ExecCtx::Run(DEBUG_LOCATION, m->on_read, absl::OkStatus());
136 m->on_read = nullptr;
137 } else {
138 grpc_slice_buffer_add(&m->read_buffer, slice);
139 }
140 gpr_mu_unlock(&m->mu);
141 }