Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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();
        }
Ejemplo n.º 4
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();
        }
        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, _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();

            var selected = await subject.SelectServerAsync(middleSelector, CancellationToken.None);
            ((DnsEndPoint)selected.EndPoint).Port.Should().Be(27020);
            _capturedEvents.Next().Should().BeOfType<ClusterSelectingServerEvent>();
            _capturedEvents.Next().Should().BeOfType<ClusterSelectedServerEvent>();
            _capturedEvents.Any().Should().BeFalse();
        }
Ejemplo n.º 6
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<EndPoint>()))
                .Returns((ClusterId _, 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();
        }