Skip to content

Commit cfaa191

Browse files
committed
RUBY-891 Tests for Single topology with RS member and mongos
1 parent 4515a82 commit cfaa191

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

lib/mongo/cluster/topology.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Topology
4040
# @example Get the initial cluster topology.
4141
# Topology.initial(topology: :replica_set)
4242
#
43+
# @param [ Array<String> ] seeds The addresses of the configured servers.
4344
# @param [ Hash ] options The cluster options.
4445
#
4546
# @return [ ReplicaSet, Sharded, Single ] The topology.

spec/mongo/cluster/topology_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@
6060
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
6161
end
6262
end
63+
64+
context 'when provided a single mongos', if: single_mongos? do
65+
66+
let(:topology) do
67+
described_class.initial(ADDRESSES, {})
68+
end
69+
70+
it 'returns a single topology' do
71+
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
72+
end
73+
end
74+
75+
context 'when provided a single replica set member', if: single_rs_member? do
76+
77+
let(:topology) do
78+
described_class.initial(ADDRESSES, {})
79+
end
80+
81+
it 'returns a single topology' do
82+
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
83+
end
84+
end
6385
end
6486
end
6587
end

spec/mongo/cluster_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,28 @@
126126
expect(cluster.scan!).to be true
127127
end
128128
end
129+
130+
describe '#servers' do
131+
132+
context 'when topology is single', if: single_seed? do
133+
134+
let(:cluster) do
135+
described_class.new(ADDRESSES)
136+
end
137+
138+
context 'when the server is a mongos', if: single_mongos? do
139+
140+
it 'returns the mongos' do
141+
expect(cluster.servers.size).to eq(1)
142+
end
143+
end
144+
145+
context 'when the server is a replica set member', if: single_rs_member? do
146+
147+
it 'returns the replica set member' do
148+
expect(cluster.servers.size).to eq(1)
149+
end
150+
end
151+
end
152+
end
129153
end

spec/spec_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ def sharded?
9999
$sharded ||= $mongo_client.cluster.sharded?
100100
end
101101

102+
# Determine whether the single address provided is a replica set member.
103+
#
104+
# @since 2.0.0
105+
def single_rs_member?
106+
$mongo_client ||= initialize_scanned_client!
107+
single_seed? && $mongo_client.cluster.servers.first.replica_set_name
108+
end
109+
110+
# Determine whether the single address provided is a mongos.
111+
#
112+
# @since 2.0.0
113+
def single_mongos?
114+
$mongo_client ||= initialize_scanned_client!
115+
single_seed? && $mongo_client.cluster.servers.first.mongos?
116+
end
117+
118+
# Determine whether a single address was provided.
119+
#
120+
# @since 2.0.0
121+
def single_seed?
122+
ADDRESSES.size == 1
123+
end
124+
102125
# For instances where behaviour is different on different versions, we need to
103126
# determine in the specs if we are 2.6 or higher.
104127
#

0 commit comments

Comments
 (0)