public void PrimaryPreferred_should_select_the_primary_when_it_exists()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.PrimaryPreferred);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _primary });
        }
        public void Secondary_should_select_only_secondaries_when_they_match_the_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary1 });
        }
        public void SecondaryPreferred_should_select_the_primary_when_no_secondaries_exist_regardless_of_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.SecondaryPreferred, new[] { new TagSet(new[] { new Tag("a", "2") }) }));

            var result = subject.SelectServers(_description, new[] { _primary, _secondary1 }).ToList();

            result.Should().BeEquivalentTo(new object[] { _primary });
        }
        public void Primary_should_select_none_when_no_primary_exists()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Primary);

            var result = subject.SelectServers(_description, new [] { _secondary1, _secondary2 }).ToList();

            result.Should().BeEmpty();
        }
        public void Nearest_should_select_all_the_servers()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Nearest);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(_description.Servers);
        }
        public void SecondaryPreferred_should_select_the_primary_when_no_secondaries_exist()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.SecondaryPreferred);

            var result = subject.SelectServers(_description, new [] { _primary }).ToList();

            result.Should().BeEquivalentTo(new[] { _primary });
        }
        public void PrimaryPreferred_should_select_the_secondaries_when_no_primary_exists_when_tags_exist()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.PrimaryPreferred, new[] { new TagSet(new[] { new Tag("a", "2") }) }));

            var result = subject.SelectServers(_description, new[] { _secondary1, _secondary2 }).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary2 });
        }
        public void Nearest_should_select_all_the_servers_respecting_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Nearest, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new [] { _primary, _secondary1 });
        }
        public void Secondary_should_select_a_secondary()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Secondary);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary1, _secondary2 });
        }
        public void Primary_should_select_the_primary()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Primary);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _primary });
        }
        public void Nearest_should_select_all_the_servers()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Nearest);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(_description.Servers);
        }
        public void Secondary_should_select_a_secondary()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Secondary);

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary1, _secondary2 });
        }
        public void Primary_should_select_none_when_no_primary_exists()
        {
            var subject = new ReadPreferenceServerSelector(ReadPreference.Primary);

            var result = subject.SelectServers(_description, new [] { _secondary1, _secondary2 }).ToList();

            result.Should().BeEmpty();
        }
        public void PrimaryPreferred_should_select_the_primary_regardless_of_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.PrimaryPreferred, new[] { new TagSet(new[] { new Tag("a", "2") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _primary });
        }
        public void Nearest_should_select_all_the_servers_respecting_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Nearest, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new [] { _primary, _secondary1 });
        }
        public void PrimaryPreferred_should_select_the_primary_when_it_exists_regardless_of_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.PrimaryPreferred, new[] { new TagSet(new[] { new Tag("a", "2") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _primary });
        }
        public void Should_select_nothing_when_attempting_to_match_tags_and_servers_do_not_have_tags()
        {
            var clusterId = new ClusterId();
            var primary   = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), ServerType.ReplicaSetPrimary);
            var secondary = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary);

            var description = new ClusterDescription(
                clusterId,
                ClusterType.ReplicaSet,
                new[] { primary, secondary });

            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(description, description.Servers).ToList();

            result.Should().BeEmpty();
        }
        public void ReadPreference_should_be_ignored_when_directly_connected_with_a_server()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Primary));

            var clusterId = new ClusterId();
            var server    = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary);

            var description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Direct,
                ClusterType.ReplicaSet,
                new[] { server });

            var result = subject.SelectServers(description, description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { server });
        }
Beispiel #19
0
        // methods
        /// <inheritdoc/>
        public IEnumerable <ServerDescription> SelectServers(ClusterDescription cluster, IEnumerable <ServerDescription> servers)
        {
            if (cluster.IsDirectConnection)
            {
                return(servers);
            }

            var serversList = servers.ToList(); // avoid multiple enumeration

            if (CanUseSecondaries(cluster, serversList))
            {
                var readPreferenceSelector = new ReadPreferenceServerSelector(_mayUseSecondary.ReadPreference);
                return(readPreferenceSelector.SelectServers(cluster, serversList));
            }

            if (_mayUseSecondary != null)
            {
                _mayUseSecondary.EffectiveReadPreference = ReadPreference.Primary; // fallback to primary
            }
            return(serversList.Where(x => x.Type.IsWritable()));
        }
 private async Task<IReadBindingHandle> GetSingleServerReadBindingAsync(CancellationToken cancellationToken)
 {
     var readPreference = _options.ReadPreference ?? _database.Settings.ReadPreference;
     var selector = new ReadPreferenceServerSelector(readPreference);
     var server = await _cluster.SelectServerAsync(selector, cancellationToken).ConfigureAwait(false);
     var binding = new SingleServerReadBinding(server, readPreference);
     return new ReadBindingHandle(binding);
 }
        public void Secondary_should_select_only_secondaries_when_they_match_the_tags()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(_description, _description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary1 });
        }
        public void Should_select_nothing_when_attempting_to_match_tags_and_servers_do_not_have_tags()
        {
            var clusterId = new ClusterId();
            var primary = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), ServerType.ReplicaSetPrimary);
            var secondary = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary);

            var description = new ClusterDescription(
                clusterId,
                ClusterType.ReplicaSet,
                new[] { primary, secondary });

            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Secondary, new[] { new TagSet(new[] { new Tag("a", "1") }) }));

            var result = subject.SelectServers(description, description.Servers).ToList();

            result.Should().BeEmpty();
        }
        public void PrimaryPreferred_should_select_the_secondaries_when_no_primary_exists_when_tags_exist()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.PrimaryPreferred, new[] { new TagSet(new[] { new Tag("a", "2") }) }));

            var result = subject.SelectServers(_description, new[] { _secondary1, _secondary2 }).ToList();

            result.Should().BeEquivalentTo(new[] { _secondary2 });
        }
        public void ReadPreference_should_be_ignored_when_directly_connected_with_a_server()
        {
            var subject = new ReadPreferenceServerSelector(new ReadPreference(ReadPreferenceMode.Primary));

            var clusterId = new ClusterId();
            var server = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary);

            var description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Direct,
                ClusterType.ReplicaSet,
                new[] { server });

            var result = subject.SelectServers(description, description.Servers).ToList();

            result.Should().BeEquivalentTo(new[] { server });
        }
 private IReadBindingHandle GetSingleServerReadBinding(CancellationToken cancellationToken)
 {
     var readPreference = _options.ReadPreference ?? _database.Settings.ReadPreference;
     var selector = new ReadPreferenceServerSelector(readPreference);
     var server = _cluster.SelectServer(selector, cancellationToken);
     var binding = new SingleServerReadBinding(server, readPreference);
     return new ReadBindingHandle(binding);
 }