Skip to content

Commit

Permalink
feat grpc: switch tests to simplified grpc service handlers interface
Browse files Browse the repository at this point in the history
commit_hash:7cd1f9f93c3032efeaff6eeaae3fda991ea1a45a
  • Loading branch information
kpavlov00 committed Nov 12, 2024
1 parent 35a0548 commit 3e0d999
Show file tree
Hide file tree
Showing 22 changed files with 197 additions and 176 deletions.
8 changes: 5 additions & 3 deletions grpc/tests/async_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,18 @@ constexpr int kNumber = 42;

class AsyncTestServiceWithError final : public sample::ugrpc::UnitTestServiceBase {
public:
void Chat(ChatCall& call) override { call.FinishWithError({grpc::StatusCode::INTERNAL, "message", "details"}); }
ChatResult Chat(CallContext& /*context*/, ChatReaderWriter& /*stream*/) override {
return grpc::Status{grpc::StatusCode::INTERNAL, "message", "details"};
}
};

class AsyncTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void Chat(ChatCall& call) override {
ChatResult Chat(CallContext& /*context*/, ChatReaderWriter& /*stream*/) override {
sample::ugrpc::StreamGreetingResponse response;
response.set_number(kNumber);
response.set_name("Hello");
call.WriteAndFinish(response);
return response;
}
};

Expand Down
11 changes: 6 additions & 5 deletions grpc/tests/baggage_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace {

class ServerBaggageTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&&) override {
SayHelloResult SayHello(CallContext& /*context*/, sample::ugrpc::GreetingRequest&& /*request*/) override {
sample::ugrpc::GreetingResponse response;
const auto* bg = baggage::BaggageManager::TryGetBaggage();

Expand All @@ -39,7 +39,7 @@ class ServerBaggageTestService final : public sample::ugrpc::UnitTestServiceBase
response.set_name("null");
}

call.Finish(response);
return response;
}
};

Expand Down Expand Up @@ -122,17 +122,18 @@ namespace {

class ClientBaggageTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&&) override {
SayHelloResult SayHello(CallContext& context, sample::ugrpc::GreetingRequest&& /*request*/) override {
sample::ugrpc::GreetingResponse response;
const auto* baggage_header = utils::FindOrNullptr(call.GetContext().client_metadata(), ugrpc::impl::kXBaggage);
const auto* baggage_header =
utils::FindOrNullptr(context.GetServerContext().client_metadata(), ugrpc::impl::kXBaggage);

if (baggage_header) {
response.set_name(ugrpc::impl::ToString(*baggage_header));
} else {
response.set_name("null");
}

call.Finish(response);
return response;
}
};

