forked from googleapis/google-cloud-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_connection_test.cc
More file actions
107 lines (94 loc) · 4.05 KB
/
data_connection_test.cc
File metadata and controls
107 lines (94 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2023 Google LLC
//
// 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
//
// https://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 "google/cloud/bigtable/data_connection.h"
#include "google/cloud/bigtable/internal/bigtable_stub_factory.h"
#include "google/cloud/bigtable/options.h"
#include "google/cloud/common_options.h"
#include "google/cloud/credentials.h"
#include "google/cloud/grpc_options.h"
#include "google/cloud/testing_util/opentelemetry_matchers.h"
#include "rpc_retry_policy.h"
#include <gmock/gmock.h>
#include <chrono>
namespace google {
namespace cloud {
namespace bigtable {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
namespace {
using ms = std::chrono::milliseconds;
using ::google::cloud::testing_util::DisableTracing;
using ::google::cloud::testing_util::EnableTracing;
using ::google::cloud::testing_util::SpanNamed;
using ::testing::Contains;
using ::testing::Eq;
using ::testing::IsEmpty;
Options TestOptions() {
return Options{}
// Stay on this machine. Don't make a connection to the actual service.
.set<EndpointOption>("localhost:1")
.set<UnifiedCredentialsOption>(MakeInsecureCredentials())
// Disable channel refreshing, which is not under test.
.set<MaxConnectionRefreshOption>(ms(0))
// Create the minimum number of stubs.
.set<GrpcNumChannelsOption>(1)
// Disable retries
.set<DataRetryPolicyOption>(
std::make_shared<DataLimitedErrorCountRetryPolicy>(0));
}
TEST(MakeDataConnection, DefaultsOptions) {
auto conn = MakeDataConnection(
TestOptions().set<AppProfileIdOption>("user-supplied"));
auto options = conn->options();
EXPECT_TRUE(options.has<DataBackoffPolicyOption>())
<< "Options are not defaulted in MakeDataConnection()";
EXPECT_EQ(options.get<AppProfileIdOption>(), "user-supplied")
<< "User supplied Options are overridden in MakeDataConnection()";
}
TEST(MakeDataConnection, TracingEnabled) {
auto span_catcher = testing_util::InstallSpanCatcher();
auto conn = MakeDataConnection(EnableTracing(TestOptions()));
google::cloud::internal::OptionsSpan o(conn->options());
(void)conn->Apply("table-name", SingleRowMutation("row"));
EXPECT_THAT(span_catcher->GetSpans(),
Contains(SpanNamed("bigtable::Table::Apply")));
}
TEST(MakeDataConnection, InstanceChannelAffinityOption) {
InstanceResource instance_a{Project("my-project"), "instance-a"};
InstanceResource instance_b{Project("my-project"), "instance-b"};
auto conn =
MakeDataConnection(TestOptions()
.set<AppProfileIdOption>("user-supplied")
.set<experimental::InstanceChannelAffinityOption>(
{instance_a, instance_b}));
auto options = conn->options();
EXPECT_TRUE(options.has<DataBackoffPolicyOption>())
<< "Options are not defaulted in MakeDataConnection()";
EXPECT_EQ(options.get<AppProfileIdOption>(), "user-supplied")
<< "User supplied Options are overridden in MakeDataConnection()";
ASSERT_TRUE(options.has<experimental::InstanceChannelAffinityOption>());
EXPECT_THAT(options.get<experimental::InstanceChannelAffinityOption>().size(),
Eq(2));
}
TEST(MakeDataConnection, TracingDisabled) {
auto span_catcher = testing_util::InstallSpanCatcher();
auto conn = MakeDataConnection(DisableTracing(TestOptions()));
google::cloud::internal::OptionsSpan o(conn->options());
(void)conn->Apply("table-name", SingleRowMutation("row"));
EXPECT_THAT(span_catcher->GetSpans(), IsEmpty());
}
} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace bigtable
} // namespace cloud
} // namespace google