コード例 #1
0
        public void ToClusterKey_should_copy_relevant_values()
        {
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var servers     = new[] { new MongoServerAddress("localhost") };
            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols        = SslProtocols.Tls
            };

            var subject = new MongoClientSettings
            {
                ApplicationName    = "app",
                ConnectionMode     = ConnectionMode.Direct,
                ConnectTimeout     = TimeSpan.FromSeconds(1),
                Credentials        = credentials,
                GuidRepresentation = GuidRepresentation.Standard,
                HeartbeatInterval  = TimeSpan.FromSeconds(7),
                HeartbeatTimeout   = TimeSpan.FromSeconds(8),
                IPv6 = true,
                MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize = 10,
                MinConnectionPoolSize = 5,
                ReplicaSetName        = "rs",
                LocalThreshold        = TimeSpan.FromMilliseconds(20),
                Servers = servers,
                ServerSelectionTimeout = TimeSpan.FromSeconds(6),
                SocketTimeout          = TimeSpan.FromSeconds(4),
                SslSettings            = sslSettings,
                UseSsl = true,
                VerifySslCertificate = true,
                WaitQueueSize        = 20,
                WaitQueueTimeout     = TimeSpan.FromSeconds(5)
            };

            var result = subject.ToClusterKey();

            result.ApplicationName.Should().Be(subject.ApplicationName);
            result.ConnectionMode.Should().Be(subject.ConnectionMode);
            result.ConnectTimeout.Should().Be(subject.ConnectTimeout);
            result.Credentials.Should().Equal(subject.Credentials);
            result.HeartbeatInterval.Should().Be(subject.HeartbeatInterval);
            result.HeartbeatTimeout.Should().Be(subject.HeartbeatTimeout);
            result.IPv6.Should().Be(subject.IPv6);
            result.MaxConnectionIdleTime.Should().Be(subject.MaxConnectionIdleTime);
            result.MaxConnectionLifeTime.Should().Be(subject.MaxConnectionLifeTime);
            result.MaxConnectionPoolSize.Should().Be(subject.MaxConnectionPoolSize);
            result.MinConnectionPoolSize.Should().Be(subject.MinConnectionPoolSize);
            result.ReplicaSetName.Should().Be(subject.ReplicaSetName);
            result.LocalThreshold.Should().Be(subject.LocalThreshold);
            result.Servers.Should().Equal(subject.Servers);
            result.ServerSelectionTimeout.Should().Be(subject.ServerSelectionTimeout);
            result.SocketTimeout.Should().Be(subject.SocketTimeout);
            result.SslSettings.Should().Be(subject.SslSettings);
            result.UseSsl.Should().Be(subject.UseSsl);
            result.VerifySslCertificate.Should().Be(subject.VerifySslCertificate);
            result.WaitQueueSize.Should().Be(subject.WaitQueueSize);
            result.WaitQueueTimeout.Should().Be(subject.WaitQueueTimeout);
        }
コード例 #2
0
        public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings()
        {
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var servers     = new[] { new MongoServerAddress("localhost"), new MongoServerAddress("127.0.0.1", 30000), new MongoServerAddress("[::1]", 27018) };

            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols        = SslProtocols.Tls
            };

            var clientSettings = new MongoClientSettings
            {
                ApplicationName    = "app1",
                ConnectionMode     = ConnectionMode.ReplicaSet,
                ConnectTimeout     = TimeSpan.FromSeconds(1),
                Credentials        = credentials,
                GuidRepresentation = GuidRepresentation.Standard,
                HeartbeatInterval  = TimeSpan.FromSeconds(7),
                HeartbeatTimeout   = TimeSpan.FromSeconds(8),
                IPv6 = true,
                MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize = 10,
                MinConnectionPoolSize = 5,
                ReplicaSetName        = "rs",
                LocalThreshold        = TimeSpan.FromMilliseconds(20),
                Servers = servers,
                ServerSelectionTimeout = TimeSpan.FromSeconds(5),
                SocketTimeout          = TimeSpan.FromSeconds(4),
                SslSettings            = sslSettings,
                UseSsl = true,
                VerifySslCertificate = true,
                WaitQueueSize        = 20,
                WaitQueueTimeout     = TimeSpan.FromSeconds(6)
            };

            var subject = new ClusterRegistry();

            using (var cluster = subject.GetOrCreateCluster(clientSettings.ToClusterKey()))
            {
                var endPoints = new EndPoint[]
                {
                    new DnsEndPoint("localhost", 27017),
                    new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000),
                    new IPEndPoint(IPAddress.Parse("[::1]"), 27018)
                };
                cluster.Settings.ConnectionMode.Should().Be(ClusterConnectionMode.ReplicaSet);
                cluster.Settings.EndPoints.Equals(endPoints);
                cluster.Settings.ReplicaSetName.Should().Be("rs");
                cluster.Settings.ServerSelectionTimeout.Should().Be(clientSettings.ServerSelectionTimeout);
                cluster.Settings.PostServerSelector.Should().NotBeNull().And.Subject.Should().BeOfType <LatencyLimitingServerSelector>();
                cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(20);

                cluster.Description.Servers.Select(s => s.EndPoint).Should().Contain(endPoints);

                // TODO: don't know how to test the rest of the settings because they are all private to the cluster
            }
        }
