Esempio n. 1
0
        public void SelectServer_should_return_a_server_if_one_matches(
            [Values(false, true)]
            bool async)
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected);
            _capturedEvents.Clear();

            var selector = new DelegateServerSelector((c, s) => s);

            IServer result;

            if (async)
            {
                result = subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.SelectServer(selector, CancellationToken.None);
            }

            result.Should().NotBeNull();

            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 2
0
        public void SelectServer_should_throw_if_the_matched_server_cannot_be_found_and_no_others_matched(
            [Values(false, true)]
            bool async)
        {
            var subject = CreateSubject(serverSelectionTimeout: TimeSpan.FromMilliseconds(10));

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected);
            subject.RemoveServer(connected.EndPoint);
            _capturedEvents.Clear();

            var selector = new DelegateServerSelector((c, s) => s);

            Action act;

            if (async)
            {
                act = () => subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.SelectServer(selector, CancellationToken.None);
            }

            act.ShouldThrow <TimeoutException>();

            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerFailedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 3
0
        public void SelectServer_should_throw_if_no_servers_match(
            [Values(false, true)]
            bool async)
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected);
            _capturedEvents.Clear();

            var selector = new DelegateServerSelector((c, s) => Enumerable.Empty <ServerDescription>());

            Action act;

            if (async)
            {
                act = () => subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.SelectServer(selector, CancellationToken.None);
            }

            act.ShouldThrow <TimeoutException>();

            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerFailedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 4
0
        public void SelectServer_should_return_second_server_if_first_cannot_be_found(
            [Values(false, true)]
            bool async)
        {
            var subject = CreateSubject();

            subject.Initialize();

            _serverFactory.CreateServer(null, null).ReturnsForAnyArgs((IClusterableServer)null, Substitute.For <IClusterableServer>());

            var connected1 = ServerDescriptionHelper.Connected(subject.Description.ClusterId);
            var connected2 = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected1, connected2);
            _capturedEvents.Clear();

            var selector = new DelegateServerSelector((c, s) => s);

            IServer result;

            if (async)
            {
                result = subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.SelectServer(selector, CancellationToken.None);
            }

            result.Should().NotBeNull();

            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 5
0
        public void SelectServer_should_throw_if_any_servers_are_incompatible(int min, int max, bool async)
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, wireVersionRange: new Range <int>(min, max));

            subject.SetServerDescriptions(connected);
            _capturedEvents.Clear();

            var selector = new DelegateServerSelector((c, s) => s);

            Action act;

            if (async)
            {
                act = () => subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                act = () => subject.SelectServer(selector, CancellationToken.None);
            }

            act.ShouldThrow <MongoIncompatibleDriverException>();

            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerFailedEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 6
0
        public async Task SelectServerAsync_should_apply_both_pre_and_post_server_selectors()
        {
            _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(ci =>
            {
                var endPoint = ci.Arg <EndPoint>();
                var server   = Substitute.For <IClusterableServer>();
                server.EndPoint.Returns(endPoint);
                return(server);
            });

            var preSelector    = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017));
            var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018));
            var postSelector   = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019));

            var settings = new ClusterSettings(
                preServerSelector: preSelector,
                postServerSelector: postSelector);

            var subject = new StubCluster(settings, _serverFactory, _clusterListener);

            subject.Initialize();

            subject.SetServerDescriptions(
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020)));

            var selected = await subject.SelectServerAsync(middleSelector, CancellationToken.None);

            ((DnsEndPoint)selected.EndPoint).Port.Should().Be(27020);
        }
Esempio n. 7
0
        public void State_should_be_connected_if_any_server_is_connected()
        {
            var connected = ServerDescriptionHelper.Connected(new ClusterId(1));
            var subject   = new ClusterDescription(new ClusterId(1), ClusterConnectionMode.Standalone, ClusterType.Standalone, new[] { __serverDescription1, connected });

            subject.State.Should().Be(ClusterState.Connected);
        }
        public void SelectServerAsync_should_keep_trying_to_match_by_waiting_on_cluster_description_changes()
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connecting = ServerDescriptionHelper.Disconnected(subject.Description.ClusterId);
            var connected  = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connecting);

            Task.Run(() =>
            {
                var descriptions = new Queue <ServerDescription>(new[] { connecting, connecting, connecting, connected });
                while (descriptions.Count > 0)
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(20));
                    var next = descriptions.Dequeue();
                    subject.SetServerDescriptions(next);
                }
            });

            var selector = new DelegateServerSelector((c, s) => s);

            var result = subject.SelectServerAsync(selector, Timeout.InfiniteTimeSpan, CancellationToken.None).Result;

            result.Should().NotBeNull();
        }
