Skip to content

Commit 9de099e

Browse files
committed
Merge pull request #596 from estolfo/RUBY-891-standalone
RUBY-891 Rename Standalone topology to Single and add tests
2 parents 5d07aa3 + 47f9abb commit 9de099e

18 files changed

+136
-58
lines changed

lib/mongo/cluster.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Cluster
3434
# @return [ Object ] The cluster topology.
3535
attr_reader :topology
3636

37-
def_delegators :topology, :replica_set?, :replica_set_name, :sharded?, :standalone?, :unknown?
37+
def_delegators :topology, :replica_set?, :replica_set_name, :sharded?, :single?, :unknown?
3838

3939
# Determine if this cluster of servers is equal to another object. Checks the
4040
# servers currently in the cluster, not what was configured.

lib/mongo/cluster/topology.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
require 'mongo/cluster/topology/replica_set'
1616
require 'mongo/cluster/topology/sharded'
17-
require 'mongo/cluster/topology/standalone'
17+
require 'mongo/cluster/topology/single'
1818
require 'mongo/cluster/topology/unknown'
1919

2020
module Mongo
@@ -32,17 +32,18 @@ module Topology
3232
OPTIONS = {
3333
replica_set: ReplicaSet,
3434
sharded: Sharded,
35-
direct: Standalone
35+
direct: Single
3636
}
3737

3838
# Get the initial cluster topology for the provided options.
3939
#
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
#
45-
# @return [ ReplicaSet, Sharded, Standalone ] The topology.
46+
# @return [ ReplicaSet, Sharded, Single ] The topology.
4647
#
4748
# @since 2.0.0
4849
def initial(seeds, options)
@@ -52,7 +53,7 @@ def initial(seeds, options)
5253
if options.has_key?(:replica_set)
5354
ReplicaSet.new(options)
5455
else
55-
seeds.size > 1 ? Unknown.new(options) : Standalone.new(options)
56+
seeds.size > 1 ? Unknown.new(options) : Single.new(options)
5657
end
5758
end
5859
end

lib/mongo/cluster/topology/replica_set.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ def servers(servers)
135135
# @since 2.0.0
136136
def sharded?; false; end
137137

138-
# A replica set topology is not standalone.
138+
# A replica set topology is not single.
139139
#
140-
# @example Is the topology standalone?
141-
# ReplicaSet.standalone?
140+
# @example Is the topology single?
141+
# ReplicaSet.single?
142142
#
143143
# @return [ false ] Always false.
144144
#
145145
# @since 2.0.0
146-
def standalone?; false; end
146+
def single?; false; end
147147

148148
# A replica set topology is not unknown.
149149
#

lib/mongo/cluster/topology/sharded.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ def servers(servers)
107107
# @since 2.0.0
108108
def sharded?; true; end
109109

110-
# A sharded topology is not standalone.
110+
# A sharded topology is not single.
111111
#
112-
# @example Is the topology standalone?
113-
# Sharded.standalone?
112+
# @example Is the topology single?
113+
# Sharded.single?
114114
#
115115
# @return [ false ] Always false.
116116
#
117117
# @since 2.0.0
118-
def standalone?; false; end
118+
def single?; false; end
119119

120120
# A sharded topology is not unknown.
121121
#

lib/mongo/cluster/topology/standalone.rb renamed to lib/mongo/cluster/topology/single.rb

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ module Mongo
1616
class Cluster
1717
module Topology
1818

19-
# Defines behaviour for when a cluster is in standalone topology.
19+
# Defines behaviour for when a cluster is in single topology.
2020
#
2121
# @since 2.0.0
22-
class Standalone
22+
class Single
2323

2424
# The display name for the topology.
2525
#
2626
# @since 2.0.0
27-
NAME = 'Standalone'.freeze
27+
NAME = 'Single'.freeze
2828

2929
# Get the display name.
3030
#
3131
# @example Get the display name.
32-
# Standalone.display_name
32+
# Single.display_name
3333
#
3434
# @return [ String ] The display name.
3535
#
@@ -48,13 +48,13 @@ def display_name
4848
# @param [ Array<Server> ] servers The list of known servers to the
4949
# cluster.
5050
#
51-
# @return [ Standalone ] The topology.
51+
# @return [ Single ] The topology.
5252
def elect_primary(description, servers); self; end
5353

5454
# Initialize the topology with the options.
5555
#
5656
# @example Initialize the topology.
57-
# Standalone.new(options)
57+
# Single.new(options)
5858
#
5959
# @param [ Hash ] options The options.
6060
#
@@ -63,20 +63,20 @@ def initialize(options)
6363
@options = options
6464
end
6565

66-
# A standalone topology is not a replica set.
66+
# A single topology is not a replica set.
6767
#
6868
# @example Is the topology a replica set?
69-
# Sharded.replica_set?
69+
# Single.replica_set?
7070
#
7171
# @return [ false ] Always false.
7272
#
7373
# @since 2.0.0
7474
def replica_set?; false; end
7575

76-
# Standalone topologies have no replica set name.
76+
# Single topologies have no replica set name.
7777
#
7878
# @example Get the replica set name.
79-
# standalone.replica_set_name
79+
# single.replica_set_name
8080
#
8181
# @return [ nil ] Always nil.
8282
#
@@ -86,41 +86,43 @@ def replica_set_name; nil; end
8686
# Select appropriate servers for this topology.
8787
#
8888
# @example Select the servers.
89-
# Standalone.servers(servers, 'test')
89+
# Single.servers(servers, 'test')
9090
#
9191
# @param [ Array<Server> ] servers The known servers.
9292
#
93-
# @return [ Array<Server> ] The standalone servers.
93+
# @return [ Array<Server> ] The single servers.
9494
#
9595
# @since 2.0.0
9696
def servers(servers, name = nil)
97-
[ servers.detect{ |server| server.standalone? } ]
97+
[ servers.detect do |server|
98+
!server.unknown? && !server.arbiter? && !server.ghost?
99+
end ]
98100
end
99101

