Example #1
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();
        }
Example #2
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();
        }
Example #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<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();
        }
Example #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();
        }