コード例 #3
0
        public void UnregisterAndDisposeCluster_should_unregister_and_dispose_the_cluster()
        {
            var subject    = new ClusterRegistry();
            var settings   = new MongoClientSettings();
            var clusterKey = settings.ToClusterKey();
            var cluster    = subject.GetOrCreateCluster(clusterKey);

            subject.UnregisterAndDisposeCluster(cluster);

            subject._registry().Count.Should().Be(0);
            cluster._state().Should().Be(2);
        }
コード例 #4
0
        public void GetOrCreateCluster_should_return_the_same_cluster_if_client_settings_are_equal()
        {
            var clientSettings1 = new MongoClientSettings();
            var clientSettings2 = new MongoClientSettings();

            var subject = new ClusterRegistry();

            using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
                using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
                {
                    cluster2.Should().BeSameAs(cluster1);
                }
        }
コード例 #5
0
        public void GetOrCreateCluster_should_return_a_different_cluster_if_client_settings_are_not_equal()
        {
            var clientSettings1 = new MongoClientSettings();
            var clientSettings2 = new MongoClientSettings()
            {
                IPv6 = true
            };

            var subject = new ClusterRegistry();

            using (var cluster1 = subject.GetOrCreateCluster(clientSettings1.ToClusterKey()))
                using (var cluster2 = subject.GetOrCreateCluster(clientSettings2.ToClusterKey()))
                {
                    cluster2.Should().NotBeSameAs(cluster1);
                }
        }
コード例 #6
0
        private ClusterKey CreateSubject(string notEqualFieldName = null)
        {
            var applicationName    = "app1";
            var connectionMode     = ConnectionMode.Direct;
            var connectTimeout     = TimeSpan.FromSeconds(1);
            var credential         = MongoCredential.CreateMongoCRCredential("source", "username", "password");
            var guidRepresentation = GuidRepresentation.Standard;
            var heartbeatInterval  = TimeSpan.FromSeconds(7);
            var heartbeatTimeout   = TimeSpan.FromSeconds(8);
            var ipv6                   = false;
            var localThreshold         = TimeSpan.FromMilliseconds(20);
            var maxConnectionIdleTime  = TimeSpan.FromSeconds(2);
            var maxConnectionLifeTime  = TimeSpan.FromSeconds(3);
            var maxConnectionPoolSize  = 50;
            var minConnectionPoolSize  = 5;
            var replicaSetName         = "abc";
            var servers                = new[] { new MongoServerAddress("localhost") };
            var serverSelectionTimeout = TimeSpan.FromSeconds(6);
            var socketTimeout          = TimeSpan.FromSeconds(4);
            var sslSettings            = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols        = SslProtocols.Tls
            };
            var useSsl = false;
            var verifySslCertificate = false;
            var waitQueueSize        = 20;
            var waitQueueTimeout     = TimeSpan.FromSeconds(5);

            switch (notEqualFieldName)
            {
            case "ApplicationName": applicationName = "app2"; break;

            case "ConnectionMode": connectionMode = ConnectionMode.ReplicaSet; break;

            case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break;

            case "Credentials": credential = MongoCredential.CreateMongoCRCredential("different", "different", "different"); break;

            case "HeartbeatInterval": heartbeatInterval = TimeSpan.FromSeconds(99); break;

            case "HeartbeatTimeout": heartbeatTimeout = TimeSpan.FromSeconds(99); break;

            case "IPv6": ipv6 = !ipv6; break;

            case "LocalThreshold": localThreshold = TimeSpan.FromMilliseconds(99); break;

            case "MaxConnectionIdleTime": maxConnectionIdleTime = TimeSpan.FromSeconds(99); break;

            case "MaxConnectionLifeTime": maxConnectionLifeTime = TimeSpan.FromSeconds(99); break;

            case "MaxConnectionPoolSize": maxConnectionPoolSize = 99; break;

            case "MinConnectionPoolSize": minConnectionPoolSize = 99; break;

            case "ReplicaSetName": replicaSetName = "different"; break;

            case "Servers": servers = new[] { new MongoServerAddress("different") }; break;

            case "ServerSelectionTimeout": serverSelectionTimeout = TimeSpan.FromSeconds(98); break;

            case "SocketTimeout": socketTimeout = TimeSpan.FromSeconds(99); break;

            case "SslSettings": sslSettings.CheckCertificateRevocation = !sslSettings.CheckCertificateRevocation; break;

            case "UseSsl": useSsl = !useSsl; break;

            case "VerifySslCertificate": verifySslCertificate = !verifySslCertificate; break;

            case "WaitQueueSize": waitQueueSize = 99; break;

            case "WaitQueueTimeout": waitQueueTimeout = TimeSpan.FromSeconds(99); break;
            }

            var clientSettings = new MongoClientSettings
            {
                ApplicationName    = applicationName,
                ConnectionMode     = connectionMode,
                ConnectTimeout     = connectTimeout,
                Credential         = credential,
                GuidRepresentation = guidRepresentation,
                HeartbeatInterval  = heartbeatInterval,
                HeartbeatTimeout   = heartbeatTimeout,
                IPv6 = ipv6,
                MaxConnectionIdleTime = maxConnectionIdleTime,
                MaxConnectionLifeTime = maxConnectionLifeTime,
                MaxConnectionPoolSize = maxConnectionPoolSize,
                MinConnectionPoolSize = minConnectionPoolSize,
                ReplicaSetName        = replicaSetName,
                LocalThreshold        = localThreshold,
                Servers = servers,
                ServerSelectionTimeout = serverSelectionTimeout,
                SocketTimeout          = socketTimeout,
                SslSettings            = sslSettings,
                UseSsl = useSsl,
                VerifySslCertificate = verifySslCertificate,
                WaitQueueSize        = waitQueueSize,
                WaitQueueTimeout     = waitQueueTimeout
            };

            return(clientSettings.ToClusterKey());
        }
