1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
require 'spec_helper'
describe Mongo::ServerSelector do
include_context 'server selector'
describe '.get' do
let(:selector) do
described_class.get(:mode => name, :tag_sets => tag_sets)
end
context 'when a server selector object is passed' do
let(:name) do
:primary
end
it 'returns the object' do
expect(described_class.get(selector)).to be(selector)
end
end
context 'when the mode is primary' do
let(:name) do
:primary
end
it 'returns a read preference of class Primary' do
expect(selector).to be_a(Mongo::ServerSelector::Primary)
end
end
context 'when the mode is primary_preferred' do
let(:name) do
:primary_preferred
end
it 'returns a read preference of class PrimaryPreferred' do
expect(selector).to be_a(Mongo::ServerSelector::PrimaryPreferred)
end
end
context 'when the mode is secondary' do
let(:name) do
:secondary
end
it 'returns a read preference of class Secondary' do
expect(selector).to be_a(Mongo::ServerSelector::Secondary)
end
end
context 'when the mode is secondary_preferred' do
let(:name) do
:secondary_preferred
end
it 'returns a read preference of class SecondaryPreferred' do
expect(selector).to be_a(Mongo::ServerSelector::SecondaryPreferred)
end
end
context 'when the mode is nearest' do
let(:name) do
:nearest
end
it 'returns a read preference of class Nearest' do
expect(selector).to be_a(Mongo::ServerSelector::Nearest)
end
end
context 'when a mode is not provided' do
let(:selector) { described_class.get }
it 'returns a read preference of class Primary' do
expect(selector).to be_a(Mongo::ServerSelector::Primary)
end
end
context 'when tag sets are provided' do
let(:selector) do
described_class.get(:mode => :secondary, :tag_sets => tag_sets)
end
let(:tag_sets) do
[{ 'test' => 'tag' }]
end
it 'sets tag sets on the read preference object' do
expect(selector.tag_sets).to eq(tag_sets)
end
end
context 'when server_selection_timeout is specified' do
let(:selector) do
described_class.get(:mode => :secondary, :server_selection_timeout => 1)
end
it 'sets server selection timeout on the read preference object' do
expect(selector.server_selection_timeout).to eq(1)
end
end
context 'when server_selection_timeout is not specified' do
let(:selector) do
described_class.get(:mode => :secondary)
end
it 'sets server selection timeout to the default' do
expect(selector.server_selection_timeout).to eq(Mongo::ServerSelector::SERVER_SELECTION_TIMEOUT)
end
end
context 'when local_threshold is specified' do
let(:selector) do
described_class.get(:mode => :secondary, :local_threshold => 0.010)
end
it 'sets local_threshold on the read preference object' do
expect(selector.local_threshold).to eq(0.010)
end
end
context 'when local_threshold is not specified' do
let(:selector) do
described_class.get(:mode => :secondary)
end
it 'sets local threshold to the default' do
expect(selector.local_threshold).to eq(Mongo::ServerSelector::LOCAL_THRESHOLD)
end
end
end
describe "#select_server" do
context 'when #select returns a list of nils' do
let(:servers) do
[ make_server(:primary) ]
end
let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:topology).and_return(topology)
allow(c).to receive(:servers).and_return(servers)
allow(c).to receive(:single?).and_return(false)
allow(c).to receive(:sharded?).and_return(false)
allow(c).to receive(:unknown?).and_return(false)
allow(c).to receive(:scan!).and_return(true)
allow(c).to receive(:options).and_return(server_selection_timeout: 0.1)
end
end
let(:read_pref) do
described_class.get(mode: :primary).tap do |pref|
allow(pref).to receive(:select).and_return([ nil, nil ])
end
end
it 'raises a NoServerAvailable error' do
expect do
read_pref.select_server(cluster)
end.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
context 'when the cluster has a server_selection_timeout set' do
let(:servers) do
[ make_server(:secondary), make_server(:primary) ]
end
let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:topology).and_return(topology)
allow(c).to receive(:servers).and_return(servers)
allow(c).to receive(:single?).and_return(false)
allow(c).to receive(:sharded?).and_return(false)
allow(c).to receive(:unknown?).and_return(false)
allow(c).to receive(:scan!).and_return(true)
allow(c).to receive(:options).and_return(server_selection_timeout: 0)
end
end
let(:read_pref) do
described_class.get(mode: :nearest)
end
it 'uses the server_selection_timeout of the cluster' do
expect{
read_pref.select_server(cluster)
}.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
context 'when the cluster has a local_threshold set' do
let(:near_server) do
make_server(:secondary).tap do |s|
allow(s).to receive(:connectable?).and_return(true)
allow(s).to receive(:average_round_trip_time).and_return(100)
allow(s).to receive(:check_driver_support!).and_return(true)
end
end
let(:far_server) do
make_server(:secondary).tap do |s|
allow(s).to receive(:connectable?).and_return(true)
allow(s).to receive(:average_round_trip_time).and_return(200)
allow(s).to receive(:check_driver_support!).and_return(true)
end
end
let(:servers) do
[ near_server, far_server ]
end
let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:topology).and_return(topology)
allow(c).to receive(:servers).and_return(servers)
allow(c).to receive(:single?).and_return(false)
allow(c).to receive(:sharded?).and_return(false)
allow(c).to receive(:unknown?).and_return(false)
allow(c).to receive(:scan!).and_return(true)
allow(c).to receive(:options).and_return(local_threshold: 0.050)
end
end
let(:read_pref) do
described_class.get(mode: :nearest)
end
it 'uses the local_threshold of the cluster' do
expect(read_pref.select_server(cluster)).to eq(near_server)
end
end
end
shared_context 'a ServerSelector' do
context 'when cluster#servers is empty' do
let(:servers) do
[]
end
let(:cluster) do
double('cluster').tap do |c|
allow(c).to receive(:topology).and_return(topology)
allow(c).to receive(:servers).and_return(servers)
allow(c).to receive(:single?).and_return(single)
allow(c).to receive(:sharded?).and_return(sharded)
allow(c).to receive(:unknown?).and_return(false)
allow(c).to receive(:scan!).and_return(true)
allow(c).to receive(:options).and_return(server_selection_timeout: 0.1)
end
end
let(:read_pref) do
described_class.get(mode: :primary)
end
it 'raises a NoServerAvailable error' do
expect do
read_pref.select_server(cluster)
end.to raise_exception(Mongo::Error::NoServerAvailable)
end
end
end
context 'when the cluster has a Single topology' do
let(:single) { true }
let(:sharded) { false }
it_behaves_like 'a ServerSelector'
end
context 'when the cluster has a ReplicaSet topology' do
let(:single) { false }
let(:sharded) { false }
it_behaves_like 'a ServerSelector'
end
context 'when the cluster has a Sharded topology' do
let(:single) { false }
let(:sharded) { true }
it_behaves_like 'a ServerSelector'
end
describe '#inspect' do
let(:options) do
{}
end
let(:read_pref) do
described_class.get({ mode: mode }.merge(options))
end
context 'when the mode is primary' do
let(:mode) do
:primary
end
it 'includes the mode in the inspect string' do
expect(read_pref.inspect).to match(/#{mode.to_s}/i)
end
end
context 'when there are tag sets' do
let(:mode) do
:secondary
end
let(:options) do
{ tag_sets: [{ 'data_center' => 'nyc' }] }
end
it 'includes the tag sets in the inspect string' do
expect(read_pref.inspect).to include(options[:tag_sets].inspect)
end
end
context 'when there is a max staleness set' do
let(:mode) do
:secondary
end
let(:options) do
{ max_staleness: 123 }
end
it 'includes the tag sets in the inspect string' do
expect(read_pref.inspect).to match(/max_staleness/i)
expect(read_pref.inspect).to match(/123/)
end
end
end
end
|