Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/mongo/server/description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,7 @@ class Description
# call took to complete.
#
# @since 2.0.0
def initialize(address, config = {}, average_round_trip_time = 0)
if average_round_trip_time.nil?
raise ArgumentError, 'Average round trip time cannot be nil'
end
def initialize(address, config = {}, average_round_trip_time = nil)
@address = address
@config = config
unless unknown?
Expand Down
10 changes: 8 additions & 2 deletions lib/mongo/server/round_trip_time_averager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ def measure
rv = yield
rescue Exception => exc
end
@last_round_trip_time = Time.now - start
last_round_trip_time = Time.now - start

update_average_round_trip_time
# If ismaster fails, we need to return the last round trip time
# because it is used in the heartbeat failed SDAM event,
# but we must not update the round trip time recorded in the server.
unless exc
@last_round_trip_time = last_round_trip_time
update_average_round_trip_time
end

[rv, exc, last_round_trip_time, average_round_trip_time]
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mongo/server/description_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,8 @@
described_class.new(address, { 'secondary' => false }, 4.5)
end

it 'defaults to 0' do
expect(described_class.new(address).average_round_trip_time).to eq(0)
it 'defaults to nil' do
expect(described_class.new(address).average_round_trip_time).to be nil
end

it 'can be set via the constructor' do
Expand Down
19 changes: 19 additions & 0 deletions spec/mongo/server/round_trip_time_averager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,23 @@
end
end
end

describe '#measure' do
context 'block does not raise' do
it 'updates average rtt' do
expect(averager).to receive(:update_average_round_trip_time)
averager.measure do
end
end
end

context 'block raises' do
it 'does not update average rtt' do
expect(averager).not_to receive(:update_average_round_trip_time)
averager.measure do
raise "Problem"
end
end
end
end
end