Expand Down
49 changes: 26 additions & 23 deletions grpc/tests/base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ void CheckServerContext(grpc::ServerContext& context) {

class UnitTestLongAnswerService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&& request) override {
SayHelloResult SayHello(CallContext& context, sample::ugrpc::GreetingRequest&& request) override {
if (request.name() != "default_context") {
CheckServerContext(call.GetContext());
CheckServerContext(context.GetServerContext());
}
sample::ugrpc::GreetingResponse response;
response.set_name("Hello " + request.name());

engine::SleepUntil(engine::Deadline::FromDuration(answer_duration_));

call.Finish(response);
return response;
}

void SetAnswerDuration(std::chrono::milliseconds duration) { answer_duration_ = duration; };
Expand All @@ -52,51 +52,52 @@ class UnitTestLongAnswerService final : public sample::ugrpc::UnitTestServiceBas

class UnitTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&& request) override {
SayHelloResult SayHello(CallContext& context, sample::ugrpc::GreetingRequest&& request) override {
if (request.name() != "default_context") {
CheckServerContext(call.GetContext());
CheckServerContext(context.GetServerContext());
}
sample::ugrpc::GreetingResponse response;
response.set_name("Hello " + request.name());
call.Finish(response);
return response;
}

void ReadMany(ReadManyCall& call, sample::ugrpc::StreamGreetingRequest&& request) override {
CheckServerContext(call.GetContext());
ReadManyResult
ReadMany(CallContext& context, sample::ugrpc::StreamGreetingRequest&& request, ReadManyWriter& writer) override {
CheckServerContext(context.GetServerContext());
sample::ugrpc::StreamGreetingResponse response;
response.set_name("Hello again " + request.name());
for (int i = 0; i < request.number(); ++i) {
response.set_number(i);
call.Write(response);
writer.Write(response);
}
call.Finish();
return grpc::Status::OK;
}

void WriteMany(WriteManyCall& call) override {
CheckServerContext(call.GetContext());
WriteManyResult WriteMany(CallContext& context, WriteManyReader& reader) override {
CheckServerContext(context.GetServerContext());
sample::ugrpc::StreamGreetingRequest request;
int count = 0;
while (call.Read(request)) {
while (reader.Read(request)) {
++count;
}
sample::ugrpc::StreamGreetingResponse response;
response.set_name("Hello");
response.set_number(count);
call.Finish(response);
return response;
}

void Chat(ChatCall& call) override {
CheckServerContext(call.GetContext());
ChatResult Chat(CallContext& context, ChatReaderWriter& stream) override {
CheckServerContext(context.GetServerContext());
sample::ugrpc::StreamGreetingRequest request;
sample::ugrpc::StreamGreetingResponse response;
int count = 0;
while (call.Read(request)) {
while (stream.Read(request)) {
++count;
response.set_number(count);
response.set_name("Hello " + request.name());
call.Write(response);
stream.Write(response);
}
call.Finish();
return grpc::Status::OK;
}
};

Expand Down Expand Up @@ -360,18 +361,20 @@ namespace {

class WriteAndFinishService final : public sample::ugrpc::UnitTestServiceBase {
public:
void ReadMany(ReadManyCall& call, sample::ugrpc::StreamGreetingRequest&& request) override {
ReadManyResult
ReadMany(CallContext& /*context*/, sample::ugrpc::StreamGreetingRequest&& request, ReadManyWriter& /*writer*/)
override {
sample::ugrpc::StreamGreetingResponse response;
response.set_number(kNumber);
response.set_name("Hello " + request.name());
call.WriteAndFinish(response);
return response;
}

void Chat(ChatCall& call) override {
ChatResult Chat(CallContext& /*context*/, ChatReaderWriter& /*stream*/) override {
sample::ugrpc::StreamGreetingResponse response;
response.set_number(kNumber);
response.set_name("Hello");
call.WriteAndFinish(response);
return response;
}
};

Expand Down
28 changes: 16 additions & 12 deletions grpc/tests/client_cancel_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,49 @@ namespace {

class UnitTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&& request) override {
SayHelloResult SayHello(CallContext& /*context*/, sample::ugrpc::GreetingRequest&& request) override {
sample::ugrpc::GreetingResponse response;
response.set_name("Hello " + request.name());
call.Finish(response);
return response;
}

void ReadMany(ReadManyCall& call, sample::ugrpc::StreamGreetingRequest&& request) override {
ReadManyResult ReadMany(
CallContext& /*context*/,
sample::ugrpc::StreamGreetingRequest&& request,
ReadManyWriter& writer
) override {
sample::ugrpc::StreamGreetingResponse response;
response.set_name("Hello again " + request.name());
for (int i = 0; i < request.number(); ++i) {
response.set_number(i);
call.Write(response);
writer.Write(response);
}
call.Finish();
return grpc::Status::OK;
}

void WriteMany(WriteManyCall& call) override {
WriteManyResult WriteMany(CallContext& /*context*/, WriteManyReader& reader) override {
sample::ugrpc::StreamGreetingRequest request;
int count = 0;
while (call.Read(request)) {
while (reader.Read(request)) {
++count;
}
sample::ugrpc::StreamGreetingResponse response;
response.set_name("Hello");
response.set_number(count);
call.Finish(response);
return response;
}

void Chat(ChatCall& call) override {
ChatResult Chat(CallContext& /*context*/, ChatReaderWriter& stream) override {
sample::ugrpc::StreamGreetingRequest request;
sample::ugrpc::StreamGreetingResponse response;
int count = 0;
while (call.Read(request)) {
while (stream.Read(request)) {
++count;
response.set_number(count);
response.set_name("Hello " + request.name());
call.Write(response);
stream.Write(response);
}
call.Finish();
return grpc::Status::OK;
}
};

Expand Down
4 changes: 2 additions & 2 deletions grpc/tests/client_qos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ std::vector<dynamic_config::KeyValue> MakePerRpcQosConfig(std::chrono::milliseco

class UnitTestOkService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&& request) override {
SayHelloResult SayHello(CallContext& /*context*/, sample::ugrpc::GreetingRequest&& request) override {
engine::InterruptibleSleepFor(tests::kShortTimeout);

sample::ugrpc::GreetingResponse response;
response.set_name("Hello " + request.name());
call.Finish(response);
return response;
}
};

Expand Down
4 changes: 2 additions & 2 deletions grpc/tests/congestion_control_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace {

class UnitTestService final : public sample::ugrpc::UnitTestServiceBase {
public:
void SayHello(SayHelloCall& call, sample::ugrpc::GreetingRequest&& request) override {
SayHelloResult SayHello(CallContext& /*context*/, sample::ugrpc::GreetingRequest&& request) override {
sample::ugrpc::GreetingResponse response;
response.set_name("Hello " + request.name());
call.Finish(response);
return response;
}
};

Expand Down
Loading

0 comments on commit 3e0d999

Please sign in to comment.