|
| 1 | +// Copyright 2018 Google LLC |
| 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 | + |
| 15 | +#ifndef GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_ |
| 16 | +#define GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_ |
| 17 | + |
| 18 | +#include "bigtable/client/table.h" |
| 19 | +#include "bigtable/client/table_admin.h" |
| 20 | +#include "bigtable/client/testing/inprocess_admin_client.h" |
| 21 | +#include "bigtable/client/testing/inprocess_data_client.h" |
| 22 | +#include "google/bigtable/v2/bigtable.grpc.pb.h" |
| 23 | +#include <gtest/gtest.h> |
| 24 | + |
| 25 | +namespace bigtable { |
| 26 | +namespace testing { |
| 27 | + |
| 28 | +using ReceivedMetadata = std::multimap<std::string, std::string>; |
| 29 | + |
| 30 | +inline void GetClientMetadata(grpc::ServerContext* context, |
| 31 | + ReceivedMetadata& client_metadata) { |
| 32 | + for (auto const& kv : context->client_metadata()) { |
| 33 | + auto ele = std::make_pair(std::string(kv.first.begin(), kv.first.end()), |
| 34 | + std::string(kv.second.begin(), kv.second.end())); |
| 35 | + client_metadata.emplace(std::move(ele)); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Implement the portions of the `google.bigtable.v2.Bigtable` interface |
| 41 | + * necessary for the embedded server tests. |
| 42 | + * |
| 43 | + * This is not a Mock (use `google::bigtable::v2::MockBigtableStub` for that, |
| 44 | + * nor is this a Fake implementation (use the Cloud Bigtable Emulator for that), |
| 45 | + * this is an implementation of the interface that returns hardcoded values. |
| 46 | + * It is suitable for the embedded server tests, but for nothing else. |
| 47 | + */ |
| 48 | +class BigtableImpl final : public google::bigtable::v2::Bigtable::Service { |
| 49 | + public: |
| 50 | + BigtableImpl() {} |
| 51 | + grpc::Status ReadRows( |
| 52 | + grpc::ServerContext* context, |
| 53 | + google::bigtable::v2::ReadRowsRequest const* request, |
| 54 | + grpc::ServerWriter<google::bigtable::v2::ReadRowsResponse>* writer) { |
| 55 | + GetClientMetadata(context, client_metadata_); |
| 56 | + return grpc::Status::OK; |
| 57 | + } |
| 58 | + const ReceivedMetadata& client_metadata() const { return client_metadata_; } |
| 59 | + |
| 60 | + private: |
| 61 | + ReceivedMetadata client_metadata_; |
| 62 | +}; |
| 63 | + |
| 64 | +class TableAdminImpl final |
| 65 | + : public google::bigtable::admin::v2::BigtableTableAdmin::Service { |
| 66 | + public: |
| 67 | + TableAdminImpl() {} |
| 68 | + |
| 69 | + grpc::Status CreateTable( |
| 70 | + grpc::ServerContext* context, |
| 71 | + google::bigtable::admin::v2::CreateTableRequest const* request, |
| 72 | + google::bigtable::admin::v2::Table* response) override { |
| 73 | + GetClientMetadata(context, client_metadata_); |
| 74 | + return grpc::Status::OK; |
| 75 | + } |
| 76 | + grpc::Status GetTable( |
| 77 | + grpc::ServerContext* context, |
| 78 | + google::bigtable::admin::v2::GetTableRequest const* request, |
| 79 | + google::bigtable::admin::v2::Table* response) override { |
| 80 | + GetClientMetadata(context, client_metadata_); |
| 81 | + return grpc::Status::OK; |
| 82 | + } |
| 83 | + const ReceivedMetadata& client_metadata() const { return client_metadata_; } |
| 84 | + |
| 85 | + private: |
| 86 | + ReceivedMetadata client_metadata_; |
| 87 | +}; |
| 88 | + |
| 89 | +/// Common fixture for integrating embedded server into tests. |
| 90 | +class EmbeddedServerTestFixture : public ::testing::Test { |
| 91 | + protected: |
| 92 | + void StartServer(); |
| 93 | + void SetUp(); |
| 94 | + void TearDown(); |
| 95 | + void ResetChannel(); |
| 96 | + |
| 97 | + std::string const kProjectId = "foo-project"; |
| 98 | + std::string const kInstanceId = "bar-instance"; |
| 99 | + std::string const kTableId = "baz-table"; |
| 100 | + |
| 101 | + // These are hardcoded, and not computed, because we want to test the |
| 102 | + // computation. |
| 103 | + std::string const kInstanceName = |
| 104 | + "projects/foo-project/instances/bar-instance"; |
| 105 | + std::string const kTableName = |
| 106 | + "projects/foo-project/instances/bar-instance/tables/baz-table"; |
| 107 | + |
| 108 | + std::string project_id_ = kProjectId; |
| 109 | + std::string instance_id_ = kInstanceId; |
| 110 | + std::shared_ptr<DataClient> data_client_; |
| 111 | + std::shared_ptr<AdminClient> admin_client_; |
| 112 | + std::shared_ptr<bigtable::Table> table_; |
| 113 | + std::shared_ptr<bigtable::TableAdmin> admin_; |
| 114 | + std::thread wait_thread_; |
| 115 | + BigtableImpl bigtable_service_; |
| 116 | + TableAdminImpl admin_service_; |
| 117 | + grpc::ServerBuilder builder_; |
| 118 | + std::unique_ptr<grpc::Server> server_; |
| 119 | +}; |
| 120 | + |
| 121 | +} // namespace testing |
| 122 | +} // namespace bigtable |
| 123 | + |
| 124 | +#endif // GOOGLE_CLOUD_CPP_BIGTABLE_CLIENT_TESTING_EMBEDDED_SERVER_TEST_FIXTURE_H_ |
0 commit comments