コード例 #7
0
        public void ToClusterKey_should_copy_relevant_values()
        {
            var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") };
            var servers = new[] { new MongoServerAddress("localhost") };
            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols = SslProtocols.Ssl3
            };

            var subject = new MongoClientSettings
            {
                ConnectionMode = ConnectionMode.Direct,
                ConnectTimeout = TimeSpan.FromSeconds(1),
                Credentials = credentials,
                GuidRepresentation = GuidRepresentation.Standard,
                IPv6 = true,
                MaxConnectionIdleTime = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize = 10,
                MinConnectionPoolSize = 5,
                ReplicaSetName = "rs",
                LocalThreshold = TimeSpan.FromMilliseconds(20),
                Servers = servers,
                ServerSelectionTimeout = TimeSpan.FromSeconds(6),
                SocketTimeout = TimeSpan.FromSeconds(4),
                SslSettings = sslSettings,
                UseSsl = true,
                VerifySslCertificate = true,
                WaitQueueSize = 20,
                WaitQueueTimeout = TimeSpan.FromSeconds(5)
            };

            var result = subject.ToClusterKey();

            result.ConnectionMode.Should().Be(subject.ConnectionMode);
            result.ConnectTimeout.Should().Be(subject.ConnectTimeout);
            result.Credentials.Should().Equal(subject.Credentials);
            result.IPv6.Should().Be(subject.IPv6);
            result.MaxConnectionIdleTime.Should().Be(subject.MaxConnectionIdleTime);
            result.MaxConnectionLifeTime.Should().Be(subject.MaxConnectionLifeTime);
            result.MaxConnectionPoolSize.Should().Be(subject.MaxConnectionPoolSize);
            result.MinConnectionPoolSize.Should().Be(subject.MinConnectionPoolSize);
            result.ReplicaSetName.Should().Be(subject.ReplicaSetName);
            result.LocalThreshold.Should().Be(subject.LocalThreshold);
            result.Servers.Should().Equal(subject.Servers);
            result.ServerSelectionTimeout.Should().Be(subject.ServerSelectionTimeout);
            result.SocketTimeout.Should().Be(subject.SocketTimeout);
            result.SslSettings.Should().Be(subject.SslSettings);
            result.UseSsl.Should().Be(subject.UseSsl);
            result.VerifySslCertificate.Should().Be(subject.VerifySslCertificate);
            result.WaitQueueSize.Should().Be(subject.WaitQueueSize);
            result.WaitQueueTimeout.Should().Be(subject.WaitQueueTimeout);
        }
