File tree Expand file tree Collapse file tree 12 files changed +195
-78
lines changed Expand file tree Collapse file tree 12 files changed +195
-78
lines changed Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ def scan!
181
181
#
182
182
# @since 2.0.0
183
183
def servers
184
- topology . servers ( @servers )
184
+ topology . servers ( @servers . compact ) . compact
185
185
end
186
186
187
187
# Create a cluster for the provided client, for use when we don't want the
Original file line number Diff line number Diff line change @@ -106,6 +106,7 @@ def replica_set_name; nil; end
106
106
#
107
107
# @since 2.0.0
108
108
def servers ( servers )
109
+ [ ]
109
110
end
110
111
111
112
# An unknown topology is not sharded.
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ class Error < StandardError
71
71
require 'mongo/error/invalid_file'
72
72
require 'mongo/error/invalid_nonce'
73
73
require 'mongo/error/invalid_replacement_document'
74
+ require 'mongo/error/invalid_server_preference'
74
75
require 'mongo/error/invalid_signature'
75
76
require 'mongo/error/invalid_update_document'
76
77
require 'mongo/error/invalid_uri'
@@ -79,6 +80,7 @@ class Error < StandardError
79
80
require 'mongo/error/max_message_size'
80
81
require 'mongo/error/multi_index_drop'
81
82
require 'mongo/error/need_primary_server'
83
+ require 'mongo/error/no_server_available'
82
84
require 'mongo/error/socket_error'
83
85
require 'mongo/error/socket_timeout_error'
84
86
require 'mongo/error/unsupported_features'
Original file line number Diff line number Diff line change
1
+ # Copyright (C) 2014-2015 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ class Error
17
+
18
+ # Raised when an invalid server preference is provided.
19
+ #
20
+ # @since 2.0.0
21
+ class InvalidServerPreference < Error
22
+
23
+ # Instantiate the new exception.
24
+ #
25
+ # @example Instantiate the exception.
26
+ # Mongo::ServerSelector::InvalidServerPreference.new
27
+ #
28
+ # @param [ String ] name The preference name.
29
+ #
30
+ # @since 2.0.0
31
+ def initialize ( name )
32
+ super ( "This server preference #{ name } cannot be combined with tags." )
33
+ end
34
+ end
35
+ end
36
+ end
Original file line number Diff line number Diff line change
1
+ # Copyright (C) 2014-2015 MongoDB, Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ module Mongo
16
+ class Error
17
+
18
+ # Raised if there are no servers available matching the preference.
19
+ #
20
+ # @since 2.0.0
21
+ class NoServerAvailable < Error
22
+
23
+ # Instantiate the new exception.
24
+ #
25
+ # @example Instantiate the exception.
26
+ # Mongo::Error::NoServerAvailable.new(server_selector)
27
+ #
28
+ # @param [ Hash ] server_selector The server preference that could not be
29
+ # satisfied.
30
+ #
31
+ # @since 2.0.0
32
+ def initialize ( server_selector )
33
+ super ( "No server is available matching preference: #{ server_selector . inspect } " )
34
+ end
35
+ end
36
+ end
37
+ end
Original file line number Diff line number Diff line change @@ -58,24 +58,5 @@ def get(preference = {}, options = {})
58
58
options
59
59
)
60
60
end
61
-
62
- # Exception raised if there are no servers available matching the preference.
63
- #
64
- # @since 2.0.0
65
- class NoServerAvailable < Error
66
-
67
- # Instantiate the new exception.
68
- #
69
- # @example Instantiate the exception.
70
- # Mongo::ServerSelector::NoServerAvailable.new(server_selector)
71
- #
72
- # @param [ Hash ] server_selector The server preference that could not be
73
- # satisfied.
74
- #
75
- # @since 2.0.0
76
- def initialize ( server_selector )
77
- super ( "No server is available matching preference: #{ server_selector . inspect } " )
78
- end
79
- end
80
61
end
81
62
end
Original file line number Diff line number Diff line change @@ -67,7 +67,7 @@ def ==(other)
67
67
# @since 2.0.0
68
68
def initialize ( tag_sets = [ ] , options = { } )
69
69
if !tag_sets . all? { |set | set . empty? } && !tags_allowed?
70
- raise ServerSelector ::InvalidServerPreference . new ( name )
70
+ raise Error ::InvalidServerPreference . new ( name )
71
71
end
72
72
@tag_sets = tag_sets
73
73
@options = options
@@ -96,7 +96,7 @@ def select_server(cluster)
96
96
return servers . first if servers && !servers . compact . empty?
97
97
cluster . scan!
98
98
end
99
- raise NoServerAvailable . new ( self )
99
+ raise Error :: NoServerAvailable . new ( self )
100
100
end
101
101
102
102
# Get the timeout for server selection.
@@ -187,23 +187,5 @@ def match_tag_sets(candidates)
187
187
matches || [ ]
188
188
end
189
189
end
190
-
191
- # Raised when an invalid server preference is provided.
192
- #
193
- # @since 2.0.0
194
- class InvalidServerPreference < Error
195
-
196
- # Instantiate the new exception.
197
- #
198
- # @example Instantiate the exception.
199
- # Mongo::ServerSelector::InvalidServerPreference.new
200
- #
201
- # @param [ String ] name The preference name.
202
- #
203
- # @since 2.0.0
204
- def initialize ( name )
205
- super ( "This server preference #{ name } cannot be combined with tags." )
206
- end
207
- end
208
190
end
209
191
end
Original file line number Diff line number Diff line change 149
149
end
150
150
end
151
151
end
152
+
153
+ context 'when the cluster has no servers' do
154
+
155
+ let ( :cluster ) do
156
+ described_class . new ( ADDRESSES )
157
+ end
158
+
159
+ let ( :servers ) do
160
+ [ nil ]
161
+ end
162
+
163
+ before do
164
+ cluster . instance_variable_set ( :@servers , servers )
165
+ cluster . instance_variable_set ( :@topology , topology )
166
+ end
167
+
168
+ context 'when topology is Single' do
169
+
170
+ let ( :topology ) do
171
+ Mongo ::Cluster ::Topology ::Single . new ( { } )
172
+ end
173
+
174
+ it 'returns an empty array' do
175
+ expect ( cluster . servers ) . to eq ( [ ] )
176
+ end
177
+ end
178
+
179
+ context 'when topology is ReplicaSet' do
180
+
181
+ let ( :topology ) do
182
+ Mongo ::Cluster ::Topology ::ReplicaSet . new ( { } )
183
+ end
184
+
185
+ it 'returns an empty array' do
186
+ expect ( cluster . servers ) . to eq ( [ ] )
187
+ end
188
+ end
189
+
190
+ context 'when topology is Sharded' do
191
+
192
+ let ( :topology ) do
193
+ Mongo ::Cluster ::Topology ::Sharded . new ( { } )
194
+ end
195
+
196
+ it 'returns an empty array' do
197
+ expect ( cluster . servers ) . to eq ( [ ] )
198
+ end
199
+ end
200
+
201
+ context 'when topology is Unknown' do
202
+
203
+ let ( :topology ) do
204
+ Mongo ::Cluster ::Topology ::Unknown . new ( { } )
205
+ end
206
+
207
+ it 'returns an empty array' do
208
+ expect ( cluster . servers ) . to eq ( [ ] )
209
+ end
210
+ end
211
+ end
152
212
end
153
213
end
Original file line number Diff line number Diff line change 176
176
it 'uses that read preference' , unless : sharded? do
177
177
expect do
178
178
database . command ( { ping : 1 } , { read : read } )
179
- end . to raise_error ( Mongo ::ServerSelector ::NoServerAvailable )
179
+ end . to raise_error ( Mongo ::Error ::NoServerAvailable )
180
180
end
181
181
end
182
182
end
Original file line number Diff line number Diff line change 61
61
allow ( cluster ) . to receive ( :scan! ) . and_return ( true )
62
62
end
63
63
64
- context 'Valid read preference and matching server available' , unless : spec . raises_exception ? do
64
+ context 'Valid read preference and matching server available' , if : spec . server_available ? do
65
65
66
66
it 'Finds all suitable servers in the latency window' , if : spec . replica_set? do
67
67
expect ( server_selector . send ( :select , cluster . servers ) ) . to eq ( in_latency_window )
72
72
end
73
73
end
74
74
75
- context 'Invalid read preference ' , if : spec . invalid_server_preference ? do
75
+ context 'No matching server available ' , if : ! spec . server_available ? do
76
76
77
77
it 'Raises exception' do
78
78
expect do
79
79
server_selector . select_server ( cluster )
80
- end . to raise_exception ( Mongo ::ServerSelector ::InvalidServerPreference )
81
- end
82
- end
83
-
84
- context 'No matching server available' , unless : spec . server_available? do
85
-
86
- it 'Raises exception' do
87
- expect do
88
- server_selector . select_server ( cluster )
89
- end . to raise_exception ( Mongo ::ServerSelector ::NoServerAvailable )
80
+ end . to raise_exception ( Mongo ::Error ::NoServerAvailable )
90
81
end
91
82
end
92
83
end
Original file line number Diff line number Diff line change 94
94
it 'raises a NoServerAvailable error' do
95
95
expect do
96
96
read_pref . select_server ( cluster )
97
- end . to raise_exception ( Mongo ::ServerSelector ::NoServerAvailable )
97
+ end . to raise_exception ( Mongo ::Error ::NoServerAvailable )
98
98
end
99
99
end
100
100
end
101
+
102
+ shared_context 'a ServerSelector' do
103
+
104
+ context 'when cluster#servers is empty' do
105
+
106
+ let ( :servers ) { [ ] }
107
+
108
+ let ( :cluster ) do
109
+ double ( 'cluster' ) . tap do |c |
110
+ allow ( c ) . to receive ( :servers ) . and_return ( servers )
111
+ allow ( c ) . to receive ( :single? ) . and_return ( single )
112
+ allow ( c ) . to receive ( :sharded? ) . and_return ( sharded )
113
+ allow ( c ) . to receive ( :scan! ) . and_return ( true )
114
+ end
115
+ end
116
+
117
+ let ( :read_pref ) do
118
+ described_class . get ( { mode : :primary } , server_selection_timeout : 1 )
119
+ end
120
+
121
+ it 'raises a NoServerAvailable error' do
122
+ expect do
123
+ read_pref . select_server ( cluster )
124
+ end . to raise_exception ( Mongo ::Error ::NoServerAvailable )
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'when the cluster has a Single topology' do
130
+
131
+ let ( :single ) { true }
132
+ let ( :sharded ) { false }
133
+
134
+ it_behaves_like 'a ServerSelector'
135
+ end
136
+
137
+ context 'when the cluster has a ReplicaSet topology' do
138
+
139
+ let ( :single ) { false }
140
+ let ( :sharded ) { false }
141
+
142
+ it_behaves_like 'a ServerSelector'
143
+ end
144
+
145
+ context 'when the cluster has a Sharded topology' do
146
+
147
+ let ( :single ) { false }
148
+ let ( :sharded ) { true }
149
+
150
+ it_behaves_like 'a ServerSelector'
151
+ end
101
152
end
Original file line number Diff line number Diff line change @@ -92,18 +92,6 @@ def replica_set?
92
92
type == Mongo ::Cluster ::Topology ::ReplicaSet
93
93
end
94
94
95
- # Does this spec raise an exception.
96
- #
97
- # @example Determine if the spec raises an exception.
98
- # spec.raises_exception?
99
- #
100
- # @return [true, false] If the spec raises an exception.
101
- #
102
- # @since 2.0.0
103
- def raises_exception?
104
- !server_available? || invalid_server_preference?
105
- end
106
-
107
95
# Does this spec expect a server to be found.
108
96
#
109
97
# @example Will a server be found with this spec.
@@ -116,18 +104,6 @@ def server_available?
116
104
!in_latency_window . empty?
117
105
end
118
106
119
- # Is the read preference defined in the spec invalid.
120
- #
121
- # @example Determine if the spec's read preference is invalid.
122
- # spec.invalid_server_preference?
123
- #
124
- # @return [true, false] If the spec's read preference is invalid.
125
- #
126
- # @since 2.0.0
127
- def invalid_server_preference?
128
- read_preference [ 'mode' ] == 'Primary' && read_preference [ 'tag_sets' ]
129
- end
130
-
131
107
# The subset of suitable servers that falls within the allowable latency
132
108
# window.
133
109
# We have to correct for our server selection algorithm that adds the primary
You can’t perform that action at this time.
0 commit comments