|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +# Create a client with all possible configurations (forcing/discovering each |
| 4 | +# topology type) and ensure the resulting client is usable. |
| 5 | +describe 'Client construction' do |
| 6 | + let(:base_options) do |
| 7 | + SpecConfig.instance.test_options.merge( |
| 8 | + database: SpecConfig.instance.test_db, |
| 9 | + user: SpecConfig.instance.test_user.name, |
| 10 | + password: SpecConfig.instance.test_user.password) |
| 11 | + end |
| 12 | + |
| 13 | + context 'in single topology' do |
| 14 | + require_topology :single |
| 15 | + |
| 16 | + it 'discovers standalone' do |
| 17 | + options = base_options.dup |
| 18 | + options.delete(:connect) |
| 19 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 20 | + options) |
| 21 | + client['client_construction'].insert_one(test: 1) |
| 22 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single) |
| 23 | + expect(client.options[:connect]).to be nil |
| 24 | + end |
| 25 | + |
| 26 | + it 'connects directly' do |
| 27 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 28 | + base_options.merge(connect: :direct)) |
| 29 | + client['client_construction'].insert_one(test: 1) |
| 30 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single) |
| 31 | + expect(client.options[:connect]).to eq :direct |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + context 'in replica set topology' do |
| 36 | + require_topology :replica_set |
| 37 | + |
| 38 | + it 'discovers replica set' do |
| 39 | + options = base_options.dup |
| 40 | + options.delete(:connect) |
| 41 | + options.delete(:replica_set) |
| 42 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 43 | + options) |
| 44 | + client['client_construction'].insert_one(test: 1) |
| 45 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::ReplicaSetWithPrimary) |
| 46 | + expect(client.options[:connect]).to be nil |
| 47 | + expect(client.options[:replica_set]).to be nil |
| 48 | + end |
| 49 | + |
| 50 | + it 'forces replica set' do |
| 51 | + replica_set_name = ClusterConfig.instance.replica_set_name |
| 52 | + expect(replica_set_name).not_to be nil |
| 53 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 54 | + base_options.merge(connect: :replica_set, |
| 55 | + replica_set: replica_set_name)) |
| 56 | + client['client_construction'].insert_one(test: 1) |
| 57 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::ReplicaSetWithPrimary) |
| 58 | + expect(client.options[:connect]).to be :replica_set |
| 59 | + expect(client.options[:replica_set]).to eq(replica_set_name) |
| 60 | + end |
| 61 | + |
| 62 | + it 'connects directly' do |
| 63 | + primary_address = ClusterConfig.instance.primary_address |
| 64 | + client = ClientRegistry.instance.new_local_client([primary_address], |
| 65 | + base_options.merge(connect: :direct)) |
| 66 | + client['client_construction'].insert_one(test: 1) |
| 67 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single) |
| 68 | + expect(client.options[:connect]).to eq :direct |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context 'in sharded topology' do |
| 73 | + require_topology :sharded |
| 74 | + |
| 75 | + it 'connects to sharded cluster' do |
| 76 | + options = base_options.dup |
| 77 | + options.delete(:connect) |
| 78 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 79 | + base_options.merge(connect: :sharded)) |
| 80 | + client['client_construction'].insert_one(test: 1) |
| 81 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Sharded) |
| 82 | + expect(client.options[:connect]).to be :sharded |
| 83 | + end |
| 84 | + |
| 85 | + it 'connects directly' do |
| 86 | + primary_address = ClusterConfig.instance.primary_address |
| 87 | + client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], |
| 88 | + base_options.merge(connect: :direct)) |
| 89 | + client['client_construction'].insert_one(test: 1) |
| 90 | + expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single) |
| 91 | + expect(client.options[:connect]).to eq :direct |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments