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
|
# frozen_string_literal: true
# rubocop:todo all
require 'spec_helper'
describe 'Server selector' do
require_no_linting
let(:selector) { Mongo::ServerSelector::Primary.new }
let(:client) { authorized_client }
let(:cluster) { client.cluster }
describe '#select_server' do
# These tests operate on specific servers, and don't work in a multi
# shard cluster where multiple servers are equally eligible
require_no_multi_mongos
let(:result) { selector.select_server(cluster) }
it 'selects' do
expect(result).to be_a(Mongo::Server)
end
context 'no servers in the cluster' do
let(:client) { new_local_client_nmio([], server_selection_timeout: 2) }
it 'raises NoServerAvailable with a message explaining the situation' do
expect do
result
end.to raise_error(Mongo::Error::NoServerAvailable, "Cluster has no addresses, and therefore will never have a server")
end
it 'does not wait for server selection timeout' do
start_time = Mongo::Utils.monotonic_time
expect do
result
end.to raise_error(Mongo::Error::NoServerAvailable)
time_passed = Mongo::Utils.monotonic_time - start_time
expect(time_passed).to be < 1
end
end
context 'client is closed' do
context 'there is a known primary' do
before do
client.cluster.next_primary
client.close
expect(client.cluster.connected?).to be false
end
it 'returns the primary for BC reasons' do
expect(result).to be_a(Mongo::Server)
end
end
context 'there is no known primary' do
require_topology :single, :replica_set, :sharded
before do
primary_server = client.cluster.next_primary
client.close
expect(client.cluster.connected?).to be false
primary_server.unknown!
end
context 'non-lb' do
require_topology :single, :replica_set, :sharded
it 'raises NoServerAvailable with a message explaining the situation' do
expect do
result
end.to raise_error(Mongo::Error::NoServerAvailable, /The cluster is disconnected \(client may have been closed\)/)
end
end
context 'lb' do
require_topology :load_balanced
it 'returns the load balancer' do
expect(result).to be_a(Mongo::Server)
result.should be_load_balancer
end
end
end
end
context 'monitoring thread is dead' do
require_topology :single, :replica_set, :sharded
before do
client.cluster.servers.each do |server|
server.monitor.instance_variable_get('@thread').kill
end
server = client.cluster.next_primary
if server
server.instance_variable_set('@description', Mongo::Server::Description.new({}))
end
end
it 'raises NoServerAvailable with a message explaining the situation' do
expect do
result
end.to raise_error(Mongo::Error::NoServerAvailable, /The following servers have dead monitor threads/)
end
end
end
end
|