• 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 #include "src/core/lib/event_engine/query_extensions.h"
15 
16 #include <grpc/event_engine/event_engine.h>
17 #include <grpc/event_engine/slice_buffer.h>
18 #include <grpc/support/port_platform.h>
19 
20 #include <string>
21 
22 #include "absl/functional/any_invocable.h"
23 #include "absl/status/status.h"
24 #include "gtest/gtest.h"
25 #include "src/core/util/crash.h"
26 
27 namespace grpc_event_engine {
28 namespace experimental {
29 namespace {
30 
31 template <int i>
32 class TestExtension {
33  public:
34   TestExtension() = default;
35   ~TestExtension() = default;
36 
EndpointExtensionName()37   static std::string EndpointExtensionName() {
38     return "grpc.test.test_extension" + std::to_string(i);
39   }
40 
GetValue() const41   int GetValue() const { return val_; }
42 
43  private:
44   int val_ = i;
45 };
46 
47 class ExtendedTestEndpoint
48     : public ExtendedType<EventEngine::Endpoint, TestExtension<0>,
49                           TestExtension<1>, TestExtension<2>> {
50  public:
51   ExtendedTestEndpoint() = default;
52   ~ExtendedTestEndpoint() override = default;
Read(absl::AnyInvocable<void (absl::Status)>,SliceBuffer *,const ReadArgs *)53   bool Read(absl::AnyInvocable<void(absl::Status)> /*on_read*/,
54             SliceBuffer* /*buffer*/, const ReadArgs* /*args*/) override {
55     grpc_core::Crash("Not implemented");
56   };
Write(absl::AnyInvocable<void (absl::Status)>,SliceBuffer *,const WriteArgs *)57   bool Write(absl::AnyInvocable<void(absl::Status)> /*on_writable*/,
58              SliceBuffer* /*data*/, const WriteArgs* /*args*/) override {
59     grpc_core::Crash("Not implemented");
60   }
61   /// Returns an address in the format described in DNSResolver. The returned
62   /// values are expected to remain valid for the life of the Endpoint.
GetPeerAddress() const63   const EventEngine::ResolvedAddress& GetPeerAddress() const override {
64     grpc_core::Crash("Not implemented");
65   }
GetLocalAddress() const66   const EventEngine::ResolvedAddress& GetLocalAddress() const override {
67     grpc_core::Crash("Not implemented");
68   };
69 };
70 
TEST(QueryExtensionsTest,EndpointSupportsMultipleExtensions)71 TEST(QueryExtensionsTest, EndpointSupportsMultipleExtensions) {
72   ExtendedTestEndpoint endpoint;
73   TestExtension<0>* extension_0 = QueryExtension<TestExtension<0>>(&endpoint);
74   TestExtension<1>* extension_1 = QueryExtension<TestExtension<1>>(&endpoint);
75   TestExtension<2>* extension_2 = QueryExtension<TestExtension<2>>(&endpoint);
76 
77   EXPECT_NE(extension_0, nullptr);
78   EXPECT_NE(extension_1, nullptr);
79   EXPECT_NE(extension_2, nullptr);
80 
81   EXPECT_EQ(extension_0->GetValue(), 0);
82   EXPECT_EQ(extension_1->GetValue(), 1);
83   EXPECT_EQ(extension_2->GetValue(), 2);
84 }
85 }  // namespace
86 
87 }  // namespace experimental
88 }  // namespace grpc_event_engine
89 
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91   testing::InitGoogleTest(&argc, argv);
92   return RUN_ALL_TESTS();
93 }
94