Skip to content

Commit ffc3704

Browse files
committed
more cr feedback
1 parent f57decb commit ffc3704

File tree

2 files changed

+110
-42
lines changed

2 files changed

+110
-42
lines changed

lib/mongo/uri.rb

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -426,12 +426,12 @@ def self.uri_option(uri_key, name, extra = {})
426426
uri_option 'replicaset', :replica_set, :type => :replica_set
427427

428428
# Timeout Options
429-
uri_option 'connecttimeoutms', :connect_timeout, :type => :ms_convert
430-
uri_option 'sockettimeoutms', :socket_timeout, :type => :ms_convert
431-
uri_option 'serverselectiontimeoutms', :server_selection_timeout, :type => :ms_convert
432-
uri_option 'localthresholdms', :local_threshold, :type => :ms_convert
433-
uri_option 'heartbeatfrequencyms', :heartbeat_frequency, :type => :ms_convert
434-
uri_option 'maxidletimems', :max_idle_time, :type => :ms_convert
429+
uri_option 'connecttimeoutms', :connect_timeout, :type => :connect_timeout
430+
uri_option 'sockettimeoutms', :socket_timeout, :type => :socket_timeout
431+
uri_option 'serverselectiontimeoutms', :server_selection_timeout, :type => :server_selection_timeout
432+
uri_option 'localthresholdms', :local_threshold, :type => :local_threshold
433+
uri_option 'heartbeatfrequencyms', :heartbeat_frequency, :type => :heartbeat_frequency
434+
uri_option 'maxidletimems', :max_idle_time, :type => :max_idle_time
435435

436436
# Write Options
437437
uri_option 'w', :w, :group => :write
@@ -447,7 +447,7 @@ def self.uri_option(uri_key, name, extra = {})
447447
# Pool options
448448
uri_option 'minpoolsize', :min_pool_size
449449
uri_option 'maxpoolsize', :max_pool_size
450-
uri_option 'waitqueuetimeoutms', :wait_queue_timeout, :type => :ms_convert
450+
uri_option 'waitqueuetimeoutms', :wait_queue_timeout, :type => :wait_queue_timeout
451451

452452
# Security Options
453453
uri_option 'ssl', :ssl
@@ -611,7 +611,7 @@ def read_tags(value)
611611
#
612612
# @return [Hash] The tag set hash.
613613
def read_set(value)
614-
hash_extractor(value)
614+
hash_extractor('readPreferenceTags', value)
615615
end
616616

617617
# Auth mechanism properties extractor.
@@ -620,7 +620,7 @@ def read_set(value)
620620
#
621621
# @return [ Hash ] The auth mechanism properties hash.
622622
def auth_mech_props(value)
623-
properties = hash_extractor(value)
623+
properties = hash_extractor('authMechanismProperties', value)
624624
if properties[:canonicalize_host_name]
625625
properties.merge!(canonicalize_host_name:
626626
properties[:canonicalize_host_name] == 'true')
@@ -652,7 +652,7 @@ def zlib_compression_level(value)
652652
# @param value [ String ] The journal value.
653653
#
654654
# @return [ true | false | nil ] The journal value parsed out, otherwise nil (and a warning
655-
# will be raised).
655+
# will be logged).
656656
def journal(value)
657657
bool('journal', value)
658658
end
@@ -663,7 +663,7 @@ def journal(value)
663663
# @param value [ String ] The tlsAllowInvalidCertificates value.
664664
#
665665
# @return [ true | false | nil ] The ssl_verify value parsed out, otherwise nil (and a warning
666-
# will be raised).
666+
# will be logged).
667667
def ssl_verify(value)
668668
b = bool('tlsAllowInvalidCertificates', value)
669669

@@ -679,7 +679,7 @@ def ssl_verify(value)
679679
# @param value [ String ] The retryWrites value.
680680
#
681681
# @return [ true | false | nil ] The boolean value parsed out, otherwise nil (and a warning
682-
# will be raised).
682+
# will be logged).
683683
def retry_writes(value)
684684
bool('retryWrites', value)
685685
end
@@ -689,7 +689,7 @@ def retry_writes(value)
689689
# @param value [ String ] The URI option value.
690690
#
691691
# @return [ true | false | nil ] The boolean value parsed out, otherwise nil (and a warning
692-
# will be raised).
692+
# will be logged).
693693
def bool(name, value)
694694
case value
695695
when "true"
@@ -723,17 +723,87 @@ def max_staleness(value)
723723
nil
724724
end
725725