Esempio n. 9
0
        public void State_should_be_connected_if_any_server_is_connected()
        {
            var connected = ServerDescriptionHelper.Connected(new ClusterId(1));

#pragma warning disable CS0618 // Type or member is obsolete
            var subject = new ClusterDescription(new ClusterId(1), ClusterConnectionMode.Standalone, ClusterType.Standalone, new[] { __serverDescription1, connected });
#pragma warning restore CS0618 // Type or member is obsolete

            subject.State.Should().Be(ClusterState.Connected);
        }
Esempio n. 10
0
        public void ServerDescription_should_return_description_of_server()
        {
            var subject = new ServerConnectionSource(_server);

            var desc = ServerDescriptionHelper.Disconnected(new ClusterId());

            _server.Description.Returns(desc);

            var result = subject.ServerDescription;

            result.Should().BeSameAs(desc);
        }
Esempio n. 11
0
        public void ServerDescription_should_return_description_of_server()
        {
            var subject = new ServerChannelSource(_mockServer.Object);

            var desc = ServerDescriptionHelper.Disconnected(new ClusterId());

            _mockServer.SetupGet(s => s.Description).Returns(desc);

            var result = subject.ServerDescription;

            result.Should().BeSameAs(desc);
        }
        public void Setup()
        {
            var clusterId = new ClusterId();

            _primary    = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), ServerType.ReplicaSetPrimary, new TagSet(new [] { new Tag("a", "1") }));
            _secondary1 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary, new TagSet(new [] { new Tag("a", "1") }));
            _secondary2 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), ServerType.ReplicaSetSecondary, new TagSet(new[] { new Tag("a", "2") }));

            _description = new ClusterDescription(
                clusterId,
                ClusterType.ReplicaSet,
                new[] { _primary, _secondary1, _secondary2 });
        }
        public void DescriptionChanged_should_be_raised_when_the_description_changes()
        {
            int count   = 0;
            var subject = CreateSubject();

            subject.Initialize();
            subject.DescriptionChanged += (o, e) => count++;

            subject.SetServerDescriptions(ServerDescriptionHelper.Connected(subject.Description.ClusterId));
            subject.SetServerDescriptions(ServerDescriptionHelper.Connected(subject.Description.ClusterId, averageRoundTripTime: TimeSpan.FromMilliseconds(10)));
            subject.SetServerDescriptions(ServerDescriptionHelper.Connected(subject.Description.ClusterId, averageRoundTripTime: TimeSpan.FromMilliseconds(13)));

            count.Should().Be(3);
        }
Esempio n. 14
0
        public void Setup()
        {
            var clusterId = new ClusterId();

            _description = new ClusterDescription(
                clusterId,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019)),
            });
        }
        public void Setup()
        {
            var clusterId = new ClusterId();

            _description = new ClusterDescription(
                clusterId,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), averageRoundTripTime: TimeSpan.FromMilliseconds(10)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), averageRoundTripTime: TimeSpan.FromMilliseconds(30)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), averageRoundTripTime: TimeSpan.FromMilliseconds(20))
            });
        }
