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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
|
require 'spec_helper'
describe Mongo::Cluster do
let(:monitoring) do
Mongo::Monitoring.new(monitoring: false)
end
let(:cluster) do
described_class.new(ADDRESSES, monitoring, TEST_OPTIONS)
end
describe '#==' do
context 'when the other is a cluster' do
context 'when the addresses are the same' do
context 'when the options are the same' do
let(:other) do
described_class.new(ADDRESSES, monitoring, TEST_OPTIONS)
end
it 'returns true' do
expect(cluster).to eq(other)
end
end
context 'when the options are not the same' do
let(:other) do
described_class.new([ '127.0.0.1:27017' ], monitoring, TEST_OPTIONS.merge(:replica_set => 'test'))
end
it 'returns false' do
expect(cluster).to_not eq(other)
end
end
end
context 'when the addresses are not the same' do
let(:other) do
described_class.new([ '127.0.0.1:27018' ], monitoring, TEST_OPTIONS)
end
it 'returns false' do
expect(cluster).to_not eq(other)
end
end
end
context 'when the other is not a cluster' do
it 'returns false' do
expect(cluster).to_not eq('test')
end
end
end
describe '#has_readable_server?' do
let(:selector) do
Mongo::ServerSelector.get(mode: :primary)
end
it 'delegates to the topology' do
expect(cluster.has_readable_server?).to eq(cluster.topology.has_readable_server?(cluster))
end
end
describe '#has_writable_server?' do
it 'delegates to the topology' do
expect(cluster.has_writable_server?).to eq(cluster.topology.has_writable_server?(cluster))
end
end
describe '#inspect' do
let(:preference) do
Mongo::ServerSelector.get(ServerSelector::PRIMARY)
end
it 'displays the cluster seeds and topology' do
expect(cluster.inspect).to include('topology')
expect(cluster.inspect).to include('servers')
end
end
describe '#replica_set_name' do
let(:preference) do
Mongo::ServerSelector.get(ServerSelector::PRIMARY)
end
context 'when the option is provided' do
let(:cluster) do
described_class.new(
[ '127.0.0.1:27017' ],
monitoring,
TEST_OPTIONS.merge(:connect => :replica_set, :replica_set => 'testing')
)
end
it 'returns the name' do
expect(cluster.replica_set_name).to eq('testing')
end
end
context 'when the option is not provided' do
let(:cluster) do
described_class.new([ '127.0.0.1:27017' ], monitoring, TEST_OPTIONS.merge(connect: :direct).delete_if { |k| k == :replica_set })
end
it 'returns nil' do
expect(cluster.replica_set_name).to be_nil
end
end
end
describe '#scan!' do
let(:preference) do
Mongo::ServerSelector.get(ServerSelector::PRIMARY)
end
let(:known_servers) do
cluster.instance_variable_get(:@servers)
end
before do
expect(known_servers.first).to receive(:scan!).and_call_original
end
it 'returns true' do
expect(cluster.scan!).to be true
end
end
describe '#servers' do
context 'when topology is single', if: single_seed? do
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
context 'when the cluster has no servers' do
let(:servers) do
[nil]
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end
context 'when topology is Single' do
let(:topology) do
Mongo::Cluster::Topology::Single.new({}, monitoring)
end
it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end
context 'when topology is ReplicaSet' do
let(:topology) do
Mongo::Cluster::Topology::ReplicaSet.new({}, monitoring)
end
it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end
context 'when topology is Sharded' do
let(:topology) do
Mongo::Cluster::Topology::Sharded.new({}, monitoring)
end
it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end
context 'when topology is Unknown' do
let(:topology) do
Mongo::Cluster::Topology::Unknown.new({}, monitoring)
end
it 'returns an empty array' do
expect(cluster.servers).to eq([])
end
end
end
end
describe '#add' do
context 'when topology is Single' do
let(:topology) do
Mongo::Cluster::Topology::Single.new({})
end
before do
cluster.add('a')
end
it 'does not add discovered servers to the cluster' do
expect(cluster.servers[0].address.seed).to_not eq('a')
end
end
end
describe '#disconnect!' do
let(:known_servers) do
cluster.instance_variable_get(:@servers)
end
let(:periodic_executor) do
cluster.instance_variable_get(:@periodic_executor)
end
before do
known_servers.each do |server|
expect(server).to receive(:disconnect!).and_call_original
end
expect(periodic_executor).to receive(:stop!).and_call_original
end
it 'disconnects each server and the cursor reaper and returns true' do
expect(cluster.disconnect!).to be(true)
end
end
describe '#reconnect!' do
let(:periodic_executor) do
cluster.instance_variable_get(:@periodic_executor)
end
before do
cluster.servers.each do |server|
expect(server).to receive(:reconnect!).and_call_original
end
expect(periodic_executor).to receive(:restart!).and_call_original
end
it 'reconnects each server and the cursor reaper and returns true' do
expect(cluster.reconnect!).to be(true)
end
end
describe '#remove' do
let(:address_a) do
Mongo::Address.new('127.0.0.1:27017')
end
let(:address_b) do
Mongo::Address.new('127.0.0.1:27018')
end
let(:monitoring) do
Mongo::Monitoring.new(monitoring: false)
end
let(:server_a) do
Mongo::Server.new(address_a, cluster, monitoring, Mongo::Event::Listeners.new)
end
let(:server_b) do
Mongo::Server.new(address_b, cluster, monitoring, Mongo::Event::Listeners.new)
end
let(:servers) do
[ server_a, server_b ]
end
let(:addresses) do
[ address_a, address_b ]
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@addresses, addresses)
cluster.remove('127.0.0.1:27017')
end
it 'removes the host from the list of servers' do
expect(cluster.instance_variable_get(:@servers)).to eq([server_b])
end
it 'removes the host from the list of addresses' do
expect(cluster.instance_variable_get(:@addresses)).to eq([address_b])
end
end
describe '#add_hosts' do
let(:servers) do
[nil]
end
let(:hosts) do
["127.0.0.1:27018"]
end
let(:description) do
Mongo::Server::Description.new(double('address'), { 'hosts' => hosts })
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end
context 'when the topology allows servers to be added' do
let(:topology) do
double('topology').tap do |t|
allow(t).to receive(:add_hosts?).and_return(true)
end
end
it 'adds the servers' do
expect(cluster).to receive(:add).once
cluster.add_hosts(description)
end
end
context 'when the topology does not allow servers to be added' do
let(:topology) do
double('topology').tap do |t|
allow(t).to receive(:add_hosts?).and_return(false)
end
end
it 'does not add the servers' do
expect(cluster).not_to receive(:add)
cluster.add_hosts(description)
end
end
end
describe '#remove_hosts' do
let(:listeners) do
Mongo::Event::Listeners.new
end
let(:address) do
Mongo::Address.new('127.0.0.1:27017')
end
let(:monitoring) do
Mongo::Monitoring.new(monitoring: false)
end
let(:server) do
Mongo::Server.new(address, cluster, monitoring, listeners)
end
let(:servers) do
[ server ]
end
let(:hosts) do
["127.0.0.1:27018"]
end
let(:description) do
Mongo::Server::Description.new(double('address'), { 'hosts' => hosts })
end
context 'when the topology allows servers to be removed' do
context 'when the topology allows a specific server to be removed' do
let(:topology) do
double('topology').tap do |t|
allow(t).to receive(:remove_hosts?).and_return(true)
allow(t).to receive(:remove_server?).and_return(true)
end
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end
it 'removes the servers' do
expect(cluster).to receive(:remove).once
cluster.remove_hosts(description)
end
end
context 'when the topology does not allow a specific server to be removed' do
let(:topology) do
double('topology').tap do |t|
allow(t).to receive(:remove_hosts?).and_return(true)
allow(t).to receive(:remove_server?).and_return(false)
end
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end
it 'removes the servers' do
expect(cluster).not_to receive(:remove)
cluster.remove_hosts(description)
end
end
end
context 'when the topology does not allow servers to be removed' do
let(:topology) do
double('topology').tap do |t|
allow(t).to receive(:remove_hosts?).and_return(false)
end
end
before do
cluster.instance_variable_set(:@servers, servers)
cluster.instance_variable_set(:@topology, topology)
end
it 'does not remove the servers' do
expect(cluster).not_to receive(:remove)
cluster.remove_hosts(description)
end
end
end
describe '#next_primary' do
let(:cluster) do
authorized_client.cluster
end
let(:primary_candidates) do
if cluster.single?
cluster.servers
elsif cluster.sharded?
cluster.servers
else
cluster.servers.select { |s| s.primary? }
end
end
it 'always returns the primary, mongos, or standalone' do
expect(primary_candidates).to include(cluster.next_primary)
end
end
describe '#app_metadata' do
it 'returns an AppMetadata object' do
expect(cluster.app_metadata).to be_a(Mongo::Cluster::AppMetadata)
end
context 'when the client has an app_name set' do
let(:cluster) do
authorized_client.with(app_name: 'reports').cluster
end
it 'constructs an AppMetadata object with the app_name' do
expect(cluster.app_metadata.send(:full_client_document)[:application]).to eq('name' => 'reports')
end
end
context 'when the client does not have an app_name set' do
let(:cluster) do
authorized_client.cluster
end
it 'constructs an AppMetadata object with no app_name' do
expect(cluster.app_metadata.send(:full_client_document)[:application]).to be_nil
end
end
end
describe '#logical_session_timeout' do
let(:listeners) do
Mongo::Event::Listeners.new
end
let(:monitoring) do
Mongo::Monitoring.new(monitoring: false)
end
let(:server_one) do
Mongo::Server.new(default_address, cluster, monitoring, listeners)
end
let(:server_two) do
Mongo::Server.new(default_address, cluster, monitoring, listeners)
end
let(:servers) do
[ server_one, server_two ]
end
before do
allow(cluster).to receive(:servers).and_return(servers)
end
context 'when one server has a nil logical session timeout value' do
before do
allow(server_one).to receive(:logical_session_timeout).and_return(7)
allow(server_two).to receive(:logical_session_timeout).and_return(nil)
end
it 'returns nil' do
expect(cluster.logical_session_timeout).to be(nil)
end
end
context 'when all servers have a logical session timeout value' do
before do
allow(server_one).to receive(:logical_session_timeout).and_return(7)
allow(server_two).to receive(:logical_session_timeout).and_return(3)
end
it 'returns the minimum' do
expect(cluster.logical_session_timeout).to be(3)
end
end
context 'when no servers have a logical session timeout value' do
before do
allow(server_one).to receive(:logical_session_timeout).and_return(nil)
allow(server_two).to receive(:logical_session_timeout).and_return(nil)
end
it 'returns nil' do
expect(cluster.logical_session_timeout).to be(nil)
end
end
end
describe '#cluster_time' do
let(:operation) do
client.command(ping: 1)
end
let(:operation_with_session) do
client.command({ ping: 1 }, session: session)
end
let(:second_operation) do
client.command({ ping: 1 }, session: session)
end
it_behaves_like 'an operation updating cluster time'
end
describe '#update_cluster_time' do
let(:cluster) do
described_class.new(ADDRESSES, monitoring, TEST_OPTIONS.merge(heartbeat_frequency: 1000))
end
let(:result) do
double('result', cluster_time: cluster_time_doc)
end
context 'when the cluster_time variable is nil' do
before do
cluster.instance_variable_set(:@cluster_time, nil)
cluster.update_cluster_time(result)
end
context 'when the cluster time received is nil' do
let(:cluster_time_doc) do
nil
end
it 'does not set the cluster_time variable' do
expect(cluster.cluster_time).to be_nil
end
end
context 'when the cluster time received is not nil' do
let(:cluster_time_doc) do
BSON::Document.new(Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1))
end
it 'sets the cluster_time variable to the cluster time doc' do
expect(cluster.cluster_time).to eq(cluster_time_doc)
end
end
end
context 'when the cluster_time variable has a value' do
before do
cluster.instance_variable_set(:@cluster_time, BSON::Document.new(
Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1)))
cluster.update_cluster_time(result)
end
context 'when the cluster time received is nil' do
let(:cluster_time_doc) do
nil
end
it 'does not update the cluster_time variable' do
expect(cluster.cluster_time).to eq(BSON::Document.new(
Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1)))
end
end
context 'when the cluster time received is not nil' do
context 'when the cluster time received is greater than the cluster_time variable' do
let(:cluster_time_doc) do
BSON::Document.new(Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 2))
end
it 'sets the cluster_time variable to the cluster time' do
expect(cluster.cluster_time).to eq(cluster_time_doc)
end
end
context 'when the cluster time received is less than the cluster_time variable' do
let(:cluster_time_doc) do
BSON::Document.new(Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(0, 1))
end
it 'does not set the cluster_time variable to the cluster time' do
expect(cluster.cluster_time).to eq(BSON::Document.new(
Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1)))
end
end
context 'when the cluster time received is equal to the cluster_time variable' do
let(:cluster_time_doc) do
BSON::Document.new(Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1))
end
it 'does not change the cluster_time variable' do
expect(cluster.cluster_time).to eq(BSON::Document.new(
Mongo::Cluster::CLUSTER_TIME => BSON::Timestamp.new(1, 1)))
end
end
end
end
end
end
|