100-
# A standalone topology is not sharded.
102+
# A single topology is not sharded.
101103
#
102104
# @example Is the topology sharded?
103-
# Standalone.sharded?
105+
# Single.sharded?
104106
#
105107
# @return [ false ] Always false.
106108
#
107109
# @since 2.0.0
108110
def sharded?; false; end
109111

110-
# A standalone topology is standalone.
112+
# A single topology is single.
111113
#
112-
# @example Is the topology standalone?
113-
# Standalone.standalone?
114+
# @example Is the topology single?
115+
# Single.single?
114116
#
115117
# @return [ true ] Always true.
116118
#
117119
# @since 2.0.0
118-
def standalone?; true; end
120+
def single?; true; end
119121

120-
# An standalone topology is not unknown.
122+
# An single topology is not unknown.
121123
#
122124
# @example Is the topology unknown?
123-
# Standalone.unknown?
125+
# Single.unknown?
124126
#
125127
# @return [ false ] Always false.
126128
#

lib/mongo/cluster/topology/unknown.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ def servers(servers)
118118
# @since 2.0.0
119119
def sharded?; false; end
120120

121-
# An unknown topology is not standalone.
121+
# An unknown topology is not single.
122122
#
123-
# @example Is the topology standalone?
124-
# Unknown.standalone?
123+
# @example Is the topology single?
124+
# Unknown.single?
125125
#
126126
# @return [ true ] Always false.
127127
#
128128
# @since 2.0.0
129-
def standalone?; false; end
129+
def single?; false; end
130130

131131
# An unknown topology is unknown.
132132
#

lib/mongo/server_selector/selectable.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def initialize(tag_sets = [], options = {})
8686
def select_server(cluster)
8787
deadline = Time.now + server_selection_timeout
8888
while (deadline - Time.now) > 0
89-
if cluster.standalone?
89+
if cluster.single?
9090
servers = cluster.servers
9191
elsif cluster.sharded?
9292
servers = near_servers(cluster.servers)
@@ -136,7 +136,7 @@ def local_threshold
136136
# @since 2.0.0
137137
def primary(candidates)
138138
candidates.select do |server|
139-
server.primary? || server.standalone?
139+
server.primary?
140140
end
141141
end
142142

spec/mongo/cluster/topology/replica_set_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@
9292
end
9393
end
9494

95-
describe '.standalone?' do
95+
describe '.single?' do
9696

9797
it 'returns false' do
98-
expect(described_class.new({})).to_not be_standalone
98+
expect(described_class.new({})).to_not be_single
9999
end
100100
end
101101
end

spec/mongo/cluster/topology/sharded_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565
end
6666
end
6767

68-
describe '.standalone?' do
68+
describe '.single?' do
6969

7070
it 'returns false' do
71-
expect(topology).to_not be_standalone
71+
expect(topology).to_not be_single
7272
end
7373
end
7474
end

spec/mongo/cluster/topology/standalone_spec.rb renamed to spec/mongo/cluster/topology/single_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'spec_helper'
22

3-
describe Mongo::Cluster::Topology::Standalone do
3+
describe Mongo::Cluster::Topology::Single do
44

55
let(:address) do
66
Mongo::Address.new('127.0.0.1:27017')
@@ -70,10 +70,10 @@
7070
end
7171
end
7272

73-
describe '.standalone?' do
73+
describe '.single?' do
7474

7575
it 'returns true' do
76-
expect(topology).to be_standalone
76+
expect(topology).to be_single
7777
end
7878
end
7979
end

spec/mongo/cluster/topology_spec.rb

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
end
1616
end
1717

18-
context 'when provided a standalone option' do
18+
context 'when provided a single option' do
1919

2020
let(:topology) do
2121
described_class.initial([ 'a' ], connect: :direct)
2222
end
2323

24-
it 'returns a standalone topology' do
25-
expect(topology).to be_a(Mongo::Cluster::Topology::Standalone)
24+
it 'returns a single topology' do
25+
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
2626
end
2727
end
2828

@@ -56,8 +56,30 @@
5656
described_class.initial([], {})
5757
end
5858

59-
it 'returns a standalone topology' do
60-
expect(topology).to be_a(Mongo::Cluster::Topology::Standalone)
59+
it 'returns a single topology' do
60+
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
61+
end
62+
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)
6183
end
6284
end
6385
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/mongo/database_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@
158158
context 'when an alternate read preference is specified' do
159159

160160
before do
161-
allow(database.cluster).to receive(:standalone?).and_return(false)
161+
allow(database.cluster).to receive(:single?).and_return(false)
162162
end
163163

164164
let(:read) do

spec/mongo/server_selection_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let(:cluster) do
1818
double('cluster').tap do |c|
1919
allow(c).to receive(:topology).and_return(topology)
20-
allow(c).to receive(:standalone?).and_return(topology.standalone?)
20+
allow(c).to receive(:single?).and_return(topology.single?)
2121
allow(c).to receive(:sharded?).and_return(topology.sharded?)
2222
allow(c).to receive(:replica_set?).and_return(topology.replica_set?)
2323
end

0 commit comments

Comments
 (0)