Skip to content

Commit 1939f37

Browse files
p-mongop
authored andcommitted
RUBY-1747 Keep closed clusters working in 2.x (#1284)
1 parent eb61ecb commit 1939f37

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

lib/mongo/server_selector/selectable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ def select_server(cluster, ping = nil)
115115
msg = "Cluster has no addresses, and therefore will never have a server"
116116
raise Error::NoServerAvailable.new(self, cluster, msg)
117117
end
118+
=begin Add this check in version 3.0.0
118119
unless cluster.connected?
119120
msg = 'Cluster is disconnected'
120121
raise Error::NoServerAvailable.new(self, cluster, msg)
121122
end
123+
=end
122124
@local_threshold = cluster.options[:local_threshold] || LOCAL_THRESHOLD
123125
@server_selection_timeout = cluster.options[:server_selection_timeout] || SERVER_SELECTION_TIMEOUT
124126
deadline = Time.now + server_selection_timeout
@@ -168,6 +170,9 @@ def select_server(cluster, ping = nil)
168170
if dead_monitors.any?
169171
msg += ". The following servers have dead monitor threads: #{dead_monitors.map(&:summary).join(', ')}"
170172
end
173+
unless cluster.connected?
174+
msg += ". The cluster is disconnected (client may have been closed)"
175+
end
171176
raise Error::NoServerAvailable.new(self, cluster, msg)
172177
end
173178

spec/integration/server_selector_spec.rb

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,31 @@
3232
end
3333

3434
context 'client is closed' do
35-
before do
36-
client.close
35+
context 'there is a known primary' do
36+
before do
37+
client.cluster.next_primary
38+
client.close
39+
expect(client.cluster.connected?).to be false
40+
end
41+
42+
it 'returns the primary for BC reasons' do
43+
expect(result).to be_a(Mongo::Server)
44+
end
3745
end
3846

39-
it 'raises NoServerAvailable with a message explaining the situation' do
40-
expect do
41-
result
42-
end.to raise_error(Mongo::Error::NoServerAvailable, "Cluster is disconnected")
47+
context 'there is no known primary' do
48+
before do
49+
primary_server = client.cluster.next_primary
50+
client.close
51+
expect(client.cluster.connected?).to be false
52+
primary_server.unknown!
53+
end
54+
55+
it 'raises NoServerAvailable with a message explaining the situation' do
56+
expect do
57+
result
58+
end.to raise_error(Mongo::Error::NoServerAvailable, /The cluster is disconnected \(client may have been closed\)/)
59+
end
4360
end
4461
end
4562

0 commit comments

Comments
 (0)