public void TestDefaults()
        {
            string connectionString = "server=localhost";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.IsNull(builder.Username);
            Assert.IsNull(builder.Password);
            Assert.AreEqual(1, builder.Servers.Count());
            Assert.AreEqual("localhost", builder.Servers.Single().Host);
            Assert.AreEqual(27017, builder.Servers.Single().Port);
            Assert.IsNull(builder.DatabaseName);
            Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
            Assert.AreEqual(MongoDefaults.ConnectTimeout, builder.ConnectTimeout);
            Assert.AreEqual(MongoDefaults.GuidRepresentation, builder.GuidRepresentation);
            Assert.AreEqual(false, builder.IPv6);
            Assert.AreEqual(MongoDefaults.MaxConnectionIdleTime, builder.MaxConnectionIdleTime);
            Assert.AreEqual(MongoDefaults.MaxConnectionLifeTime, builder.MaxConnectionLifeTime);
            Assert.AreEqual(MongoDefaults.MaxConnectionPoolSize, builder.MaxConnectionPoolSize);
            Assert.AreEqual(null, builder.ReplicaSetName);
            Assert.IsNull(builder.SafeMode);
            Assert.AreEqual(false, builder.SlaveOk);
            Assert.AreEqual(MongoDefaults.SocketTimeout, builder.SocketTimeout);
            Assert.AreEqual(MongoDefaults.WaitQueueMultiple, builder.WaitQueueMultiple);
            Assert.AreEqual(MongoDefaults.WaitQueueSize, builder.WaitQueueSize);
            Assert.AreEqual(MongoDefaults.WaitQueueTimeout, builder.WaitQueueTimeout);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestMaximumPoolSizeIsEnsured()
        {
            var builder = new MongoConnectionStringBuilder {
                MaximumPoolSize = 2
            };

            using (var pool = new PooledConnectionFactory(builder.ToString()))
            {
                var connection1 = pool.Open();
                Assert.IsNotNull(connection1);
                Assert.IsNotNull(pool.Open());

                RawConnection connection3 = null;
                var           thread      = new Thread(o =>
                {
                    connection3 = pool.Open();
                });
                thread.Start();

                Thread.Sleep(300);

                Assert.IsNull(connection3);

                pool.Close(connection1);

                Thread.Sleep(300);

                Assert.IsNotNull(connection3);

                thread.Abort();
            }
        }
        public void TestToStringOutputWithDefaultServerPort()
        {
            var builder = new MongoConnectionStringBuilder();

            builder.AddServer("testserver");
            Assert.AreEqual("Server=testserver", builder.ToString());
        }
        public void TestMaximumPoolSizeIsEnsured()
        {
            var builder = new MongoConnectionStringBuilder { MaximumPoolSize = 2 };
            using(var pool = new PooledConnectionFactory(builder.ToString()))
            {
                var connection1 = pool.Open();
                Assert.IsNotNull(connection1);
                Assert.IsNotNull(pool.Open());

                RawConnection connection3 = null;
                var thread = new Thread(o =>
                {
                    connection3 = pool.Open();
                });
                thread.Start();
                
                Thread.Sleep(300);
                
                Assert.IsNull(connection3);

                pool.Close(connection1);
                
                Thread.Sleep(300);

                Assert.IsNotNull(connection3);

                thread.Abort();
            }
        }
        public void TestConnectTimeout()
        {
            string connectionString = "server=localhost;connectTimeout=123";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(TimeSpan.FromSeconds(123), builder.ConnectTimeout);
            Assert.AreEqual(connectionString + "s", builder.ToString()); // "s" units added

            connectionString = "server=localhost;connectTimeout=123ms";
            builder          = new MongoConnectionStringBuilder(connectionString);
            Assert.AreEqual(TimeSpan.FromMilliseconds(123), builder.ConnectTimeout);
            Assert.AreEqual(connectionString, builder.ToString());

            connectionString = "server=localhost;connectTimeout=123s";
            builder          = new MongoConnectionStringBuilder(connectionString);
            Assert.AreEqual(TimeSpan.FromSeconds(123), builder.ConnectTimeout);
            Assert.AreEqual(connectionString, builder.ToString());

            connectionString = "server=localhost;connectTimeout=123m";
            builder          = new MongoConnectionStringBuilder(connectionString);
            Assert.AreEqual(TimeSpan.FromMinutes(123), builder.ConnectTimeout);
            Assert.AreEqual(connectionString, builder.ToString());

            connectionString = "server=localhost;connectTimeout=123h";
            builder          = new MongoConnectionStringBuilder(connectionString);
            Assert.AreEqual(TimeSpan.FromHours(123), builder.ConnectTimeout);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestMinConnectionPoolSize()
        {
            string connectionString = "server=localhost;minPoolSize=123";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(123, builder.MinConnectionPoolSize);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSafeModeFalse()
        {
            string connectionString = "server=localhost;safe=false";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(SafeMode.False, builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestGuidRepresentationJavaLegacy()
        {
            string connectionString = "server=localhost;guids=JavaLegacy";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(GuidRepresentation.JavaLegacy, builder.GuidRepresentation);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSafeModeJTrue()
        {
            string connectionString = "server=localhost;safe=true;j=true";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(true, builder.SafeMode.J);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSafeModeW2()
        {
            string connectionString = "server=localhost;w=2";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(SafeMode.W2, builder.SafeMode);
            Assert.AreEqual("server=localhost;safe=true;w=2", builder.ToString()); // safe=true added
        }
        public void TestSafeModeTrueFSyncTrueW2()
        {
            string connectionString = "server=localhost;safe=true;fsync=true;w=2";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(SafeMode.Create(true, true, 2), builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSafeModeTrueFSyncTrueW2WTimeout()
        {
            string connectionString = "server=localhost;safe=true;fsync=true;w=2;wtimeout=2s";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(SafeMode.Create(true, true, 2, TimeSpan.FromSeconds(2)), builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSlaveOkTrue()
        {
            string connectionString = "server=localhost;slaveOk=true";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(true, builder.SlaveOk);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestToStringOutputWithoutUsernameAndPassword()
        {
            var builder = new MongoConnectionStringBuilder();

            builder.AddServer("testserver", 555);

            Assert.AreEqual("Server=testserver:555", builder.ToString());
        }
        public void TestWaitQueueTimeout()
        {
            string connectionString = "server=localhost;waitQueueTimeout=123ms";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(TimeSpan.FromMilliseconds(123), builder.WaitQueueTimeout);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestSafeModeJFalse()
        {
            string connectionString = "server=localhost;safe=true;j=false";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(false, builder.SafeMode.J);
            Assert.AreEqual("server=localhost;safe=true", builder.ToString()); // j=false dropped
        }
        public void TestIpV6()
        {
            string connectionString = "server=localhost;ipv6=true";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(true, builder.IPv6);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestMaxConnectionLifeTime()
        {
            string connectionString = "server=localhost;maxLifeTime=123ms";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(TimeSpan.FromMilliseconds(123), builder.MaxConnectionLifeTime);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestMinimalPoolSizeIsEnsuredAtStartup()
        {
            var builder = new MongoConnectionStringBuilder {
                MinimumPoolSize = 3
            };

            using (var pool = new PooledConnectionFactory(builder.ToString()))
                Assert.AreEqual(3, pool.PoolSize);
        }
 public void TestExceptionIfMaximumPoolSizeAndConnectionTimeoutAreReached()
 {
     var builder = new MongoConnectionStringBuilder { MaximumPoolSize = 1, ConnectionTimeout = TimeSpan.FromMilliseconds(500)};
     using(var pool = new PooledConnectionFactory(builder.ToString()))
     {
         pool.Open();
         pool.Open();
     }
 }
        /// <summary>
        /// Builds the connection string.
        /// </summary>
        /// <param name="config">The config.</param>
        public void ConnectionString(Action<MongoConnectionStringBuilder> config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            var builder = new MongoConnectionStringBuilder();
            config(builder);
            _connectionString = builder.ToString();
        }
        public void TestReplicaSetName()
        {
            string connectionString = "server=localhost;replicaSet=name";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(ConnectionMode.ReplicaSet, builder.ConnectionMode);
            Assert.AreEqual("name", builder.ReplicaSetName);
            Assert.AreEqual("server=localhost;connect=replicaSet;replicaSet=name", builder.ToString()); // connect=replicaSet added
        }
        public void TestWaitQueueSize()
        {
            string connectionString = "server=localhost;waitQueueSize=123";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(0, builder.WaitQueueMultiple);
            Assert.AreEqual(123, builder.WaitQueueSize);
            Assert.AreEqual(connectionString, builder.ToString());
        }
 public void TestBorrowOneConnection()
 {
     var builder = new MongoConnectionStringBuilder();
     using(var pool = new PooledConnectionFactory(builder.ToString()))
     {
         var connection = pool.Open();
         Assert.IsNotNull(connection);
         Assert.AreEqual(1, pool.PoolSize);
     }
 }
        public void TestSafeModeTrueWMajority()
        {
            string connectionString = "server=localhost;safe=true;w=majority";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(new SafeMode(true)
            {
                WMode = "majority"
            }, builder.SafeMode);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestBorrowOneConnection()
        {
            var builder = new MongoConnectionStringBuilder();

            using (var pool = new PooledConnectionFactory(builder.ToString()))
            {
                var connection = pool.Open();
                Assert.IsNotNull(connection);
                Assert.AreEqual(1, pool.PoolSize);
            }
        }
예제 #27
0
        /// <summary>
        /// Builds the connection string.
        /// </summary>
        /// <param name="config">The config.</param>
        public void ConnectionString(Action <MongoConnectionStringBuilder> config)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            var builder = new MongoConnectionStringBuilder();

            config(builder);
            _connectionString = builder.ToString();
        }
        public void TestExceptionIfMaximumPoolSizeAndConnectionTimeoutAreReached()
        {
            var builder = new MongoConnectionStringBuilder {
                MaximumPoolSize = 1, ConnectionTimeout = TimeSpan.FromMilliseconds(500)
            };

            using (var pool = new PooledConnectionFactory(builder.ToString()))
            {
                pool.Open();
                pool.Open();
            }
        }
        private Mongo ConnectAndAuthenticatedMongo(string username, string password)
        {
            var builder = new MongoConnectionStringBuilder(_connectionString)
            {
                Username = username,
                Password = password
            };
            var mongo = new Mongo(builder.ToString());

            mongo.Connect();
            return(mongo);
        }
        public void TestConnectionMode()
        {
            string connectionString = "server=localhost;connect=direct";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
            Assert.AreEqual(connectionString, builder.ToString());

            connectionString = "server=localhost;connect=replicaSet";
            builder          = new MongoConnectionStringBuilder(connectionString);
            Assert.AreEqual(ConnectionMode.ReplicaSet, builder.ConnectionMode);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        public void TestMinimalPoolSizeIsEnsuredAtRuntime()
        {
            var builder = new MongoConnectionStringBuilder { MinimumPoolSize = 3, ConnectionLifetime = TimeSpan.FromMilliseconds(200)};
            using(var pool = new PooledConnectionFactory(builder.ToString()))
            {
                Assert.AreEqual(3, pool.PoolSize);

                Thread.Sleep(500); // ensure connection lifetime reached

                pool.Cleanup();

                Assert.AreEqual(3, pool.PoolSize);
            }
        }
예제 #32
0
        public void CREATE_CLIENT()
        {
            MongoConnectionStringBuilder builder = new MongoConnectionStringBuilder();

            builder.Journal = true;
            builder.Server  = new MongoServerAddress("localhost");
            string connectionString = builder.ToString();

            client = new MongoClient(connectionString);

            //client.GetServer().GetDatabase("EventStore").Drop();
            BsonClassMap.RegisterClassMap <Events.SampleEvent>();
            BsonClassMap.RegisterClassMap <Events.SampleID>();
        }
        public void TestHostWithPort()
        {
            string connectionString = "server=mongo.xyz.com:12345";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.IsNull(builder.Username);
            Assert.IsNull(builder.Password);
            Assert.AreEqual(1, builder.Servers.Count());
            Assert.AreEqual("mongo.xyz.com", builder.Servers.Single().Host);
            Assert.AreEqual(12345, builder.Servers.Single().Port);
            Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
            Assert.AreEqual(null, builder.ReplicaSetName);
            Assert.AreEqual(connectionString, builder.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public MongoDatabaseProvider CreateProvider(
            IDictionary <string, string> options)
        {
            string connection_string;
            string database = options.GetString(kDatabaseOption);

            if (!options.TryGetValue(kConnectionStringOption, out connection_string))
            {
                var    builder = new MongoConnectionStringBuilder();
                string user_name, password;
                options.TryGetValue(kUserNameOption, out user_name);
                options.TryGetValue(kPasswordOption, out password);

                user_name = options.GetString(kUserNameOption, null);
                if (user_name != null)
                {
                    builder.Username     = user_name;
                    builder.Password     = options.GetString(kPasswordOption);
                    builder.DatabaseName = database;
                }

                var servers = new List <MongoServerAddress>();
                foreach (KeyValuePair <string, string> pair in options)
                {
                    if (pair.Value.CompareOrdinal(kHostOption))
                    {
                        string host  = pair.Value;
                        int    index = host.IndexOf(":");
                        if (index == -1)
                        {
                            servers.Add(new MongoServerAddress(host));
                        }
                        servers.Add(new MongoServerAddress(host.Substring(0, index)));
                    }
                }

                if (servers.Count == 0)
                {
                    throw new KeyNotFoundException(
                              "At least one host should be specified.");
                }
                builder.Servers   = servers;
                connection_string = builder.ToString();
            }
            var client = new MongoClient(connection_string);
            var server = client.GetServer();

            return(new MongoDatabaseProvider(server, database));
        }
        public void TestUsernamePasswordLocalhostDatabase()
        {
            string connectionString = "server=localhost;database=database;username=username;password=password";
            var    builder          = new MongoConnectionStringBuilder(connectionString);

            Assert.AreEqual("username", builder.Username);
            Assert.AreEqual("password", builder.Password);
            Assert.AreEqual(1, builder.Servers.Count());
            Assert.AreEqual("localhost", builder.Servers.Single().Host);
            Assert.AreEqual(27017, builder.Servers.Single().Port);
            Assert.AreEqual("database", builder.DatabaseName);
            Assert.AreEqual(ConnectionMode.Direct, builder.ConnectionMode);
            Assert.AreEqual(null, builder.ReplicaSetName);
            Assert.AreEqual(connectionString, builder.ToString());
        }
 public void TestServerCirculationWorks()
 {
     var builder = new MongoConnectionStringBuilder();
     builder.AddServer("localhost", 27017);
     builder.AddServer("localhost", 27018);
     using(var pool = new PooledConnectionFactory(builder.ToString()))
     {
         var connection1 = pool.Open();
         var connection2 = pool.Open();
         var connection3 = pool.Open();
         var connection4 = pool.Open();
         var connection5 = pool.Open();
         Assert.AreEqual(27017, connection1.EndPoint.Port);
         Assert.AreEqual(27018, connection2.EndPoint.Port);
         Assert.AreEqual(27017, connection3.EndPoint.Port);
         Assert.AreEqual(27018, connection4.EndPoint.Port);
         Assert.AreEqual(27017, connection5.EndPoint.Port);
     }
 }
        public void TestCleanup()
        {
            var builder = new MongoConnectionStringBuilder
            {
                MinimumPoolSize = 0,
                ConnectionLifetime = TimeSpan.FromMilliseconds(100)
            };
            using(var pool = new PooledConnectionFactory(builder.ToString()))
            {
                var connection1 = pool.Open();
                var connection2 = pool.Open();
                var connection3 = pool.Open();
                pool.Close(connection1);
                pool.Close(connection2);
                pool.Close(connection3);

                Thread.Sleep(300); // ensure lifetime reached

                pool.Cleanup();

                Assert.AreEqual(0,pool.PoolSize);
            }
        }
        public void TestToStringOutputWithoutUsernameAndPassword(){
            var builder = new MongoConnectionStringBuilder();
            builder.AddServer("testserver", 555);

            Assert.AreEqual("Server=testserver:555", builder.ToString());
        }
 public void TestMinimalPoolSizeIsEnsuredAtStartup()
 {
     var builder = new MongoConnectionStringBuilder {MinimumPoolSize = 3};
     using(var pool = new PooledConnectionFactory(builder.ToString()))
         Assert.AreEqual(3, pool.PoolSize);
 }
 public void TestIfConnectionLifetimeIsReachedItDosenotReturndToPool()
 {
     var builder = new MongoConnectionStringBuilder
     {
         ConnectionLifetime = TimeSpan.FromMilliseconds(100)
     };
     using(var pool = new PooledConnectionFactory(builder.ToString()))
     {
         var connection = pool.Open();
         Thread.Sleep(200); // wait for lifetime reached
         pool.Close(connection);
         Assert.AreEqual(0, pool.PoolSize);
     }
 }
예제 #41
0
 private Mongo ConnectAndAuthenticatedMongo(string username,string password)
 {
     var builder = new MongoConnectionStringBuilder(_connectionString)
     {
         Username = username,
         Password = password
     };
     var mongo = new Mongo(builder.ToString());
     mongo.Connect();
     return mongo;
 }
 public void TestToStringOutputWithDefaultServerPort(){
     var builder = new MongoConnectionStringBuilder();
     builder.AddServer("testserver");
     Assert.AreEqual("Server=testserver", builder.ToString());
 }
        public void TestToStringOutput(){
            var builder = new MongoConnectionStringBuilder
            {
                Password = "******",
                Username = "******",
                ConnectionLifetime = TimeSpan.FromSeconds(50),
                MaximumPoolSize = 101,
                MinimumPoolSize = 202,
                ConnectionTimeout = TimeSpan.FromSeconds(60)
            };
            builder.AddServer("testserver1", 555);
            builder.AddServer("testserver2");

            Assert.AreEqual(
                "Username=testusername;Password=testpassword;Server=testserver1:555,testserver2;MaximumPoolSize=101;MinimumPoolSize=202;ConnectionTimeout=60;ConnectionLifetime=50",
                builder.ToString());
        }