Esempio n. 16
0
        public void SelectServer_should_call_custom_selector(
            [Values(true, false)] bool withEligibleServers,
            [Values(true, false)] bool async)
        {
            int numberOfCustomServerSelectorCalls = 0;
            var customServerSelector = new DelegateServerSelector((c, s) =>
            {
                numberOfCustomServerSelectorCalls++;
                return(s.Skip(1));
            });

            var settings = _settings.With(postServerSelector: customServerSelector);
            var subject  = new StubCluster(settings, _mockServerFactory.Object, _capturedEvents);

            subject.Initialize();
            subject.SetServerDescriptions(
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020)));
            _capturedEvents.Clear();

            if (withEligibleServers)
            {
                var selectedServer = SelectServerAttempt(
                    subject,
                    new DelegateServerSelector((c, s) => s), // do not filter servers
                    async);

                var selectedServerPort = ((DnsEndPoint)selectedServer.EndPoint).Port;
                selectedServerPort.Should().Be(27020);
                _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
                _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            }
            else
            {
                var exception = Record.Exception(
                    () =>
                    SelectServerAttempt(
                        subject,
                        new DelegateServerSelector((c, s) => new ServerDescription[0]),     // no eligible servers
                        async));

                exception.Should().BeOfType <TimeoutException>();
                _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
                _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerFailedEvent>();
            }

            numberOfCustomServerSelectorCalls.Should().Be(1);
            _capturedEvents.Any().Should().BeFalse();
        }
        public LatencyLimitingServerSelectorTests()
        {
            var clusterId = new ClusterId();

            _description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), averageRoundTripTime: TimeSpan.FromMilliseconds(10)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), averageRoundTripTime: TimeSpan.FromMilliseconds(30)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), averageRoundTripTime: TimeSpan.FromMilliseconds(20))
            });
        }
Esempio n. 18
0
        public EndPointServerSelectorTests()
        {
            var clusterId = new ClusterId();

            _description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019)),
            });
        }
        public void SelectServerAsync_should_throw_if_no_servers_match()
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected);

            var selector = new DelegateServerSelector((c, s) => Enumerable.Empty <ServerDescription>());

            Action act = () => subject.SelectServerAsync(selector, TimeSpan.FromMilliseconds(50), CancellationToken.None).Wait();

            act.ShouldThrow <TimeoutException>();
        }
        public ReadPreferenceServerSelectorTests()
        {
            var clusterId = new ClusterId();

            _primary    = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), ServerType.ReplicaSetPrimary, new TagSet(new[] { new Tag("a", "1") }));
            _secondary1 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), ServerType.ReplicaSetSecondary, new TagSet(new[] { new Tag("a", "1") }));
            _secondary2 = ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), ServerType.ReplicaSetSecondary, new TagSet(new[] { new Tag("a", "2") }));

#pragma warning disable CS0618 // Type or member is obsolete
            _description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.ReplicaSet,
                ClusterType.ReplicaSet,
                new[] { _primary, _secondary1, _secondary2 });
#pragma warning restore CS0618 // Type or member is obsolete
        }
        public void SelectServerAsync_should_throw_if_any_servers_are_incompatible()
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, wireVersionRange: new Range <int>(10, 12));

            subject.SetServerDescriptions(connected);

            var selector = new DelegateServerSelector((c, s) => s);

            Action act = () => subject.SelectServerAsync(selector, TimeSpan.FromMilliseconds(50), CancellationToken.None).Wait();

            act.ShouldThrow <MongoException>();
        }
        public void SelectServerAsync_should_return_a_server_if_one_matches()
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connected);

            var selector = new DelegateServerSelector((c, s) => s);

            var result = subject.SelectServerAsync(selector, Timeout.InfiniteTimeSpan, CancellationToken.None).Result;

            result.Should().NotBeNull();
        }
