Skip to content

Commit 3ec2911

Browse files
authored
RCBC-388: Add Configuration Profiles (#55)
1 parent e283299 commit 3ec2911

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

lib/couchbase/config_profiles.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Copyright 2020-2022 Couchbase, Inc.
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+
module Couchbase
16+
module ConfigProfiles
17+
class Profiles
18+
attr :profiles
19+
20+
def initialize
21+
@profiles = {}
22+
register_profile("wan_development", DevelopmentProfile.new)
23+
end
24+
25+
def register_profile(name, profile)
26+
@profiles[name] = profile
27+
end
28+
29+
def apply(profile_name, options)
30+
raise ArgumentError, "#{profile_name} is not a registered profile" unless @profiles.key?(profile_name)
31+
32+
@profiles[profile_name].apply(options)
33+
end
34+
end
35+
36+
class Profile
37+
def apply(options); end
38+
end
39+
40+
class DevelopmentProfile < Profile
41+
def apply(options)
42+
options.key_value_timeout = 20_000
43+
# TODO: Add `options.key_value_durable_timeout = 20_000` when key_value_durable_timeout is added to Options::Cluster
44+
options.connect_timeout = 20_000
45+
options.view_timeout = 120_000
46+
options.query_timeout = 120_000
47+
options.analytics_timeout = 120_000
48+
options.search_timeout = 120_000
49+
options.management_timeout = 120_000
50+
end
51+
end
52+
53+
KNOWN_PROFILES = Profiles.new
54+
end
55+
end

lib/couchbase/options.rb

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

1515
require "couchbase/utils/time"
16+
require "couchbase/config_profiles"
1617

1718
module Couchbase
1819
# Definition of the Option classes for data APIs
@@ -1461,6 +1462,11 @@ def authenticate(username, password)
14611462
@authenticator = PasswordAuthenticator.new(username, password)
14621463
end
14631464

1465+
# @param [String] profile_name The name of the configuration profile to apply (e.g. "wan_development")
1466+
def apply_profile(profile_name)
1467+
ConfigProfiles::KNOWN_PROFILES.apply(profile_name, self)
1468+
end
1469+
14641470
# @api private
14651471
def to_backend
14661472
{

test/config_profiles_test.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2020-2022 Couchbase, Inc.
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+
require_relative "test_helper"
16+
17+
module Couchbase
18+
class ConfigProfilesTest < Minitest::Test
19+
include TestUtilities
20+
21+
class CustomProfile < Couchbase::ConfigProfiles::Profile
22+
def apply(options)
23+
options.key_value_timeout = 15000
24+
options.connect_timeout = 17000
25+
end
26+
end
27+
28+
def setup
29+
# Do nothing
30+
end
31+
32+
def teardown
33+
# Do nothing
34+
end
35+
36+
def test_apply_development_config_profile
37+
options = Couchbase::Options::Cluster.new
38+
options.apply_profile("wan_development")
39+
assert_equal 20000, options.key_value_timeout
40+
assert_equal 20000, options.connect_timeout
41+
assert_equal 120000, options.view_timeout
42+
assert_equal 120000, options.query_timeout
43+
assert_equal 120000, options.analytics_timeout
44+
assert_equal 120000, options.search_timeout
45+
assert_equal 120000, options.management_timeout
46+
end
47+
48+
def test_apply_custom_config_profile
49+
Couchbase::ConfigProfiles::KNOWN_PROFILES.register_profile("custom_profile", CustomProfile.new)
50+
options = Couchbase::Options::Cluster.new
51+
options.apply_profile("custom_profile")
52+
assert_equal 15000, options.key_value_timeout
53+
assert_equal 17000, options.connect_timeout
54+
end
55+
end
56+
end

0 commit comments

Comments
 (0)