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 358 359 360
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe Mongo::Operation::ReadPreferenceSupported do
let(:selector) do
{ name: 'test' }
end
let(:options) do
{}
end
let(:cluster) do
double('cluster').tap do |cluster|
allow(cluster).to receive(:single?).and_return(single?)
end
end
let(:operation) do
Class.new do
include Mongo::Operation::ReadPreferenceSupported
end.new.tap do |op|
allow(op).to receive(:read).and_return(read_pref)
allow(op).to receive(:selector).and_return(selector)
allow(op).to receive(:options).and_return(options)
end
end
let(:description) do
double('description').tap do |description|
allow(description).to receive(:mongos?).and_return(mongos?)
allow(description).to receive(:standalone?).and_return(standalone?)
# TODO consider adding tests for load-balanced topologies also
allow(description).to receive(:load_balancer?).and_return(false)
end
end
let(:server) do
double('server').tap do |server|
allow(server).to receive(:cluster).and_return(cluster)
# TODO consider adding tests for load-balanced topologies also
allow(server).to receive(:load_balancer?).and_return(false)
end
end
let(:connection) do
double('connection').tap do |connection|
allow(connection).to receive(:server).and_return(server)
allow(connection).to receive(:description).and_return(description)
end
end
describe '#add_secondary_ok_flag?' do
let(:actual) do
operation.send(:add_secondary_ok_flag?, connection)
end
shared_examples_for 'sets the secondary_ok flag as expected' do
it 'sets the secondary_ok flag as expected' do
expect(actual).to eq(expected)
end
end
shared_examples_for 'never sets secondary_ok' do
let(:expected) { false }
context 'when no read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when primary read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :primary) }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when secondary read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :secondary) }
it_behaves_like 'sets the secondary_ok flag as expected'
end
end
shared_examples_for 'always sets secondary_ok' do
let(:expected) { true }
context 'when no read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when primary read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :primary) }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when secondary read preference is specified' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :secondary) }
it_behaves_like 'sets the secondary_ok flag as expected'
end
end
shared_examples_for 'sets secondary_ok if read preference is specified and is not primary' do
context 'when there is no read preference set' do
let(:read_pref) { Mongo::ServerSelector.get }
let(:expected) { false }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when there is a read preference' do
context 'when the read preference requires the secondary_ok flag' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :secondary) }
let(:expected) { true }
it_behaves_like 'sets the secondary_ok flag as expected'
end
context 'when the read preference does not require the secondary_ok flag' do
let(:read_pref) { Mongo::ServerSelector.get(:mode => :primary) }
let(:expected) { false }
it_behaves_like 'sets the secondary_ok flag as expected'
end
end
end
context 'when the topology is Single' do
let(:single?) { true }
let(:mongos?) { false }
context 'when the server is a standalone' do
let(:standalone?) { true }
it_behaves_like 'never sets secondary_ok'
end
context 'when the server is a mongos' do
let(:standalone?) { false }
let(:mongos?) { true }
it_behaves_like 'always sets secondary_ok'
end
context 'when the server is a replica set member' do
let(:standalone?) { false }
let(:mongos?) { false }
it_behaves_like 'always sets secondary_ok'
end
end
context 'when the topology is not Single' do
let(:single?) { false }
let(:mongos?) { false }
context 'when the server is a standalone' do
let(:standalone?) { true }
it_behaves_like 'never sets secondary_ok'
end
context 'when the server is a mongos' do
let(:standalone?) { false }
let(:mongos?) { true }
it_behaves_like 'sets secondary_ok if read preference is specified and is not primary'
end
context 'when the server is a replica set member' do
let(:standalone?) { false }
let(:mongos?) { false }
it_behaves_like 'sets secondary_ok if read preference is specified and is not primary'
end
end
end
describe '#add_read_preference_legacy' do
let(:read_pref) do
Mongo::ServerSelector.get(:mode => mode)
end
# Behavior of sending $readPreference is the same regardless of topology.
shared_examples_for '$readPreference in the command' do
let(:actual) do
operation.send(:add_read_preference_legacy, operation.send(:selector), connection)
end
let(:expected_read_preference) do
{mode: mode.to_s.gsub(/_(.)/) { $1.upcase }}
end
shared_examples_for 'adds read preference moving existing contents to $query' do
let(:expected) do
{ :$query => selector, :$readPreference => expected_read_preference }
end
it 'moves existing selector contents under $query and adds read preference' do
expect(actual).to eq(expected)
end
context 'when the selector already has $query in it' do
let(:selector) do
{ :$query => { :name => 'test' },
:$orderby => { :name => -1 } }
end
let(:expected) do
selector.merge(:$readPreference => expected_read_preference)
end
it 'keeps existing $query and adds read preference' do
expect(actual).to eq(expected)
end
end
end
shared_examples_for 'does not modify selector' do
it 'does not modify selector' do
expect(actual).to eq(selector)
end
end
shared_examples_for 'does not send read preference' do
([nil] + %i(primary primary_preferred secondary secondary_preferred nearest)).each do |_mode|
active_mode = _mode
context "when read preference mode is #{active_mode}" do
let(:mode) { active_mode }
it_behaves_like 'does not modify selector'
end
end
end
context 'when the server is a standalone' do
let(:standalone?) { true }
let(:mongos?) { false }
it_behaves_like 'does not send read preference'
end
context 'when the server is a mongos' do
let(:standalone?) { false }
let(:mongos?) { true }
context 'when the read preference mode is nil' do
let(:mode) { nil }
it_behaves_like 'does not modify selector'
end
context 'when the read preference mode is primary' do
let(:mode) { :primary }
it_behaves_like 'does not modify selector'
end
context 'when the read preference mode is primary_preferred' do
let(:mode) { :primary_preferred }
it_behaves_like 'adds read preference moving existing contents to $query'
end
context 'when the read preference mode is secondary' do
let(:mode) { :secondary }
it_behaves_like 'adds read preference moving existing contents to $query'
end
context 'when the read preference mode is secondary_preferred' do
let(:mode) { :secondary_preferred }
it_behaves_like 'does not modify selector'
context 'when there are fields in the selector besides :mode' do
let(:read_pref) do
Mongo::ServerSelector.get(:mode => mode, tag_sets: ['dc' => 'nyc'])
end
let(:expected_read_preference) do
{mode: mode.to_s.gsub(/_(.)/) { $1.upcase }, tags: ['dc' => 'nyc']}
end
it_behaves_like 'adds read preference moving existing contents to $query'
end
end
context 'when the read preference mode is nearest' do
let(:mode) { :nearest }
it_behaves_like 'adds read preference moving existing contents to $query'
end
end
context 'when the server is a replica set member' do
let(:standalone?) { false }
let(:mongos?) { false }
# $readPreference is not sent to replica set nodes running legacy
# servers - the allowance of secondary reads is handled by secondary_ok
# flag.
it_behaves_like 'does not send read preference'
end
end
context 'in single topology' do
let(:single?) { true }
it_behaves_like '$readPreference in the command'
end
context 'not in single topology' do
let(:single?) { false }
it_behaves_like '$readPreference in the command'
end
end
end
|