From 10b17f373990630df4682250a8bc37fb207cb302 Mon Sep 17 00:00:00 2001 From: Esun Kim Date: Thu, 25 Mar 2021 14:04:39 -0700 Subject: [PATCH] Added mock_stream_test (#25103) --- CMakeLists.txt | 49 +++++++++++++++++++++++ build_autogenerated.yaml | 13 ++++++ include/grpcpp/test/mock_stream.h | 7 ++++ test/cpp/test/BUILD | 17 ++++++++ test/cpp/test/mock_stream_test.cc | 60 ++++++++++++++++++++++++++++ tools/run_tests/generated/tests.json | 24 +++++++++++ 6 files changed, 170 insertions(+) create mode 100644 test/cpp/test/mock_stream_test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index f463115ee6f..c3460501ab6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -888,6 +888,7 @@ if(gRPC_BUILD_TESTS) add_dependencies(buildtests_cxx log_test) add_dependencies(buildtests_cxx matchers_test) add_dependencies(buildtests_cxx message_allocator_end2end_test) + add_dependencies(buildtests_cxx mock_stream_test) add_dependencies(buildtests_cxx mock_test) add_dependencies(buildtests_cxx nonblocking_test) add_dependencies(buildtests_cxx noop-benchmark) @@ -12087,6 +12088,54 @@ target_link_libraries(message_allocator_end2end_test ) +endif() +if(gRPC_BUILD_TESTS) + +add_executable(mock_stream_test + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo.grpc.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/echo_messages.grpc.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.grpc.pb.cc + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.pb.h + ${_gRPC_PROTO_GENS_DIR}/src/proto/grpc/testing/simple_messages.grpc.pb.h + test/cpp/test/mock_stream_test.cc + third_party/googletest/googletest/src/gtest-all.cc + third_party/googletest/googlemock/src/gmock-all.cc +) + +target_include_directories(mock_stream_test + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/include + ${_gRPC_ADDRESS_SORTING_INCLUDE_DIR} + ${_gRPC_RE2_INCLUDE_DIR} + ${_gRPC_SSL_INCLUDE_DIR} + ${_gRPC_UPB_GENERATED_DIR} + ${_gRPC_UPB_GRPC_GENERATED_DIR} + ${_gRPC_UPB_INCLUDE_DIR} + ${_gRPC_XXHASH_INCLUDE_DIR} + ${_gRPC_ZLIB_INCLUDE_DIR} + third_party/googletest/googletest/include + third_party/googletest/googletest + third_party/googletest/googlemock/include + third_party/googletest/googlemock + ${_gRPC_PROTO_GENS_DIR} +) + +target_link_libraries(mock_stream_test + ${_gRPC_PROTOBUF_LIBRARIES} + ${_gRPC_ALLTARGETS_LIBRARIES} + grpc++_test + grpc++_test_util +) + + endif() if(gRPC_BUILD_TESTS) diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index f1dfdaca646..a7089e79270 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -5638,6 +5638,19 @@ targets: - test/cpp/end2end/test_service_impl.cc deps: - grpc++_test_util +- name: mock_stream_test + gtest: true + build: test + language: c++ + headers: [] + src: + - src/proto/grpc/testing/echo.proto + - src/proto/grpc/testing/echo_messages.proto + - src/proto/grpc/testing/simple_messages.proto + - test/cpp/test/mock_stream_test.cc + deps: + - grpc++_test + - grpc++_test_util - name: mock_test gtest: true build: test diff --git a/include/grpcpp/test/mock_stream.h b/include/grpcpp/test/mock_stream.h index e33595d7096..deffad3d909 100644 --- a/include/grpcpp/test/mock_stream.h +++ b/include/grpcpp/test/mock_stream.h @@ -90,6 +90,8 @@ class MockClientAsyncResponseReader public: MockClientAsyncResponseReader() = default; + /// ClientAsyncResponseReaderInterface + MOCK_METHOD0_T(StartCall, void()); MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); MOCK_METHOD3_T(Finish, void(R*, Status*, void*)); }; @@ -100,6 +102,7 @@ class MockClientAsyncReader : public ClientAsyncReaderInterface { MockClientAsyncReader() = default; /// ClientAsyncStreamingInterface + MOCK_METHOD1_T(StartCall, void(void*)); MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); MOCK_METHOD2_T(Finish, void(Status*, void*)); @@ -113,11 +116,13 @@ class MockClientAsyncWriter : public ::grpc::ClientAsyncWriterInterface { MockClientAsyncWriter() = default; /// ClientAsyncStreamingInterface + MOCK_METHOD1_T(StartCall, void(void*)); MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); MOCK_METHOD2_T(Finish, void(Status*, void*)); /// AsyncWriterInterface MOCK_METHOD2_T(Write, void(const W&, void*)); + MOCK_METHOD3_T(Write, void(const W&, ::grpc::WriteOptions, void*)); /// ClientAsyncWriterInterface MOCK_METHOD1_T(WritesDone, void(void*)); @@ -130,11 +135,13 @@ class MockClientAsyncReaderWriter MockClientAsyncReaderWriter() = default; /// ClientAsyncStreamingInterface + MOCK_METHOD1_T(StartCall, void(void*)); MOCK_METHOD1_T(ReadInitialMetadata, void(void*)); MOCK_METHOD2_T(Finish, void(Status*, void*)); /// AsyncWriterInterface MOCK_METHOD2_T(Write, void(const W&, void*)); + MOCK_METHOD3_T(Write, void(const W&, ::grpc::WriteOptions, void*)); /// AsyncReaderInterface MOCK_METHOD2_T(Read, void(R*, void*)); diff --git a/test/cpp/test/BUILD b/test/cpp/test/BUILD index e970c6b752c..6c6b94b5c81 100644 --- a/test/cpp/test/BUILD +++ b/test/cpp/test/BUILD @@ -21,6 +21,23 @@ grpc_package( visibility = "public", ) +grpc_cc_test( + name = "mock_stream_test", + srcs = ["mock_stream_test.cc"], + external_deps = [ + "gtest", + ], + deps = [ + "//:gpr", + "//:grpc", + "//:grpc++", + "//:grpc++_test", + "//src/proto/grpc/testing:echo_proto", + "//test/core/util:grpc_test_util", + "//test/cpp/util:test_util", + ], +) + grpc_cc_test( name = "server_context_test_spouse_test", srcs = ["server_context_test_spouse_test.cc"], diff --git a/test/cpp/test/mock_stream_test.cc b/test/cpp/test/mock_stream_test.cc new file mode 100644 index 00000000000..6545762c75e --- /dev/null +++ b/test/cpp/test/mock_stream_test.cc @@ -0,0 +1,60 @@ +/* + * + * Copyright 2020 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. + * + */ + +#include +#include + +#include "absl/memory/memory.h" + +#include "src/proto/grpc/testing/echo.grpc.pb.h" + +using grpc::testing::EchoRequest; +using grpc::testing::EchoResponse; + +TEST(MockStreamTest, Basic) { + auto cr = absl::make_unique>(); + ASSERT_NE(cr, nullptr); + + auto cw = absl::make_unique>(); + ASSERT_NE(cw, nullptr); + + auto crw = absl::make_unique< + grpc::testing::MockClientReaderWriter>(); + ASSERT_NE(crw, nullptr); + + auto carr = absl::make_unique< + grpc::testing::MockClientAsyncResponseReader>(); + ASSERT_NE(carr, nullptr); + + auto car = + absl::make_unique>(); + ASSERT_NE(car, nullptr); + + auto caw = + absl::make_unique>(); + ASSERT_NE(caw, nullptr); + + auto carw = absl::make_unique< + grpc::testing::MockClientAsyncReaderWriter>(); + ASSERT_NE(carw, nullptr); +} + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/tools/run_tests/generated/tests.json b/tools/run_tests/generated/tests.json index d902312b13e..cae9ef0526b 100644 --- a/tools/run_tests/generated/tests.json +++ b/tools/run_tests/generated/tests.json @@ -5151,6 +5151,30 @@ ], "uses_polling": true }, + { + "args": [], + "benchmark": false, + "ci_platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "cpu_cost": 1.0, + "exclude_configs": [], + "exclude_iomgrs": [], + "flaky": false, + "gtest": true, + "language": "c++", + "name": "mock_stream_test", + "platforms": [ + "linux", + "mac", + "posix", + "windows" + ], + "uses_polling": true + }, { "args": [], "benchmark": false,