mirror of https://github.com/grpc/grpc.git
commit
1f22298ac9
25 changed files with 914 additions and 122 deletions
@ -0,0 +1,70 @@ |
||||
// Copyright 2023 gRPC Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#ifndef GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H |
||||
#define GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "absl/strings/string_view.h" |
||||
|
||||
#include <grpc/event_engine/event_engine.h> |
||||
|
||||
namespace grpc_event_engine { |
||||
namespace experimental { |
||||
|
||||
namespace endpoint_detail { |
||||
|
||||
template <typename Querying, typename... Es> |
||||
struct QueryExtensionRecursion; |
||||
|
||||
template <typename Querying, typename E, typename... Es> |
||||
struct QueryExtensionRecursion<Querying, E, Es...> { |
||||
static void* Query(absl::string_view id, Querying* p) { |
||||
if (id == E::EndpointExtensionName()) return static_cast<E*>(p); |
||||
return QueryExtensionRecursion<Querying, Es...>::Query(id, p); |
||||
} |
||||
}; |
||||
|
||||
template <typename Querying> |
||||
struct QueryExtensionRecursion<Querying> { |
||||
static void* Query(absl::string_view, Querying*) { return nullptr; } |
||||
}; |
||||
|
||||
} // namespace endpoint_detail
|
||||
|
||||
// A helper class to derive from some set of base classes and export
|
||||
// QueryExtension for them all.
|
||||
// Endpoint implementations which need to support different extensions just need
|
||||
// to derive from ExtendedEndpoint class.
|
||||
template <typename... Exports> |
||||
class ExtendedEndpoint : public EventEngine::Endpoint, public Exports... { |
||||
public: |
||||
void* QueryExtension(absl::string_view id) override { |
||||
return endpoint_detail::QueryExtensionRecursion<ExtendedEndpoint, |
||||
Exports...>::Query(id, |
||||
this); |
||||
} |
||||
}; |
||||
|
||||
/// A helper method which returns a valid pointer if the extension is supported
|
||||
/// by the endpoint.
|
||||
template <typename T> |
||||
T* QueryExtension(EventEngine::Endpoint* endpoint) { |
||||
return static_cast<T*>(endpoint->QueryExtension(T::EndpointExtensionName())); |
||||
} |
||||
|
||||
} // namespace experimental
|
||||
} // namespace grpc_event_engine
|
||||
|
||||
#endif // GRPC_SRC_CORE_LIB_EVENT_ENGINE_QUERY_EXTENSIONS_H
|
@ -0,0 +1,11 @@ |
||||
test_id: 3 |
||||
event_engine_actions { |
||||
run_delay: 9851624184873214 |
||||
run_delay: 1 |
||||
run_delay: 1 |
||||
run_delay: 0 |
||||
run_delay: 53876069761024 |
||||
} |
||||
config_vars { |
||||
experiments: 280384054960896 |
||||
} |
@ -0,0 +1,95 @@ |
||||
// Copyright 2023 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/event_engine/query_extensions.h" |
||||
|
||||
#include <string> |
||||
|
||||
#include "absl/functional/any_invocable.h" |
||||
#include "absl/status/status.h" |
||||
#include "gtest/gtest.h" |
||||
|
||||
#include <grpc/event_engine/event_engine.h> |
||||
#include <grpc/event_engine/slice_buffer.h> |
||||
|
||||
#include "src/core/lib/gprpp/crash.h" |
||||
|
||||
namespace grpc_event_engine { |
||||
namespace experimental { |
||||
namespace { |
||||
|
||||
template <int i> |
||||
class TestExtension { |
||||
public: |
||||
TestExtension() = default; |
||||
~TestExtension() = default; |
||||
|
||||
static std::string EndpointExtensionName() { |
||||
return "grpc.test.test_extension" + std::to_string(i); |
||||
} |
||||
|
||||
int GetValue() const { return val_; } |
||||
|
||||
private: |
||||
int val_ = i; |
||||
}; |
||||
|
||||
class ExtendedTestEndpoint |
||||
: public ExtendedEndpoint<TestExtension<0>, TestExtension<1>, |
||||
TestExtension<2>> { |
||||
public: |
||||
ExtendedTestEndpoint() = default; |
||||
~ExtendedTestEndpoint() override = default; |
||||
bool Read(absl::AnyInvocable<void(absl::Status)> /*on_read*/, |
||||
SliceBuffer* /*buffer*/, const ReadArgs* /*args*/) override { |
||||
grpc_core::Crash("Not implemented"); |
||||
}; |
||||
bool Write(absl::AnyInvocable<void(absl::Status)> /*on_writable*/, |
||||
SliceBuffer* /*data*/, const WriteArgs* /*args*/) override { |
||||
grpc_core::Crash("Not implemented"); |
||||
} |
||||
/// Returns an address in the format described in DNSResolver. The returned
|
||||
/// values are expected to remain valid for the life of the Endpoint.
|
||||
const EventEngine::ResolvedAddress& GetPeerAddress() const override { |
||||
grpc_core::Crash("Not implemented"); |
||||
} |
||||
const EventEngine::ResolvedAddress& GetLocalAddress() const override { |
||||
grpc_core::Crash("Not implemented"); |
||||
}; |
||||
}; |
||||
|
||||
TEST(QueryExtensionsTest, EndpointSupportsMultipleExtensions) { |
||||
ExtendedTestEndpoint endpoint; |
||||
TestExtension<0>* extension_0 = QueryExtension<TestExtension<0>>(&endpoint); |
||||
TestExtension<1>* extension_1 = QueryExtension<TestExtension<1>>(&endpoint); |
||||
TestExtension<2>* extension_2 = QueryExtension<TestExtension<2>>(&endpoint); |
||||
|
||||
EXPECT_NE(extension_0, nullptr); |
||||
EXPECT_NE(extension_1, nullptr); |
||||
EXPECT_NE(extension_2, nullptr); |
||||
|
||||
EXPECT_EQ(extension_0->GetValue(), 0); |
||||
EXPECT_EQ(extension_1->GetValue(), 1); |
||||
EXPECT_EQ(extension_2->GetValue(), 2); |
||||
} |
||||
} // namespace
|
||||
|
||||
} // namespace experimental
|
||||
} // namespace grpc_event_engine
|
||||
|
||||
int main(int argc, char** argv) { |
||||
testing::InitGoogleTest(&argc, argv); |
||||
return RUN_ALL_TESTS(); |
||||
} |
@ -1,23 +0,0 @@ |
||||
#! /bin/bash |
||||
# Copyright 2021 The gRPC Authors |
||||
# |
||||
# Licensed under the Apache License, Version 2.0 (the "License"); |
||||
# you may not use this file except in compliance with the License. |
||||
# You may obtain a copy of the License at |
||||
# |
||||
# http://www.apache.org/licenses/LICENSE-2.0 |
||||
# |
||||
# Unless required by applicable law or agreed to in writing, software |
||||
# distributed under the License is distributed on an "AS IS" BASIS, |
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
# See the License for the specific language governing permissions and |
||||
# limitations under the License. |
||||
# |
||||
# Checks if any file contains "DO NOT SUBMIT" |
||||
|
||||
cd "$(dirname "$0")/../../.." || exit 1 |
||||
git grep -Irn 'DO NOT SUBMIT' -- \ |
||||
'./*' \ |
||||
':!*check_do_not_submit.sh' \ |
||||
':!third_party/' |
||||
test $? -eq 1 || exit 1 |
Loading…
Reference in new issue