Esempio n. 23
0
        public void SelectServer_should_apply_both_pre_and_post_server_selectors(
            [Values(false, true)]
            bool async)
        {
            _mockServerFactory.Setup(f => f.CreateServer(It.IsAny <ClusterId>(), It.IsAny <IClusterClock>(), It.IsAny <EndPoint>()))
            .Returns((ClusterId _, IClusterClock clusterClock, EndPoint endPoint) =>
            {
                var mockServer = new Mock <IClusterableServer>();
                mockServer.SetupGet(s => s.EndPoint).Returns(endPoint);
                return(mockServer.Object);
            });

            var preSelector    = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017));
            var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018));
            var postSelector   = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019));

            var settings = new ClusterSettings(
                preServerSelector: preSelector,
                postServerSelector: postSelector);

            var subject = new StubCluster(settings, _mockServerFactory.Object, _capturedEvents);

            subject.Initialize();

            subject.SetServerDescriptions(
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020)));
            _capturedEvents.Clear();

            IServer result;

            if (async)
            {
                result = subject.SelectServerAsync(middleSelector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.SelectServer(middleSelector, CancellationToken.None);
            }

            ((DnsEndPoint)result.EndPoint).Port.Should().Be(27020);
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 24
0
        public void SelectServer_should_keep_trying_to_match_by_waiting_on_cluster_description_changes(
            [Values(false, true)]
            bool async)
        {
            var subject = CreateSubject();

            subject.Initialize();

            var connecting = ServerDescriptionHelper.Disconnected(subject.Description.ClusterId);
            var connected  = ServerDescriptionHelper.Connected(subject.Description.ClusterId);

            subject.SetServerDescriptions(connecting);
            _capturedEvents.Clear();

            Task.Run(() =>
            {
                var descriptions = new Queue <ServerDescription>(new[] { connecting, connecting, connecting, connected });
                while (descriptions.Count > 0)
                {
                    Thread.Sleep(TimeSpan.FromMilliseconds(20));
                    var next = descriptions.Dequeue();
                    subject.SetServerDescriptions(next);
                }
            });

            var selector = new DelegateServerSelector((c, s) => s);

            IServer result;

            if (async)
            {
                result = subject.SelectServerAsync(selector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.SelectServer(selector, CancellationToken.None);
            }

            result.Should().NotBeNull();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterDescriptionChangedEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 25
0
        public void SelectServer_should_apply_both_pre_and_post_server_selectors(
            [Values(false, true)]
            bool async)
        {
            _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(ci =>
            {
                var endPoint = ci.Arg <EndPoint>();
                var server   = Substitute.For <IClusterableServer>();
                server.EndPoint.Returns(endPoint);
                return(server);
            });

            var preSelector    = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27017));
            var middleSelector = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27018));
            var postSelector   = new DelegateServerSelector((cd, sds) => sds.Where(x => ((DnsEndPoint)x.EndPoint).Port != 27019));

            var settings = new ClusterSettings(
                preServerSelector: preSelector,
                postServerSelector: postSelector);

            var subject = new StubCluster(settings, _serverFactory, _capturedEvents);

            subject.Initialize();

            subject.SetServerDescriptions(
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27019)),
                ServerDescriptionHelper.Connected(subject.Description.ClusterId, new DnsEndPoint("localhost", 27020)));
            _capturedEvents.Clear();

            IServer result;

            if (async)
            {
                result = subject.SelectServerAsync(middleSelector, CancellationToken.None).GetAwaiter().GetResult();
            }
            else
            {
                result = subject.SelectServer(middleSelector, CancellationToken.None);
            }

            ((DnsEndPoint)result.EndPoint).Port.Should().Be(27020);
            _capturedEvents.Next().Should().BeOfType <ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType <ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Esempio n. 26
0
        public CompositeServerSelectorTests()
        {
            var clusterId = new ClusterId();

#pragma warning disable CS0618 // Type or member is obsolete
            _description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019)),
            });
#pragma warning restore CS0618 // Type or member is obsolete
        }
Esempio n. 27
0
        public DelegateServerSelectorTests()
        {
            var clusterId = new ClusterId();

#pragma warning disable CS0618 // Type or member is obsolete
            _description = new ClusterDescription(
                clusterId,
                ClusterConnectionMode.Automatic,
                ClusterType.Unknown,
                new[]
            {
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27017), averageRoundTripTime: TimeSpan.FromMilliseconds(10)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27018), averageRoundTripTime: TimeSpan.FromMilliseconds(30)),
                ServerDescriptionHelper.Connected(clusterId, new DnsEndPoint("localhost", 27019), averageRoundTripTime: TimeSpan.FromMilliseconds(20))
            });
#pragma warning restore CS0618 // Type or member is obsolete
        }
        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 });
        }
        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 Description_should_not_contain_any_servers_if_the_provided_server_is_not_of_the_required_type(ClusterConnectionMode connectionMode, ServerType serverType)
        {
            var server = Substitute.For <IClusterableServer>();

            _serverFactory.CreateServer(null, null).ReturnsForAnyArgs(server);

            _settings = _settings.WithConnectionMode(connectionMode);

            var subject = CreateSubject();

            subject.Initialize();

            var connected = ServerDescriptionHelper.Connected(subject.Description.ClusterId, serverType: serverType);

            server.DescriptionChanged += Raise.EventWith(new object(), new ServerDescriptionChangedEventArgs(connected, connected));

            subject.Description.Servers.Should().BeEmpty();
        }