コード例 #8
0
        public void ToClusterKey_should_copy_relevant_values()
        {
            var clusterConfigurator = new Action <ClusterBuilder>(b => { });

#pragma warning disable 618
            var credential = MongoCredential.CreateMongoCRCredential("source", "username", "password");
#pragma warning restore 618
            var servers     = new[] { new MongoServerAddress("localhost") };
            var sslSettings = new SslSettings
            {
                CheckCertificateRevocation = true,
                EnabledSslProtocols        = SslProtocols.Tls
            };

            var subject = new MongoClientSettings
            {
                AllowInsecureTls    = false,
                ApplicationName     = "app",
                ClusterConfigurator = clusterConfigurator,
                ConnectionMode      = ConnectionMode.Direct,
                ConnectTimeout      = TimeSpan.FromSeconds(1),
                Credential          = credential,
                HeartbeatInterval   = TimeSpan.FromSeconds(7),
                HeartbeatTimeout    = TimeSpan.FromSeconds(8),
                IPv6                   = true,
                LocalThreshold         = TimeSpan.FromMilliseconds(20),
                MaxConnectionIdleTime  = TimeSpan.FromSeconds(2),
                MaxConnectionLifeTime  = TimeSpan.FromSeconds(3),
                MaxConnectionPoolSize  = 10,
                MinConnectionPoolSize  = 5,
                ReplicaSetName         = "rs",
                Scheme                 = ConnectionStringScheme.MongoDB,
                SdamLogFilename        = "pokédex",
                Servers                = servers,
                ServerSelectionTimeout = TimeSpan.FromSeconds(6),
                SocketTimeout          = TimeSpan.FromSeconds(4),
                SslSettings            = sslSettings,
                UseTls                 = true,
#pragma warning disable 618
                WaitQueueSize = 20,
#pragma warning restore 618
                WaitQueueTimeout = TimeSpan.FromSeconds(5)
            };
#pragma warning disable 618
            if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2)
            {
                subject.GuidRepresentation = GuidRepresentation.Standard;
            }
#pragma warning restore 618

            var result = subject.ToClusterKey();

            result.AllowInsecureTls.Should().Be(subject.AllowInsecureTls);
            result.ApplicationName.Should().Be(subject.ApplicationName);
            result.ClusterConfigurator.Should().BeSameAs(clusterConfigurator);
            result.ConnectionMode.Should().Be(subject.ConnectionMode);
            result.ConnectTimeout.Should().Be(subject.ConnectTimeout);
#pragma warning disable 618
            result.Credentials.Should().Equal(subject.Credentials);
#pragma warning restore
            result.HeartbeatInterval.Should().Be(subject.HeartbeatInterval);
            result.HeartbeatTimeout.Should().Be(subject.HeartbeatTimeout);
            result.IPv6.Should().Be(subject.IPv6);
            result.LocalThreshold.Should().Be(subject.LocalThreshold);
            result.MaxConnectionIdleTime.Should().Be(subject.MaxConnectionIdleTime);
            result.MaxConnectionLifeTime.Should().Be(subject.MaxConnectionLifeTime);
            result.MaxConnectionPoolSize.Should().Be(subject.MaxConnectionPoolSize);
            result.MinConnectionPoolSize.Should().Be(subject.MinConnectionPoolSize);
            result.ReceiveBufferSize.Should().Be(MongoDefaults.TcpReceiveBufferSize);
            result.ReplicaSetName.Should().Be(subject.ReplicaSetName);
            result.Scheme.Should().Be(subject.Scheme);
            result.SdamLogFilename.Should().Be(subject.SdamLogFilename);
            result.SendBufferSize.Should().Be(MongoDefaults.TcpSendBufferSize);
            result.Servers.Should().Equal(subject.Servers);
            result.ServerSelectionTimeout.Should().Be(subject.ServerSelectionTimeout);
            result.SocketTimeout.Should().Be(subject.SocketTimeout);
            result.SslSettings.Should().Be(subject.SslSettings);
            result.UseTls.Should().Be(subject.UseTls);
#pragma warning disable 618
            result.WaitQueueSize.Should().Be(subject.WaitQueueSize);
#pragma warning restore 618
            result.WaitQueueTimeout.Should().Be(subject.WaitQueueTimeout);
        }