Skip to content

RUBY-891 Rename Standalone topology to Single and add tests #596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 7, 2015
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
2 changes: 1 addition & 1 deletion lib/mongo/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Cluster
# @return [ Object ] The cluster topology.
attr_reader :topology

def_delegators :topology, :replica_set?, :replica_set_name, :sharded?, :standalone?, :unknown?
def_delegators :topology, :replica_set?, :replica_set_name, :sharded?, :single?, :unknown?

# Determine if this cluster of servers is equal to another object. Checks the
# servers currently in the cluster, not what was configured.
Expand Down
9 changes: 5 additions & 4 deletions lib/mongo/cluster/topology.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

require 'mongo/cluster/topology/replica_set'
require 'mongo/cluster/topology/sharded'
require 'mongo/cluster/topology/standalone'
require 'mongo/cluster/topology/single'
require 'mongo/cluster/topology/unknown'

module Mongo
Expand All @@ -32,17 +32,18 @@ module Topology
OPTIONS = {
replica_set: ReplicaSet,
sharded: Sharded,
direct: Standalone
direct: Single
}

# Get the initial cluster topology for the provided options.
#
# @example Get the initial cluster topology.
# Topology.initial(topology: :replica_set)
#
# @param [ Array<String> ] seeds The addresses of the configured servers.
# @param [ Hash ] options The cluster options.
#
# @return [ ReplicaSet, Sharded, Standalone ] The topology.
# @return [ ReplicaSet, Sharded, Single ] The topology.
#
# @since 2.0.0
def initial(seeds, options)
Expand All @@ -52,7 +53,7 @@ def initial(seeds, options)
if options.has_key?(:replica_set)
ReplicaSet.new(options)
else
seeds.size > 1 ? Unknown.new(options) : Standalone.new(options)
seeds.size > 1 ? Unknown.new(options) : Single.new(options)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo/cluster/topology/replica_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ def servers(servers)
# @since 2.0.0
def sharded?; false; end

# A replica set topology is not standalone.
# A replica set topology is not single.
#
# @example Is the topology standalone?
# ReplicaSet.standalone?
# @example Is the topology single?
# ReplicaSet.single?
#
# @return [ false ] Always false.
#
# @since 2.0.0
def standalone?; false; end
def single?; false; end

# A replica set topology is not unknown.
#
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo/cluster/topology/sharded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def servers(servers)
# @since 2.0.0
def sharded?; true; end

# A sharded topology is not standalone.
# A sharded topology is not single.
#
# @example Is the topology standalone?
# Sharded.standalone?
# @example Is the topology single?
# Sharded.single?
#
# @return [ false ] Always false.
#
# @since 2.0.0
def standalone?; false; end
def single?; false; end

# A sharded topology is not unknown.
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ module Mongo
class Cluster
module Topology

# Defines behaviour for when a cluster is in standalone topology.
# Defines behaviour for when a cluster is in single topology.
#
# @since 2.0.0
class Standalone
class Single

# The display name for the topology.
#
# @since 2.0.0
NAME = 'Standalone'.freeze
NAME = 'Single'.freeze

# Get the display name.
#
# @example Get the display name.
# Standalone.display_name
# Single.display_name
#
# @return [ String ] The display name.
#
Expand All @@ -48,13 +48,13 @@ def display_name
# @param [ Array<Server> ] servers The list of known servers to the
# cluster.
#
# @return [ Standalone ] The topology.
# @return [ Single ] The topology.
def elect_primary(description, servers); self; end

# Initialize the topology with the options.
#
# @example Initialize the topology.
# Standalone.new(options)
# Single.new(options)
#
# @param [ Hash ] options The options.
#
Expand All @@ -63,20 +63,20 @@ def initialize(options)
@options = options
end

# A standalone topology is not a replica set.
# A single topology is not a replica set.
#
# @example Is the topology a replica set?
# Sharded.replica_set?
# Single.replica_set?
#
# @return [ false ] Always false.
#
# @since 2.0.0
def replica_set?; false; end

# Standalone topologies have no replica set name.
# Single topologies have no replica set name.
#
# @example Get the replica set name.
# standalone.replica_set_name
# single.replica_set_name
#
# @return [ nil ] Always nil.
#
Expand All @@ -86,41 +86,43 @@ def replica_set_name; nil; end
# Select appropriate servers for this topology.
#
# @example Select the servers.
# Standalone.servers(servers, 'test')
# Single.servers(servers, 'test')
#
# @param [ Array<Server> ] servers The known servers.
#
# @return [ Array<Server> ] The standalone servers.
# @return [ Array<Server> ] The single servers.
#
# @since 2.0.0
def servers(servers, name = nil)
[ servers.detect{ |server| server.standalone? } ]
[ servers.detect do |server|
!server.unknown? && !server.arbiter? && !server.ghost?
end ]
end

