Skip to content

Commit

Permalink
refactor grpc: remove old client methods
Browse files Browse the repository at this point in the history
commit_hash:336a799859e8e6b81cf580818cbf716cf78912dc
  • Loading branch information
Anton3 committed Dec 26, 2024
1 parent 88ff58d commit c70b32e
Show file tree
Hide file tree
Showing 30 changed files with 158 additions and 142 deletions.
2 changes: 1 addition & 1 deletion grpc/benchmarks/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void UnaryRPCPayload(sample::ugrpc::UnitTestServiceClient& client) {
sample::ugrpc::GreetingRequest out;
out.set_name("userver");
sample::ugrpc::GreetingResponse in;
in = client.SyncSayHello(out, PrepareClientContext());
in = client.SayHello(out, PrepareClientContext());
UINVARIANT("Hello " + out.name() == in.name(), "Behavior broken");
}

Expand Down
2 changes: 1 addition & 1 deletion grpc/functional_tests/basic_chaos/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ std::string GreeterClient::SayHello(std::string name, bool is_small_timeout) {
api::GreetingRequest request;
request.set_name(std::move(name));

api::GreetingResponse response = client_.SyncSayHello(request, CreateClientContext(is_small_timeout));
api::GreetingResponse response = client_.SayHello(request, CreateClientContext(is_small_timeout));

return std::move(*response.mutable_greeting());
}
Expand Down
2 changes: 1 addition & 1 deletion grpc/functional_tests/metrics/src/greeter_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ inline std::string GreeterClient::SayHello(std::string name) {

auto context = std::make_unique<grpc::ClientContext>();

samples::api::GreetingResponse response = client_.SyncSayHello(request, std::move(context));
samples::api::GreetingResponse response = client_.SayHello(request, std::move(context));

return std::move(*response.mutable_greeting());
}
Expand Down
1 change: 1 addition & 0 deletions grpc/include/userver/ugrpc/client/generic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class GenericClient final {
const GenericOptions& options = {}
) const;