726-
# Parses an unsigned integer value.
726+
# Parses the connectTimeoutMS value.
727727
#
728-
# @param value [ String ] The URI option value.
728+
# @param value [ String ] The connectTimeoutMS value.
729+
#
730+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
731+
# logged).
732+
def connect_timeout(value)
733+
ms_convert('connectTimeoutMS', value)
734+
end
735+
736+
# Parses the localThresholdMS value.
737+
#
738+
# @param value [ String ] The localThresholdMS value.
729739
#
730740
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
731-
# raised).
741+
# logged).
742+
def local_threshold(value)
743+
ms_convert('localThresholdMS', value)
744+
end
745+
746+
# Parses the heartbeatFrequencyMS value.
747+
#
748+
# @param value [ String ] The heartbeatFrequencyMS value.
749+
#
750+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
751+
# logged).
752+
def heartbeat_frequency(value)
753+
ms_convert('heartbeatFrequencyMS', value)
754+
end
755+
756+
# Parses the maxIdleTimeMS value.
757+
#
758+
# @param value [ String ] The maxIdleTimeMS value.
759+
#
760+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
761+
# logged).
762+
def max_idle_time(value)
763+
ms_convert('maxIdleTimeMS', value)
764+
end
765+
766+
# Parses the serverSelectionMS value.
767+
#
768+
# @param value [ String ] The serverSelectionMS value.
769+
#
770+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
771+
# logged).
772+
def server_selection_timeout(value)
773+
ms_convert('serverSelectionTimeoutMS', value)
774+
end
775+
776+
# Parses the socketTimeoutMS value.
777+
#
778+
# @param value [ String ] The socketTimeoutMS value.
779+
#
780+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
781+
# logged).
782+
def socket_timeout(value)
783+
ms_convert('socketTimeoutMS', value)
784+
end
785+
786+
# Parses the waitQueueTimeoutMS value.
787+
#
788+
# @param value [ String ] The waitQueueTimeoutMS value.
789+
#
790+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
791+
# logged).
792+
def wait_queue_timeout(value)
793+
ms_convert('MS', value)
794+
end
795+
796+
# Parses the wtimeoutMS value.
797+
#
798+
# @param value [ String ] The wtimeoutMS value.
799+
#
800+
# @return [ Integer | nil ] The integer parsed out, otherwise nil (and a warning will be
801+
# logged).
732802
def wtimeout(value)
733-
unless /\A\d+\z/ =~ value
803+
unless /\A\d+\z/ =~ value
734804
log_warn("Invalid wtimeoutMS value: #{value}")
735805
return nil
736-
end
806+
end
737807

738808
value.to_i
739809
end
@@ -747,9 +817,9 @@ def wtimeout(value)
747817
# @return [ Float ] The seconds value.
748818
#
749819
# @since 2.0.0
750-
def ms_convert(value)
820+
def ms_convert(name, value)
751821
unless /\A-?\d+(\.\d+)?\z/ =~ value
752-
log_warn("Invalid ms value: #{value}")
822+
log_warn("Invalid ms value for #{name}: #{value}")
753823
return nil
754824
end
755825

@@ -761,11 +831,11 @@ def ms_convert(value)
761831
# @param value [ String ] The string to build a hash from.
762832
#
763833
# @return [ Hash ] The hash built from the string.
764-
def hash_extractor(value)
834+
def hash_extractor(name, value)
765835
value.split(',').reduce({}) do |set, tag|
766836
k, v = tag.split(':')
767837
if v.nil?
768-
log_warn("#{value} is not a valid URI hash")
838+
log_warn("Invalid hash value for #{name}: #{value}")
769839
return nil
770840
end
771841

spec/spec_tests/uri_options_spec.rb

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,21 @@ class Address
4141
spec.tests.each do |test|
4242

4343
# Skip optional tests for options the driver doesn't support.
44-
if test.description.start_with?('tlsAllowInvalidHostnames') || test.description.start_with?('tlsInsecure')
45-
next
46-
end
44+
next if test.description.start_with?('tlsAllowInvalidHostnames', 'tlsInsecure')
4745

4846
context "#{test.description}" do
4947

48+
context 'when the uri should warn', if: test.warn? do
49+
50+
before do
51+
expect(Mongo::Logger.logger).to receive(:warn)
52+
end
53+
54+
it 'warns' do
55+
expect(test.client).to be_a(Mongo::Client)
56+
end
57+
end
58+
5059
context 'when the uri is invalid', unless: test.valid? do
5160

5261
it 'raises an error' do
@@ -69,27 +78,16 @@ class Address
6978
it 'creates a client with the correct options' do
7079
expect(test.client).to match_options(test)
7180
end
81+
end
7282

73-
context 'when the uri should not warn', if: !test.warn? do
74-
75-
before do
76-
expect(Mongo::Logger.logger).not_to receive(:warn)
77-
end
83+
context 'when the uri should not warn', if: !test.warn? do
7884

79-
it 'does not raise an exception or warning' do
80-
expect(test.uri).to be_a(Mongo::URI)
81-
end
85+
before do
86+
expect(Mongo::Logger.logger).not_to receive(:warn)
8287
end
8388

84-
context 'when the uri should warn', if: test.warn? do
85-
86-
before do
87-
expect(Mongo::Logger.logger).to receive(:warn)
88-
end
89-
90-
it 'warns' do
91-
expect(test.client).to be_a(Mongo::Client)
92-
end
89+
it 'does not raise an exception or warning' do
90+
expect(test.client).to be_a(Mongo::Client)
9391
end
9492
end
9593
end

0 commit comments

Comments
 (0)