Skip to content

Commit 4680478

Browse files
authored
refactor: renamed option to GrpcBackgroundThreadsFactoryOption (#5942)
This PR also adds a internal::DefaultBackgroundThreadsFactory() function. This function will be convenient to use as a default value when our factories inspect an option like: auto f = opts.get_or<GrpcBackgroundThreadsFactoryOption>(internal::DefaultBackgroundThreadsFactory);
1 parent 7730b65 commit 4680478

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

google/cloud/connection_options.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ Options MakeOptions(ConnectionOptions<ConnectionTraits> old) {
263263
opts.set<GrpcNumChannelsOption>(old.num_channels_);
264264
opts.set<UserAgentPrefixOption>({std::move(old.user_agent_prefix_)});
265265
opts.set<GrpcTracingOptionsOption>(std::move(old.tracing_options_));
266-
opts.set<GrpcBackgroundThreadsOption>(old.background_threads_factory());
266+
opts.set<GrpcBackgroundThreadsFactoryOption>(
267+
old.background_threads_factory());
267268
if (!old.tracing_components_.empty()) {
268269
opts.set<TracingComponentsOption>(std::move(old.tracing_components_));
269270
}

google/cloud/internal/grpc_options.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "google/cloud/internal/grpc_options.h"
1616
#include "google/cloud/internal/absl_str_join_quiet.h"
17+
#include "google/cloud/internal/background_threads_impl.h"
1718
#include "google/cloud/internal/common_options.h"
1819

1920
namespace google {
@@ -34,6 +35,10 @@ grpc::ChannelArguments MakeChannelArguments(Options const& opts) {
3435
return channel_arguments;
3536
}
3637

38+
std::unique_ptr<BackgroundThreads> DefaultBackgroundThreadsFactory() {
39+
return absl::make_unique<AutomaticallyCreatedBackgroundThreads>();
40+
}
41+
3742
} // namespace internal
3843

3944
} // namespace GOOGLE_CLOUD_CPP_NS

google/cloud/internal/grpc_options.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct GrpcTracingOptionsOption {
8181
*/
8282
using BackgroundThreadsFactory =
8383
std::function<std::unique_ptr<BackgroundThreads>()>;
84-
struct GrpcBackgroundThreadsOption {
84+
struct GrpcBackgroundThreadsFactoryOption {
8585
using Type = BackgroundThreadsFactory;
8686
};
8787

@@ -92,6 +92,9 @@ namespace internal {
9292
/// Creates a new `grpc::ChannelArguments` configured with @p opts.
9393
grpc::ChannelArguments MakeChannelArguments(Options const& opts);
9494

95+
/// Returns a factory to use if `GrpcBackgroundThreadsFactoryOption` is unset.
96+
std::unique_ptr<BackgroundThreads> DefaultBackgroundThreadsFactory();
97+
9598
/**
9699
* A list of all the options in this file.
97100
*
@@ -108,7 +111,7 @@ grpc::ChannelArguments MakeChannelArguments(Options const& opts);
108111
using GrpcOptions =
109112
std::tuple<GrpcCredentialOption, GrpcNumChannelsOption,
110113
GrpcChannelArgumentsOption, GrpcTracingOptionsOption,
111-
GrpcBackgroundThreadsOption>;
114+
GrpcBackgroundThreadsFactoryOption>;
112115

113116
} // namespace internal
114117

google/cloud/internal/grpc_options_test.cc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
#include "google/cloud/internal/grpc_options.h"
16+
#include "google/cloud/internal/background_threads_impl.h"
1617
#include "google/cloud/testing_util/scoped_log.h"
1718
#include "absl/memory/memory.h"
1819
#include <gmock/gmock.h>
@@ -46,7 +47,7 @@ TEST(GrpcOptions, RegularOptions) {
4647
TestGrpcOption<GrpcTracingOptionsOption>(TracingOptions{});
4748
}
4849

49-
TEST(GrpcOptions, GrpcBackgroundThreadsOption) {
50+
TEST(GrpcOptions, GrpcBackgroundThreadsFactoryOption) {
5051
struct Fake : BackgroundThreads {
5152
CompletionQueue cq() const override { return {}; }
5253
};
@@ -55,12 +56,20 @@ TEST(GrpcOptions, GrpcBackgroundThreadsOption) {
5556
invoked = true;
5657
return absl::make_unique<Fake>();
5758
};
58-
auto opts = Options{}.set<GrpcBackgroundThreadsOption>(factory);
59+
auto opts = Options{}.set<GrpcBackgroundThreadsFactoryOption>(factory);
5960
EXPECT_FALSE(invoked);
60-
opts.get_or<GrpcBackgroundThreadsOption>({})();
61+
opts.get_or<GrpcBackgroundThreadsFactoryOption>({})();
6162
EXPECT_TRUE(invoked);
6263
}
6364

65+
TEST(GrpcOptions, DefaultBackgroundThreadsFactory) {
66+
auto f = DefaultBackgroundThreadsFactory();
67+
auto* tp =
68+
dynamic_cast<internal::AutomaticallyCreatedBackgroundThreads*>(f.get());
69+
ASSERT_NE(nullptr, tp);
70+
EXPECT_EQ(1, tp->pool_size());
71+
}
72+
6473
TEST(GrpcOptions, Expected) {
6574
testing_util::ScopedLog log;
6675
Options opts;

0 commit comments

Comments
 (0)