/// Initiate a `single request -> single response` RPC with the given name.
grpc::ByteBuffer UnaryCall(
std::string_view call_name,
const grpc::ByteBuffer& request,
Expand Down
20 changes: 11 additions & 9 deletions grpc/include/userver/ugrpc/client/response_future.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ namespace ugrpc::client {
template <typename Response>
class [[nodiscard]] ResponseFuture final {
public:
/// @cond
// For internal use only
explicit ResponseFuture(UnaryCall<Response>&& call);
/// @endcond

/// @brief Checks if the asynchronous call has completed
/// Note, that once user gets result, IsReady should not be called
/// @return true if result ready
Expand All @@ -42,19 +37,26 @@ class [[nodiscard]] ResponseFuture final {
CallAnyBase& GetCall();

/// @cond
// For internal use only
template <typename PrepareFunc, typename Request>
ResponseFuture(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req);

// For internal use only.
engine::impl::ContextAccessor* TryGetContextAccessor() noexcept;
/// @endcond

private:
UnaryCall<Response> call_;
impl::UnaryCall<Response> call_;
std::unique_ptr<Response> response_;
UnaryFuture future_;
impl::UnaryFuture future_;
};

template <typename Response>
ResponseFuture<Response>::ResponseFuture(UnaryCall<Response>&& call)
: call_{std::move(call)}, response_{std::make_unique<Response>()}, future_{call_.FinishAsync(*response_)} {}
template <typename PrepareFunc, typename Request>
ResponseFuture<Response>::ResponseFuture(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req)
: call_(std::move(params), prepare_func, req),
response_{std::make_unique<Response>()},
future_{call_.FinishAsync(*response_)} {}

template <typename Response>
bool ResponseFuture<Response>::IsReady() const noexcept {
Expand Down
12 changes: 10 additions & 2 deletions grpc/include/userver/ugrpc/client/rpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ struct MiddlewarePipeline {
static void PostFinish(impl::RpcData& data, const grpc::Status& status);
};

} // namespace impl

/// @brief UnaryFuture for waiting a single response RPC
class [[nodiscard]] UnaryFuture {
public:
Expand Down Expand Up @@ -95,6 +93,8 @@ class [[nodiscard]] UnaryFuture {
mutable std::exception_ptr exception_;
};

} // namespace impl

/// @brief StreamReadFuture for waiting a single read response from stream
template <typename RPC>
class [[nodiscard]] StreamReadFuture {
Expand Down Expand Up @@ -166,6 +166,8 @@ class CallAnyBase {
std::unique_ptr<impl::RpcData> data_;
};

namespace impl {

/// @brief Controls a single request -> single response RPC
///
/// This class is not thread-safe except for `GetContext`.
Expand Down Expand Up @@ -212,6 +214,8 @@ class [[nodiscard]] UnaryCall final : public CallAnyBase {
impl::RawResponseReader<Response> reader_;
};

} // namespace impl

/// @brief Controls a single request -> response stream RPC
///
/// This class is not thread-safe except for `GetContext`.
Expand Down Expand Up @@ -497,6 +501,8 @@ bool StreamReadFuture<RPC>::IsReady() const noexcept {
return method.IsReady();
}

namespace impl {

template <typename Response>
template <typename PrepareFunc, typename Request>
UnaryCall<Response>::UnaryCall(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req)
Expand Down Expand Up @@ -539,6 +545,8 @@ UnaryFuture UnaryCall<Response>::FinishAsync(Response& response) {
return UnaryFuture{GetData(), post_finish};
}

} // namespace impl

template <typename Response>
template <typename PrepareFunc, typename Request>
InputStream<Response>::InputStream(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req)
Expand Down
3 changes: 1 addition & 2 deletions grpc/src/ugrpc/client/generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ client::ResponseFuture<grpc::ByteBuffer> GenericClient::AsyncUnaryCall(
) const {
auto& stub = impl_.NextGenericStub<GenericStubService>();
auto grpcpp_call_name = utils::StrCat<grpc::string>("/", call_name);
client::UnaryCall<grpc::ByteBuffer> call{
return {
impl::CreateGenericCallParams(
impl_, call_name, std::move(context), generic_options.qos, generic_options.metrics_call_name
),
Expand All @@ -44,7 +44,6 @@ client::ResponseFuture<grpc::ByteBuffer> GenericClient::AsyncUnaryCall(
) { return stub.PrepareUnaryCall(context, grpcpp_call_name, request, cq); },
request,
};
return client::ResponseFuture<grpc::ByteBuffer>{std::move(call)};
}

grpc::ByteBuffer GenericClient::UnaryCall(
Expand Down
4 changes: 2 additions & 2 deletions grpc/src/ugrpc/client/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ void MiddlewarePipeline::PostFinish(impl::RpcData& data, const grpc::Status& sta
}
}

} // namespace impl

UnaryFuture::UnaryFuture(
impl::RpcData& data,
std::function<void(impl::RpcData& data, const grpc::Status& status)> post_finish
Expand Down Expand Up @@ -177,6 +175,8 @@ void UnaryFuture::ProcessFinish() const {
data_->ResetSpan();
}

} // namespace impl

grpc::ClientContext& CallAnyBase::GetContext() { return data_->GetContext(); }

impl::RpcData& CallAnyBase::GetData() {
Expand Down
16 changes: 8 additions & 8 deletions grpc/tests/baggage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ UTEST_F(GrpcServerTestBaggage, TestGrpcBaggage) {
context->AddMetadata(ugrpc::impl::kXBaggage, ugrpc::impl::ToGrpcString(baggage));

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(out, std::move(context)));
ASSERT_EQ(in.name(), baggage);
}

Expand All @@ -87,7 +87,7 @@ UTEST_F(GrpcServerTestBaggage, TestGrpcBaggageMultiply) {
context->AddMetadata(ugrpc::impl::kXBaggage, ugrpc::impl::ToGrpcString(baggage));

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(out, std::move(context)));
ASSERT_EQ(in.name(), baggage);
}

Expand All @@ -97,7 +97,7 @@ UTEST_F(GrpcServerTestBaggage, TestGrpcBaggageNoBaggage) {
auto context = std::make_unique<grpc::ClientContext>();

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(out, std::move(context)));
ASSERT_EQ(in.name(), "null");
}

Expand All @@ -109,7 +109,7 @@ UTEST_F(GrpcServerTestBaggage, TestGrpcBaggageWrongKey) {
context->AddMetadata(ugrpc::impl::kXBaggage, "wrong_key=wrong_value");

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(out, std::move(context)));
ASSERT_EQ(in.name(), "");
}

Expand Down Expand Up @@ -166,7 +166,7 @@ UTEST_F(GrpcClientTestBaggage, TestGrpcClientBaggage) {
auto context = std::make_unique<grpc::ClientContext>();

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(request, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(request, std::move(context)));
ASSERT_EQ(in.name(), baggage);
}

Expand All @@ -181,7 +181,7 @@ UTEST_F(GrpcClientTestBaggage, TestGrpcClientBaggageMultiply) {
auto context = std::make_unique<grpc::ClientContext>();

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(request, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(request, std::move(context)));
ASSERT_EQ(in.name(), baggage);
}

Expand All @@ -193,7 +193,7 @@ UTEST_F(GrpcClientTestBaggage, TestGrpcClientNoBaggage) {
auto context = std::make_unique<grpc::ClientContext>();

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(request, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(request, std::move(context)));
ASSERT_EQ(in.name(), "null");
}

Expand All @@ -207,7 +207,7 @@ UTEST_F(GrpcClientTestBaggage, TestGrpcClientWrongKey) {
auto context = std::make_unique<grpc::ClientContext>();

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(request, std::move(context)));
UEXPECT_NO_THROW(in = client.SayHello(request, std::move(context)));
ASSERT_EQ(in.name(), "");
}

