public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings() { var clusterConfigurator = new Action <ClusterBuilder>(b => { }); #pragma warning disable 618 var credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; #pragma warning restore 618 var serverApi = new ServerApi(ServerApiVersion.V1, true, true); 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 kmsProviders = new Dictionary <string, IReadOnlyDictionary <string, object> >() { { "local", new Dictionary <string, object>() { { "key", new byte[96] } } } }; var schemaMap = new Dictionary <string, BsonDocument>() { { "db.coll", new BsonDocument() } }; var clusterKey = new ClusterKey( allowInsecureTls: false, applicationName: "app1", clusterConfigurator: clusterConfigurator, compressors: new[] { new CompressorConfiguration(CompressorType.Zlib) }, #pragma warning disable CS0618 // Type or member is obsolete connectionMode: ConnectionMode.ReplicaSet, connectionModeSwitch: ConnectionModeSwitch.UseConnectionMode, #pragma warning restore CS0618 // Type or member is obsolete connectTimeout: TimeSpan.FromSeconds(1), credentials: credentials, directConnection: null, heartbeatInterval: TimeSpan.FromSeconds(2), heartbeatTimeout: TimeSpan.FromSeconds(3), ipv6: true, kmsProviders: kmsProviders, loadBalanced: false, localThreshold: TimeSpan.FromSeconds(4), maxConnectionIdleTime: TimeSpan.FromSeconds(5), maxConnectionLifeTime: TimeSpan.FromSeconds(6), maxConnectionPoolSize: 7, minConnectionPoolSize: 8, receiveBufferSize: 9, replicaSetName: "rs", schemaMap: schemaMap, scheme: ConnectionStringScheme.MongoDB, sdamLogFilename: "sdam.log", sendBufferSize: 10, serverApi: serverApi, servers: servers, serverSelectionTimeout: TimeSpan.FromSeconds(11), socketTimeout: TimeSpan.FromSeconds(12), sslSettings: sslSettings, useTls: true, waitQueueSize: 13, waitQueueTimeout: TimeSpan.FromSeconds(14)); var subject = new ClusterRegistry(); using (var cluster = subject.GetOrCreateCluster(clusterKey)) { var expectedEndPoints = new EndPoint[] { new DnsEndPoint("localhost", 27017), new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30000), new IPEndPoint(IPAddress.Parse("[::1]"), 27018) }; #pragma warning disable CS0618 // Type or member is obsolete cluster.Settings.ConnectionMode.Should().Be(clusterKey.ConnectionMode.ToCore()); #pragma warning restore CS0618 // Type or member is obsolete cluster.Settings.KmsProviders.Should().BeEquivalentTo(kmsProviders); cluster.Settings.EndPoints.Should().Equal(expectedEndPoints); cluster.Settings.LoadBalanced.Should().Be(clusterKey.LoadBalanced); cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(clusterKey.WaitQueueSize); cluster.Settings.ReplicaSetName.Should().Be(clusterKey.ReplicaSetName); cluster.Settings.SchemaMap.Should().BeEquivalentTo(schemaMap); cluster.Settings.Scheme.Should().Be(clusterKey.Scheme); cluster.Settings.ServerApi.Should().Be(clusterKey.ServerApi); cluster.Settings.ServerSelectionTimeout.Should().Be(clusterKey.ServerSelectionTimeout); cluster.Description.Servers.Select(s => s.EndPoint).Should().BeEquivalentTo(expectedEndPoints); // TODO: don't know how to test the rest of the settings because they are all private to the cluster } }
public void TestEquals() { var settings = new MongoClientSettings(); var clone = settings.Clone(); Assert.True(clone.Equals(settings)); clone = settings.Clone(); clone.ApplicationName = "app2"; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectionMode = ConnectionMode.Direct; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.Credential = MongoCredential.CreateMongoCRCredential("db2", "user2", "password2"); #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.Credential = MongoCredential.CreateMongoCRCredential("db", "user2", "password2"); #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.GuidRepresentation = GuidRepresentation.PythonLegacy; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.HeartbeatInterval = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.HeartbeatTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.IPv6 = !settings.IPv6; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionIdleTime = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionLifeTime = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionPoolSize = settings.MaxConnectionPoolSize + 1; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReadConcern = ReadConcern.Majority; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MinConnectionPoolSize = settings.MinConnectionPoolSize + 1; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReadPreference = ReadPreference.Secondary; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReplicaSetName = "abc"; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.RetryWrites = true; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.LocalThreshold = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.Server = new MongoServerAddress("someotherhost"); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ServerSelectionTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.SocketTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.SslSettings = new SslSettings { CheckCertificateRevocation = false }; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.UseSsl = !settings.UseSsl; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.VerifySslCertificate = !settings.VerifySslCertificate; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.WaitQueueSize = settings.WaitQueueSize + 1; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.WaitQueueTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.WriteConcern = WriteConcern.W2; Assert.False(clone.Equals(settings)); }
public void ToClusterKey_should_copy_relevant_values() { #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 { ApplicationName = "app", ConnectionMode = ConnectionMode.Direct, ConnectTimeout = TimeSpan.FromSeconds(1), Credential = credential, 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); #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.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); }
/// <summary> /// 根据config获得MongoClientSettings,同时更新一些运行时变量 /// </summary> /// <param name="config"></param> /// <returns></returns> public static MongoClientSettings CreateMongoClientSettingsByConfig(ref MongoConnectionConfig config) { //修改获得数据实例的方法 var mongoClientSetting = new MongoClientSettings(); if (string.IsNullOrEmpty(config.ConnectionString)) { mongoClientSetting.ConnectionMode = ConnectionMode.Direct; SetReadPreferenceWriteConcern(mongoClientSetting, config); //Replset时候可以不用设置吗? mongoClientSetting.Server = new MongoServerAddress(config.Host, config.Port); //MapReduce的时候将消耗大量时间。不过这里需要平衡一下,太长容易造成并发问题 //The default value for SocketTimeout has been changed from 30 seconds to 0, if (config.SocketTimeoutMs != 0) { mongoClientSetting.SocketTimeout = new TimeSpan(0, 0, (int)(config.SocketTimeoutMs / 1000)); } if (config.ConnectTimeoutMs != 0) { mongoClientSetting.ConnectTimeout = new TimeSpan(0, 0, (int)(config.ConnectTimeoutMs / 1000)); } // if (SystemConfig.configHelperInstance.wtimeoutMS != 0) // { // mongoClientSetting.WaitQueueTimeout = new TimeSpan(0, 0, (int)(SystemConfig.configHelperInstance.wtimeoutMS / 1000)); // } // if (SystemConfig.configHelperInstance.WaitQueueSize != 0) // { // mongoClientSetting.WaitQueueSize = SystemConfig.configHelperInstance.WaitQueueSize; // } //运行时LoginAsAdmin的设定 config.LoginAsAdmin = (config.DataBaseName == string.Empty); if (!(string.IsNullOrEmpty(config.UserName) || string.IsNullOrEmpty(config.Password))) { //认证的设定:注意,这里的密码是明文 if (string.IsNullOrEmpty(config.DataBaseName)) { mongoClientSetting.Credentials = new[] { MongoCredential.CreateMongoCRCredential(ConstMgr.DatabaseNameAdmin, config.UserName, config.Password) }; } else { mongoClientSetting.Credentials = new[] { MongoCredential.CreateMongoCRCredential(config.DataBaseName, config.UserName, config.Password) }; } } if (config.ReplSetName != string.Empty) { mongoClientSetting.ReplicaSetName = config.ReplSetName; config.ServerRole = MongoConnectionConfig.SvrRoleType.ReplsetSvr; } else { config.ServerRole = MongoConnectionConfig.SvrRoleType.DataSvr; } if (config.ServerRole == MongoConnectionConfig.SvrRoleType.ReplsetSvr) { //ReplsetName不是固有属性,可以设置,不过必须保持与配置文件的一致 mongoClientSetting.ConnectionMode = ConnectionMode.ReplicaSet; //添加Replset服务器,注意,这里可能需要事先初始化副本 var replsetSvrList = new List <MongoServerAddress>(); foreach (var item in config.ReplsetList) { //如果这里的服务器在启动的时候没有--Replset参数,将会出错,当然作为单体的服务器,启动是没有任何问题的 MongoServerAddress replSrv; if (item.Split(":".ToCharArray()).Length == 2) { replSrv = new MongoServerAddress( item.Split(":".ToCharArray())[0], Convert.ToInt16(item.Split(":".ToCharArray())[1])); } else { replSrv = new MongoServerAddress(item); } replsetSvrList.Add(replSrv); } mongoClientSetting.Servers = replsetSvrList; } } else { //使用MongoConnectionString建立连接 mongoClientSetting = MongoClientSettings.FromUrl(MongoUrl.Create(config.ConnectionString)); } //为了避免出现无法读取数据库结构的问题,将读权限都设置为Preferred if (mongoClientSetting.ReadPreference == ReadPreference.Primary) { mongoClientSetting.ReadPreference = ReadPreference.PrimaryPreferred; } if (mongoClientSetting.ReadPreference == ReadPreference.Secondary) { mongoClientSetting.ReadPreference = ReadPreference.SecondaryPreferred; } return(mongoClientSetting); }
public void TestEquals() { var settings = new MongoClientSettings(); var clone = settings.Clone(); Assert.IsTrue(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectionMode = ConnectionMode.Direct; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectTimeout = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.Credentials = new[] { MongoCredential.CreateMongoCRCredential("db2", "user2", "password2") }; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.Credentials = new[] { MongoCredential.CreateMongoCRCredential("db", "user2", "password2") }; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.GuidRepresentation = GuidRepresentation.PythonLegacy; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.IPv6 = !settings.IPv6; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionIdleTime = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionLifeTime = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionPoolSize = settings.MaxConnectionPoolSize + 1; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.MinConnectionPoolSize = settings.MinConnectionPoolSize + 1; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.OperationTimeout = TimeSpan.FromMilliseconds(20); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.ReadPreference = ReadPreference.Secondary; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.ReplicaSetName = "abc"; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.LocalThreshold = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.Server = new MongoServerAddress("someotherhost"); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.SocketTimeout = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.SslSettings = new SslSettings { CheckCertificateRevocation = false }; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.UseSsl = !settings.UseSsl; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.VerifySslCertificate = !settings.VerifySslCertificate; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.WaitQueueSize = settings.WaitQueueSize + 1; Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.WaitQueueTimeout = new TimeSpan(1, 2, 3); Assert.IsFalse(clone.Equals(settings)); clone = settings.Clone(); clone.WriteConcern = WriteConcern.W2; Assert.IsFalse(clone.Equals(settings)); }
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, GuidRepresentation = GuidRepresentation.Standard, 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) }; 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); }
private ClusterKey CreateSubject(string notEqualFieldName = null) { var allowInsecureTls = true; var applicationName = "app1"; var clusterConfigurator = new Action <ClusterBuilder>(b => { }); var compressors = new CompressorConfiguration[0]; var connectionMode = ConnectionMode.Direct; var connectTimeout = TimeSpan.FromSeconds(1); #pragma warning disable 618 var credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; #pragma warning restore 618 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 receiveBufferSize = 1; var replicaSetName = "abc"; var scheme = ConnectionStringScheme.MongoDB; var sdamLogFileName = "stdout"; var sendBufferSize = 1; 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 useTls = false; var waitQueueSize = 20; var waitQueueTimeout = TimeSpan.FromSeconds(5); if (notEqualFieldName != null) { switch (notEqualFieldName) { case "AllowInsecureTls": allowInsecureTls = !allowInsecureTls; break; case "ApplicationName": applicationName = "app2"; break; case "ClusterConfigurator": clusterConfigurator = new Action <ClusterBuilder>(b => { }); break; case "Compressors": compressors = new[] { new CompressorConfiguration(CompressorType.Zlib) }; break; case "ConnectionMode": connectionMode = ConnectionMode.ReplicaSet; break; case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break; #pragma warning disable 618 case "Credentials": credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("different", "different", "different") }; break; #pragma warning restore 618 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 "ReceiveBufferSize": receiveBufferSize = 2; break; case "ReplicaSetName": replicaSetName = "different"; break; case "Scheme": scheme = ConnectionStringScheme.MongoDBPlusSrv; break; case "SdamLogFileName": sdamLogFileName = "different"; break; case "SendBufferSize": sendBufferSize = 2; 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 "UseTls": useTls = !useTls; break; case "WaitQueueSize": waitQueueSize = 99; break; case "WaitQueueTimeout": waitQueueTimeout = TimeSpan.FromSeconds(99); break; default: throw new ArgumentException($"Invalid field name: \"{notEqualFieldName}\".", nameof(notEqualFieldName)); } } return(new ClusterKey( allowInsecureTls, applicationName, clusterConfigurator, compressors, connectionMode, connectTimeout, credentials, heartbeatInterval, heartbeatTimeout, ipv6, localThreshold, maxConnectionIdleTime, maxConnectionLifeTime, maxConnectionPoolSize, minConnectionPoolSize, receiveBufferSize, replicaSetName, scheme, sdamLogFileName, sendBufferSize, servers, serverSelectionTimeout, socketTimeout, sslSettings, useTls, waitQueueSize, waitQueueTimeout)); }
private ClusterKey CreateSubject(string notEqualFieldName = null) { var connectionMode = ConnectionMode.Direct; var connectTimeout = TimeSpan.FromSeconds(1); var credentials = new[] { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; var guidRepresentation = GuidRepresentation.Standard; 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.Ssl3 }; var useSsl = false; var verifySslCertificate = false; var waitQueueSize = 20; var waitQueueTimeout = TimeSpan.FromSeconds(5); switch (notEqualFieldName) { case "ConnectionMode": connectionMode = ConnectionMode.ReplicaSet; break; case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break; case "Credentials": credentials = new[] { MongoCredential.CreateMongoCRCredential("different", "different", "different") }; 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 { ConnectionMode = connectionMode, ConnectTimeout = connectTimeout, Credentials = credentials, GuidRepresentation = guidRepresentation, 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()); }
public void GetOrCreateCluster_should_return_a_cluster_with_the_correct_settings() { var clusterConfigurator = new Action <ClusterBuilder>(b => { }); #pragma warning disable 618 var credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; #pragma warning restore 618 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 clusterKey = new ClusterKey( applicationName: "app1", clusterConfigurator: clusterConfigurator, compressors: new[] { new CompressorConfiguration(CompressorType.Zlib) }, connectionMode: ConnectionMode.ReplicaSet, connectTimeout: TimeSpan.FromSeconds(1), credentials: credentials, heartbeatInterval: TimeSpan.FromSeconds(2), heartbeatTimeout: TimeSpan.FromSeconds(3), ipv6: true, localThreshold: TimeSpan.FromSeconds(4), maxConnectionIdleTime: TimeSpan.FromSeconds(5), maxConnectionLifeTime: TimeSpan.FromSeconds(6), maxConnectionPoolSize: 7, minConnectionPoolSize: 8, receiveBufferSize: 9, replicaSetName: "rs", scheme: ConnectionStringScheme.MongoDB, sdamLogFilename: "sdam.log", sendBufferSize: 10, servers: servers, serverSelectionTimeout: TimeSpan.FromSeconds(11), socketTimeout: TimeSpan.FromSeconds(12), sslSettings: sslSettings, useSsl: true, verifySslCertificate: true, waitQueueSize: 13, waitQueueTimeout: TimeSpan.FromSeconds(14)); var subject = new ClusterRegistry(); using (var cluster = subject.GetOrCreateCluster(clusterKey)) { var expectedEndPoints = 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(clusterKey.ConnectionMode.ToCore()); cluster.Settings.EndPoints.Should().Equal(expectedEndPoints); cluster.Settings.MaxServerSelectionWaitQueueSize.Should().Be(clusterKey.WaitQueueSize); cluster.Settings.ReplicaSetName.Should().Be(clusterKey.ReplicaSetName); cluster.Settings.Scheme.Should().Be(clusterKey.Scheme); cluster.Settings.ServerSelectionTimeout.Should().Be(clusterKey.ServerSelectionTimeout); cluster.Description.Servers.Select(s => s.EndPoint).Should().BeEquivalentTo(expectedEndPoints); // TODO: don't know how to test the rest of the settings because they are all private to the cluster } }
public void TestEquals() { var settings = new MongoClientSettings(); var clone = settings.Clone(); Assert.True(clone.Equals(settings)); clone = settings.Clone(); clone.AllowInsecureTls = !settings.AllowInsecureTls; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ApplicationName = "app2"; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.Compressors = new[] { new CompressorConfiguration(CompressorType.Zlib) }; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectionMode = ConnectionMode.Direct; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ConnectTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.Credential = MongoCredential.CreateMongoCRCredential("db2", "user2", "password2"); #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.Credential = MongoCredential.CreateMongoCRCredential("db", "user2", "password2"); #pragma warning restore 618 Assert.False(clone.Equals(settings)); #pragma warning disable 618 if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { clone = settings.Clone(); var unequalGuidRepresentation = clone.GuidRepresentation != GuidRepresentation.PythonLegacy ? GuidRepresentation.PythonLegacy : GuidRepresentation.CSharpLegacy; clone.GuidRepresentation = unequalGuidRepresentation; Assert.False(clone.Equals(settings)); } #pragma warning restore 618 clone = settings.Clone(); clone.HeartbeatInterval = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.HeartbeatTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.IPv6 = !settings.IPv6; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.LocalThreshold = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionIdleTime = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionLifeTime = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MaxConnectionPoolSize = settings.MaxConnectionPoolSize + 1; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReadConcern = ReadConcern.Majority; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.MinConnectionPoolSize = settings.MinConnectionPoolSize + 1; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReadPreference = ReadPreference.Secondary; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ReplicaSetName = "abc"; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.RetryReads = false; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.RetryWrites = false; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.Scheme = ConnectionStringScheme.MongoDBPlusSrv; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.Server = new MongoServerAddress("someotherhost"); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.ServerSelectionTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.SocketTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.SslSettings = new SslSettings { CheckCertificateRevocation = false }; Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.UseSsl = !settings.UseSsl; #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.UseTls = !settings.UseTls; Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.VerifySslCertificate = !settings.VerifySslCertificate; #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); #pragma warning disable 618 clone.WaitQueueSize = settings.WaitQueueSize + 1; #pragma warning restore 618 Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.WaitQueueTimeout = new TimeSpan(1, 2, 3); Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.WriteConcern = WriteConcern.W2; Assert.False(clone.Equals(settings)); clone = settings.Clone(); clone.SdamLogFilename = "garbage"; Assert.False(clone.Equals(settings)); }
internal ClusterKey CreateSubjectWith( Dictionary <string, IReadOnlyDictionary <string, object> > kmsProvidersValue = null, Dictionary <string, BsonDocument> schemaMapValue = null, #pragma warning disable CS0618 // Type or member is obsolete ConnectionModeSwitch connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode) #pragma warning restore CS0618 // Type or member is obsolete { var allowInsecureTls = true; var applicationName = "app1"; var clusterConfigurator = new Action <ClusterBuilder>(b => { }); var compressors = new CompressorConfiguration[0]; #pragma warning disable CS0618 // Type or member is obsolete var connectionMode = connectionModeSwitch != ConnectionModeSwitch.UseConnectionMode ? ConnectionMode.Automatic : ConnectionMode.Direct; #pragma warning restore CS0618 // Type or member is obsolete var connectTimeout = TimeSpan.FromSeconds(1); #pragma warning disable 618 var credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; #pragma warning restore 618 bool?directConnection = null; var heartbeatInterval = TimeSpan.FromSeconds(7); var heartbeatTimeout = TimeSpan.FromSeconds(8); var ipv6 = false; var kmsProviders = kmsProvidersValue ?? new Dictionary <string, IReadOnlyDictionary <string, object> >(); var localThreshold = TimeSpan.FromMilliseconds(20); var maxConnectionIdleTime = TimeSpan.FromSeconds(2); var maxConnectionLifeTime = TimeSpan.FromSeconds(3); var maxConnectionPoolSize = 50; var minConnectionPoolSize = 5; var receiveBufferSize = 1; var replicaSetName = "abc"; var schemaMap = schemaMapValue ?? new Dictionary <string, BsonDocument>(); var scheme = ConnectionStringScheme.MongoDB; var sdamLogFileName = "stdout"; var sendBufferSize = 1; var serverApi = new ServerApi(ServerApiVersion.V1, true, true); 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 useTls = false; var waitQueueSize = 20; var waitQueueTimeout = TimeSpan.FromSeconds(5); return(new ClusterKey( allowInsecureTls, applicationName, clusterConfigurator, compressors, connectionMode, connectionModeSwitch, connectTimeout, credentials, directConnection, heartbeatInterval, heartbeatTimeout, ipv6, kmsProviders, localThreshold, maxConnectionIdleTime, maxConnectionLifeTime, maxConnectionPoolSize, minConnectionPoolSize, receiveBufferSize, replicaSetName, schemaMap, scheme, sdamLogFileName, sendBufferSize, serverApi, servers, serverSelectionTimeout, socketTimeout, sslSettings, useTls, waitQueueSize, waitQueueTimeout)); }
// private methods private ClusterKey CreateSubject(string notEqualFieldName = null) { var allowInsecureTls = true; var applicationName = "app1"; var clusterConfigurator = new Action <ClusterBuilder>(b => { }); #pragma warning disable CS0618 // Type or member is obsolete var connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode; #pragma warning restore CS0618 // Type or member is obsolete var compressors = new CompressorConfiguration[0]; #pragma warning disable CS0618 // Type or member is obsolete var connectionMode = ConnectionMode.Direct; #pragma warning restore CS0618 // Type or member is obsolete var connectTimeout = TimeSpan.FromSeconds(1); #pragma warning disable 618 var credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("source", "username", "password") }; #pragma warning restore 618 bool?directConnection = null; var heartbeatInterval = TimeSpan.FromSeconds(7); var heartbeatTimeout = TimeSpan.FromSeconds(8); var ipv6 = false; var kmsProviders = new Dictionary <string, IReadOnlyDictionary <string, object> >(); var localThreshold = TimeSpan.FromMilliseconds(20); var maxConnectionIdleTime = TimeSpan.FromSeconds(2); var maxConnectionLifeTime = TimeSpan.FromSeconds(3); var maxConnectionPoolSize = 50; var minConnectionPoolSize = 5; var receiveBufferSize = 1; var replicaSetName = "abc"; var schemaMap = new Dictionary <string, BsonDocument>(); var scheme = ConnectionStringScheme.MongoDB; var sdamLogFileName = "stdout"; var sendBufferSize = 1; var serverApi = new ServerApi(ServerApiVersion.V1, true, true); 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 useTls = false; var waitQueueSize = 20; var waitQueueTimeout = TimeSpan.FromSeconds(5); if (notEqualFieldName != null) { switch (notEqualFieldName) { case "AllowInsecureTls": allowInsecureTls = !allowInsecureTls; break; case "ApplicationName": applicationName = "app2"; break; case "ClusterConfigurator": clusterConfigurator = new Action <ClusterBuilder>(b => { }); break; case "Compressors": compressors = new[] { new CompressorConfiguration(CompressorType.Zlib) }; break; #pragma warning disable CS0618 // Type or member is obsolete case "ConnectionMode": { connectionMode = ConnectionMode.ReplicaSet; connectionModeSwitch = ConnectionModeSwitch.UseConnectionMode; directConnection = null; // reset } break; #pragma warning restore CS0618 // Type or member is obsolete case "ConnectTimeout": connectTimeout = TimeSpan.FromSeconds(99); break; #pragma warning disable CS0618 // Type or member is obsolete case "Credentials": credentials = new List <MongoCredential> { MongoCredential.CreateMongoCRCredential("different", "different", "different") }; break; case "DirectConnection": { directConnection = true; connectionModeSwitch = ConnectionModeSwitch.UseDirectConnection; connectionMode = ConnectionMode.Automatic; // reset #pragma warning restore CS0618 // Type or member is obsolete } break; case "HeartbeatInterval": heartbeatInterval = TimeSpan.FromSeconds(99); break; case "HeartbeatTimeout": heartbeatTimeout = TimeSpan.FromSeconds(99); break; case "IPv6": ipv6 = !ipv6; break; case "KmsProviders": kmsProviders.Add("local", new Dictionary <string, object>() { { "key", "test" } }); 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 "ReceiveBufferSize": receiveBufferSize = 2; break; case "ReplicaSetName": replicaSetName = "different"; break; case "SchemaMap": schemaMap.Add("db.coll", new BsonDocument()); break; case "Scheme": scheme = ConnectionStringScheme.MongoDBPlusSrv; break; case "SdamLogFileName": sdamLogFileName = "different"; break; case "SendBufferSize": sendBufferSize = 2; break; case "ServerApi": serverApi = new ServerApi(ServerApiVersion.V1); 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 "UseTls": useTls = !useTls; break; case "WaitQueueSize": waitQueueSize = 99; break; case "WaitQueueTimeout": waitQueueTimeout = TimeSpan.FromSeconds(99); break; default: throw new ArgumentException($"Invalid field name: \"{notEqualFieldName}\".", nameof(notEqualFieldName)); } } return(new ClusterKey( allowInsecureTls, applicationName, clusterConfigurator, compressors, connectionMode, connectionModeSwitch, connectTimeout, credentials, directConnection, heartbeatInterval, heartbeatTimeout, ipv6, kmsProviders, localThreshold, maxConnectionIdleTime, maxConnectionLifeTime, maxConnectionPoolSize, minConnectionPoolSize, receiveBufferSize, replicaSetName, schemaMap, scheme, sdamLogFileName, sendBufferSize, serverApi, servers, serverSelectionTimeout, socketTimeout, sslSettings, useTls, waitQueueSize, waitQueueTimeout)); }