# A standalone topology is not sharded.
# A single topology is not sharded.
#
# @example Is the topology sharded?
# Standalone.sharded?
# Single.sharded?
#
# @return [ false ] Always false.
#
# @since 2.0.0
def sharded?; false; end

# A standalone topology is standalone.
# A single topology is single.
#
# @example Is the topology standalone?
# Standalone.standalone?
# @example Is the topology single?
# Single.single?
#
# @return [ true ] Always true.
#
# @since 2.0.0
def standalone?; true; end
def single?; true; end

# An standalone topology is not unknown.
# An single topology is not unknown.
#
# @example Is the topology unknown?
# Standalone.unknown?
# Single.unknown?
#
# @return [ false ] Always false.
#
Expand Down
8 changes: 4 additions & 4 deletions lib/mongo/cluster/topology/unknown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ def servers(servers)
# @since 2.0.0
def sharded?; false; end

# An unknown topology is not standalone.
# An unknown topology is not single.
#
# @example Is the topology standalone?
# Unknown.standalone?
# @example Is the topology single?
# Unknown.single?
#
# @return [ true ] Always false.
#
# @since 2.0.0
def standalone?; false; end
def single?; false; end

# An unknown topology is unknown.
#
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo/server_selector/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def initialize(tag_sets = [], options = {})
def select_server(cluster)
deadline = Time.now + server_selection_timeout
while (deadline - Time.now) > 0
if cluster.standalone?
if cluster.single?
servers = cluster.servers
elsif cluster.sharded?
servers = near_servers(cluster.servers)
Expand Down Expand Up @@ -136,7 +136,7 @@ def local_threshold
# @since 2.0.0
def primary(candidates)
candidates.select do |server|
server.primary? || server.standalone?
server.primary?
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/mongo/cluster/topology/replica_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@
end
end

describe '.standalone?' do
describe '.single?' do

it 'returns false' do
expect(described_class.new({})).to_not be_standalone
expect(described_class.new({})).to_not be_single
end
end
end
4 changes: 2 additions & 2 deletions spec/mongo/cluster/topology/sharded_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
end
end

describe '.standalone?' do
describe '.single?' do

it 'returns false' do
expect(topology).to_not be_standalone
expect(topology).to_not be_single
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'spec_helper'

describe Mongo::Cluster::Topology::Standalone do
describe Mongo::Cluster::Topology::Single do

let(:address) do
Mongo::Address.new('127.0.0.1:27017')
Expand Down Expand Up @@ -70,10 +70,10 @@
end
end

describe '.standalone?' do
describe '.single?' do

it 'returns true' do
expect(topology).to be_standalone
expect(topology).to be_single
end
end
end
32 changes: 27 additions & 5 deletions spec/mongo/cluster/topology_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
end
end

context 'when provided a standalone option' do
context 'when provided a single option' do

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

it 'returns a standalone topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Standalone)
it 'returns a single topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
end
end

Expand Down Expand Up @@ -56,8 +56,30 @@
described_class.initial([], {})
end

it 'returns a standalone topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Standalone)
it 'returns a single topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
end
end

context 'when provided a single mongos', if: single_mongos? do

let(:topology) do
described_class.initial(ADDRESSES, {})
end

it 'returns a single topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
end
end

context 'when provided a single replica set member', if: single_rs_member? do

let(:topology) do
described_class.initial(ADDRESSES, {})
end

it 'returns a single topology' do
expect(topology).to be_a(Mongo::Cluster::Topology::Single)
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions spec/mongo/cluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,28 @@
expect(cluster.scan!).to be true
end
end

describe '#servers' do

context 'when topology is single', if: single_seed? do

let(:cluster) do
described_class.new(ADDRESSES)
end

context 'when the server is a mongos', if: single_mongos? do

it 'returns the mongos' do
expect(cluster.servers.size).to eq(1)
end
end

context 'when the server is a replica set member', if: single_rs_member? do

it 'returns the replica set member' do
expect(cluster.servers.size).to eq(1)
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/mongo/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
context 'when an alternate read preference is specified' do

before do
allow(database.cluster).to receive(:standalone?).and_return(false)
allow(database.cluster).to receive(:single?).and_return(false)
end

let(:read) do
Expand Down
2 changes: 1 addition & 1 deletion spec/mongo/server_selection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:topology).and_return(topology)
allow(c).to receive(:standalone?).and_return(topology.standalone?)
allow(c).to receive(:single?).and_return(topology.single?)
allow(c).to receive(:sharded?).and_return(topology.sharded?)
allow(c).to receive(:replica_set?).and_return(topology.replica_set?)
end
Expand Down
Loading