Expand Down
4 changes: 2 additions & 2 deletions grpc/tests/base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ UTEST_F(GrpcClientTest, UnaryRPC) {
sample::ugrpc::GreetingRequest out;
out.set_name("userver");
sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out, PrepareClientContext()));
UEXPECT_NO_THROW(in = client.SayHello(out, PrepareClientContext()));
EXPECT_EQ("Hello " + out.name(), in.name());
}

Expand Down Expand Up @@ -167,7 +167,7 @@ UTEST_F(GrpcClientTest, UnaryRPCDefaultContext) {
out.set_name("default_context");

sample::ugrpc::GreetingResponse in;
UEXPECT_NO_THROW(in = client.SyncSayHello(out));
UEXPECT_NO_THROW(in = client.SayHello(out));
EXPECT_EQ("Hello " + out.name(), in.name());
}

Expand Down
8 changes: 4 additions & 4 deletions grpc/tests/cancel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ UTEST(GrpcServer, DeadlineAffectsWaitForReady) {
context->set_wait_for_ready(true);

auto long_deadline = engine::Deadline::FromDuration(100ms + 1s);
UEXPECT_THROW(client.SyncSayHello({}, std::move(context)), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(client.SayHello({}, std::move(context)), ugrpc::client::DeadlineExceededError);
EXPECT_FALSE(long_deadline.IsReached());
}

Expand Down Expand Up @@ -278,7 +278,7 @@ UTEST_F_MT(GrpcCancelByClient, CancelByClient, 3) {
auto context = std::make_unique<grpc::ClientContext>();
context->set_deadline(engine::Deadline::FromDuration(100ms));
context->set_wait_for_ready(true);
UEXPECT_THROW(client.SyncSayHello({}, std::move(context)), ugrpc::client::BaseError);
UEXPECT_THROW(client.SayHello({}, std::move(context)), ugrpc::client::BaseError);

ASSERT_TRUE(GetService().GetFinishEvent().WaitForEventFor(std::chrono::seconds{5}));
}
Expand All @@ -288,7 +288,7 @@ UTEST_F_MT(GrpcCancelByClient, CancelByClientNoReadyWait, 3) {

auto context = std::make_unique<grpc::ClientContext>();
context->set_deadline(engine::Deadline::FromDuration(100ms));
UEXPECT_THROW(client.SyncSayHello({}, std::move(context)), ugrpc::client::BaseError);
UEXPECT_THROW(client.SayHello({}, std::move(context)), ugrpc::client::BaseError);

ASSERT_TRUE(GetService().GetFinishEvent().WaitForEventFor(std::chrono::seconds{5}));
}
Expand All @@ -312,7 +312,7 @@ UTEST_F(GrpcCancelSleep, CancelByTimeoutLogging) {
auto client = MakeClient<sample::ugrpc::UnitTestServiceClient>();

UEXPECT_THROW(
client.SyncSayHello(
client.SayHello(
{}, std::make_unique<::grpc::ClientContext>(), ugrpc::client::Qos{std::chrono::milliseconds(100)}
),
ugrpc::client::DeadlineExceededError
Expand Down
2 changes: 1 addition & 1 deletion grpc/tests/client_cancel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ UTEST_F(GrpcClientCancel, UnaryCall) {

sample::ugrpc::GreetingRequest out;
out.set_name("userver");
UEXPECT_THROW(client.SyncSayHello(out, PrepareClientContext()), ugrpc::client::RpcCancelledError);
UEXPECT_THROW(client.SayHello(out, PrepareClientContext()), ugrpc::client::RpcCancelledError);
}

const auto stats =
Expand Down
20 changes: 9 additions & 11 deletions grpc/tests/client_qos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ UTEST_F(GrpcClientQosConfigOk, Ok) {
ExtendDynamicConfig(MakeQosConfig(utest::kMaxTestWaitTime));

sample::ugrpc::GreetingResponse response;
UEXPECT_NO_THROW(response = client.SyncSayHello(kRequest));
UEXPECT_NO_THROW(response = client.SayHello(kRequest));
EXPECT_EQ(response.name(), "Hello " + kRequest.name());
}

Expand All @@ -111,7 +111,7 @@ UTEST_F(GrpcClientQosConfigOk, ContextDeadlineOverrides) {
context->set_deadline(engine::Deadline::FromDuration(utest::kMaxTestWaitTime));

sample::ugrpc::GreetingResponse response;
UEXPECT_NO_THROW(response = client.SyncSayHello(kRequest, std::move(context)));
UEXPECT_NO_THROW(response = client.SayHello(kRequest, std::move(context)));
EXPECT_EQ(response.name(), "Hello " + kRequest.name());
}

Expand All @@ -126,7 +126,7 @@ UTEST_F(GrpcClientQosConfigOk, UserQosOverridesEverything) {
qos.timeout = tests::kLongTimeout;

sample::ugrpc::GreetingResponse response;
UEXPECT_NO_THROW(response = client.SyncSayHello(kRequest, std::move(context), qos));
UEXPECT_NO_THROW(response = client.SayHello(kRequest, std::move(context), qos));
EXPECT_EQ(response.name(), "Hello " + kRequest.name());
}

Expand All @@ -136,7 +136,7 @@ UTEST_F(GrpcClientQosConfigExceeded, DefaultTimeout) {
ExtendDynamicConfig(MakeQosConfig(tests::kShortTimeout));

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(response = client.SyncSayHello(kRequest), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(response = client.SayHello(kRequest), ugrpc::client::DeadlineExceededError);
}

UTEST_F(GrpcClientQosConfigExceeded, PerRpcTimeout) {
Expand All @@ -145,7 +145,7 @@ UTEST_F(GrpcClientQosConfigExceeded, PerRpcTimeout) {
ExtendDynamicConfig(MakePerRpcQosConfig(tests::kShortTimeout));

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(response = client.SyncSayHello(kRequest), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(response = client.SayHello(kRequest), ugrpc::client::DeadlineExceededError);
}

UTEST_F(GrpcClientQosConfigExceeded, DeadlinePropagationWorks) {
Expand All @@ -155,7 +155,7 @@ UTEST_F(GrpcClientQosConfigExceeded, DeadlinePropagationWorks) {
tests::InitTaskInheritedDeadline(engine::Deadline::FromDuration(tests::kShortTimeout));

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(response = client.SyncSayHello(kRequest), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(response = client.SayHello(kRequest), ugrpc::client::DeadlineExceededError);
}

UTEST_F(GrpcClientQosConfigExceeded, ContextDeadlineOverrides) {
Expand All @@ -167,7 +167,7 @@ UTEST_F(GrpcClientQosConfigExceeded, ContextDeadlineOverrides) {
context->set_deadline(engine::Deadline::FromDuration(tests::kShortTimeout));

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(response = client.SyncSayHello(kRequest, std::move(context)), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(response = client.SayHello(kRequest, std::move(context)), ugrpc::client::DeadlineExceededError);
}

UTEST_F(GrpcClientQosConfigExceeded, UserQosOverridesEverything) {
Expand All @@ -182,9 +182,7 @@ UTEST_F(GrpcClientQosConfigExceeded, UserQosOverridesEverything) {
qos.timeout = tests::kShortTimeout;

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(
response = client.SyncSayHello(kRequest, std::move(context), qos), ugrpc::client::DeadlineExceededError
);
UEXPECT_THROW(response = client.SayHello(kRequest, std::move(context), qos), ugrpc::client::DeadlineExceededError);
}

UTEST_F(GrpcClientQosConfigExceeded, EmptyConfigMeansInfinity) {
Expand All @@ -195,7 +193,7 @@ UTEST_F(GrpcClientQosConfigExceeded, EmptyConfigMeansInfinity) {
tests::InitTaskInheritedDeadline(engine::Deadline::FromDuration(tests::kShortTimeout));

sample::ugrpc::GreetingResponse response;
UEXPECT_THROW(response = client.SyncSayHello(kRequest), ugrpc::client::DeadlineExceededError);
UEXPECT_THROW(response = client.SayHello(kRequest), ugrpc::client::DeadlineExceededError);
}

USERVER_NAMESPACE_END
2 changes: 1 addition & 1 deletion grpc/tests/deadline_metrics_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class DeadlineStatsTests : public ugrpc::tests::ServiceFixture<UnitTestDeadlineS

auto client_context = tests::MakeClientContext(set_deadline);
try {
response = client.SyncSayHello(request, std::move(client_context));
response = client.SayHello(request, std::move(client_context));
EXPECT_EQ(response.name(), "Hello abacaba");
return true;
} catch (const ugrpc::client::DeadlineExceededError& /*exception*/) {
Expand Down
Loading

0 comments on commit c70b32e

Please sign in to comment.