Exemplo n.º 1
0
        public void ThreadsSharePooledConnections()
        {
            // This test should probably test the Mongo class instead of testing the pool
            // provider directly!?
            var provider = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=3"));
            var idList   = new List <int>();

            for (var i = 0; i < 10; i++)
            {
                ThreadPool.QueueUserWorkItem(w =>
                {
                    var connection = provider.Open(null);
                    idList.Add(connection.GetHashCode());
                    provider.Close(connection);
                });
            }

            // Simply wait - don't need to join monitor callbacks this simplistic unit test.
            System.Threading.Thread.Sleep(1000);
            Assert.Equal(10, idList.Count);

            // Count could be more than the pool size since new connections are created
            // for an empty pool, but it shold generally matchthe pool size in the
            // connection string.
            var connectionCount = idList.Distinct().Count();

            Assert.True(connectionCount < 10);
            Console.WriteLine("Threading produced {0} connections for 10 threads", connectionCount);
        }
Exemplo n.º 2
0
 public void CreatesDigestFromNonce()
 {
     using (var connection = new DisposableConnection(ConnectionStringBuilder.Create(TestHelper.ConnectionString("querytimeout=30&strict=false", "ussrr", "ppaassss"))))
     {
         Assert.Equal("21069b52452d123b3f4885400c1c9581", connection.Digest("1234abc"));
     }
 }
Exemplo n.º 3
0
        public void DefaultAuthenticationWhenNotSpecified()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://host/db");

            Assert.Equal(null, builder.UserName);
            Assert.Equal(null, builder.Password);
        }
Exemplo n.º 4
0
        public void ParsesAuthenticationInformation()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://*****:*****@host/db");

            Assert.Equal("user", builder.UserName);
            Assert.Equal("pass", builder.Password);
        }
        public void ThrowsExceptionIfConnectingWithInvalidCredentials()
        {
            var provider = new PooledConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("bad", "boy")));
            var ex       = Assert.Throws <MongoException>(() => provider.Open(null));

            Assert.Equal("auth fails", ex.Message);
        }
Exemplo n.º 6
0
        public void ParsesSingleHostWithPort()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://host:123");

            Assert.Equal(1, builder.Servers.Count);
            Assert.Equal("host", builder.Servers[0].Host);
            Assert.Equal(123, builder.Servers[0].Port);
        }
        public void ClosesTheUnderlyingConnection()
        {
            var provider   = new NormalConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString()));
            var connection = provider.Open(null);

            provider.Close(connection);
            Assert.Null(connection.Client.Client);
        }
        public void AuthenticatesWithProperCredentials()
        {
            var provider   = new NormalConnectionProvider(ConnectionStringBuilder.Create(AuthenticatedConnectionString("usr", "pss")));
            var connection = provider.Open(null);

            Assert.Equal(true, connection.Client.Connected);
            provider.Close(connection);
        }
Exemplo n.º 9
0
 public void ThrowsExceptionWhenTryingToOverrideLifetime()
 {
     using (var connection = new DisposableConnection(ConnectionStringBuilder.Create(TestHelper.ConnectionString())))
     {
         var ex = Assert.Throws <MongoException>(() => connection.LoadOptions("lifetime=23"));
         Assert.Equal("Lifetime cannot be provided as an override option", ex.Message);
     }
 }
Exemplo n.º 10
0
        public void ClosingAConnectionReturnsItToThePool()
        {
            var provider    = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=1"));
            var connection1 = provider.Open(null);

            provider.Close(connection1);
            Assert.Same(connection1, provider.Open(null));
        }
Exemplo n.º 11
0
        public void WaitsUntilTimeoutForConnectionToFreeUp()
        {
            var provider   = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=1&timeout=3")));
            var connection = provider.Open(null);

            new Timer(c => provider.Close(connection), null, 2000, 0);
            Assert.Same(connection, provider.Open(null));
        }
Exemplo n.º 12
0
        public void ClosingAConnectionReturnsItToThePool()
        {
            var provider    = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=1")));
            var connection1 = provider.Open(null);

            provider.Close(connection1);
            Assert.Same(connection1, provider.Open(null));
        }
Exemplo n.º 13
0
        public static string Create(string layerName)
        {
            if (layerName.ToUpperInvariant().StartsWith("SGID10"))
            {
                return(ConnectionStringBuilder.Create(new Sgid10ConnectionInformation()));
            }

            return(null);
        }
Exemplo n.º 14
0
 public void OverridesQueryTimeout()
 {
     using (var connection = new DisposableConnection(ConnectionStringBuilder.Create(TestHelper.ConnectionString("querytimeout=30"))))
     {
         Assert.Equal(30, connection.QueryTimeout);
         connection.LoadOptions("querytimeout=32");
         Assert.Equal(32, connection.QueryTimeout);
     }
 }
Exemplo n.º 15
0
 public void OverridesStrictMode()
 {
     using (var connection = new DisposableConnection(ConnectionStringBuilder.Create(TestHelper.ConnectionString("?strict=true"))))
     {
         Assert.Equal(true, connection.StrictMode);
         connection.LoadOptions("strict=false");
         Assert.Equal(false, connection.StrictMode);
     }
 }
Exemplo n.º 16
0
        public void PoolsUpToPoolSizeConections()
        {
            var provider = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=4&timeout=1")));

            provider.Open(null);
            provider.Open(null);
            provider.Open(null);
            provider.Open(null);
            Assert.Throws <MongoException>(() => provider.Open(null));
        }
Exemplo n.º 17
0
        public void ThrowsExceptionIfNoConnectionsAvailable()
        {
            var provider = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=1&timeout=1")));

            provider.Open(null);

            var ex = Assert.Throws <MongoException>(() => provider.Open(null));

            Assert.Equal("Connection timeout trying to get connection from connection pool", ex.Message);
        }
Exemplo n.º 18
0
        public void NewConnectionsAreCreatedWhenQueueIsEmpty()
        {
            var provider    = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=1&timeout=1"));
            var connection1 = provider.Open(null);

            var connection2 = provider.Open(null);

            Assert.NotNull(connection2);
            Assert.NotSame(connection1, connection2);
        }
Exemplo n.º 19
0
        public void ParsesMultipleHosts()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://vegeta,freeza");

            Assert.Equal(2, builder.Servers.Count);
            Assert.Equal("vegeta", builder.Servers[0].Host);
            Assert.Equal(27017, builder.Servers[0].Port);
            Assert.Equal("freeza", builder.Servers[1].Host);
            Assert.Equal(27017, builder.Servers[1].Port);
        }
Exemplo n.º 20
0
        public void ParsesMultipleHostsWithPorts()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://vegeta:8999,goku:9001");

            Assert.Equal(2, builder.Servers.Count);
            Assert.Equal("vegeta", builder.Servers[0].Host);
            Assert.Equal(8999, builder.Servers[0].Port);
            Assert.Equal("goku", builder.Servers[1].Host);
            Assert.Equal(9001, builder.Servers[1].Port);
        }
Exemplo n.º 21
0
        public void ResetsDefaults()
        {
            using (var connection = new DisposableConnection(ConnectionStringBuilder.Create(TestHelper.ConnectionString("querytimeout=30&strict=false"))))
            {
                connection.LoadOptions("querytimeout=23&strict=true");
                Assert.Equal(true, connection.StrictMode);
                Assert.Equal(23, connection.QueryTimeout);

                connection.ResetOptions();
                Assert.Equal(false, connection.StrictMode);
                Assert.Equal(30, connection.QueryTimeout);
            }
        }
Exemplo n.º 22
0
        public void PoolsUpToPoolSizeConections()
        {
            var provider = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=4"));

            provider.Open(null);
            provider.Open(null);
            provider.Open(null);
            provider.Open(null);

            // Creating a new connection instead of throwing an exception.  Not sure if it's a
            // good idea yet, but in theory it supports sudden surges in activity.
            Assert.DoesNotThrow(() => provider.Open(null));
        }
Exemplo n.º 23
0
        public void WaitsUntilTimeoutForConnectionToFreeUpAndThrowsExceptionIfNot()
        {
            var provider = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=1&timeout=3")));

            provider.Open(null);

            var start = DateTime.Now; //this doesn't seem like a very good way to do this..we'll see

            Assert.Throws <MongoException>(() => provider.Open(null));
            var elasped = DateTime.Now.Subtract(start).TotalSeconds;

            Assert.True(elasped > 3);
            Assert.True(elasped < 4);
        }
Exemplo n.º 24
0
        public void ParsesComplexConnectionString()
        {
            var builder = ConnectionStringBuilder.Create("mongodb://*****:*****@host,goku:9001/dbz?strict=false&pooling=true&poolsize=100");

            Assert.Equal(2, builder.Servers.Count);
            Assert.Equal("host", builder.Servers[0].Host);
            Assert.Equal(27017, builder.Servers[0].Port);
            Assert.Equal("goku", builder.Servers[1].Host);
            Assert.Equal(9001, builder.Servers[1].Port);
            Assert.Equal("dbz", builder.Database);
            Assert.Equal("its", builder.UserName);
            Assert.Equal("over", builder.Password);
            Assert.Equal(false, builder.StrictMode);
            Assert.Equal(true, builder.Pooled);
            Assert.Equal(100, builder.PoolSize);
        }
Exemplo n.º 25
0
        public void PoolReusesQueuedConnections()
        {
            var provider       = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=2"));
            var connection1    = provider.Open(null);
            var conection1Hash = connection1.GetHashCode();

            // Don't close connection 1 yet, now open and close connection 2
            var connection2    = provider.Open(null);
            var conection2Hash = connection2.GetHashCode();

            provider.Close(connection2);

            var connection3    = provider.Open(null);
            var conection3Hash = connection3.GetHashCode();

            // Connection 2 should have been re-queued and returned
            Assert.NotEqual(conection1Hash, conection2Hash);
            Assert.Equal(conection2Hash, conection3Hash);
        }
Exemplo n.º 26
0
        public void PoolReusesConnectionsUponConnectionClose()
        {
            var provider       = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=2"));
            var connection1    = provider.Open(null);
            var conection1Hash = connection1.GetHashCode();

            provider.Close(connection1);

            var connection2    = provider.Open(null);
            var conection2Hash = connection2.GetHashCode();

            provider.Close(connection2);

            var connection3    = provider.Open(null);
            var conection3Hash = connection3.GetHashCode();

            Assert.NotEqual(conection1Hash, conection2Hash);
            Assert.Equal(conection1Hash, conection3Hash);
        }
Exemplo n.º 27
0
        public void CreatesANewConnectionForEachOpen()
        {
            IConnection connection1 = null;
            IConnection connection2 = null;
            var         provider    = new NormalConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString()));

            try
            {
                connection1 = provider.Open(null);
                connection2 = provider.Open(null);
                Assert.NotSame(connection1, connection2);
            }
            finally
            {
                if (connection1 != null)
                {
                    provider.Close(connection1);
                }
                if (connection2 != null)
                {
                    provider.Close(connection2);
                }
            }
        }
Exemplo n.º 28
0
 public void ThrowsExceptionIfCantConnect()
 {
     Assert.Throws <SocketException>(() => new Connection(ConnectionStringBuilder.Create("mongodb://localhost2/test")));
 }
Exemplo n.º 29
0
        public void ReturnsDifferentConnections()
        {
            var provider = new PooledConnectionProvider(ConnectionStringBuilder.Create(TestHelper.ConnectionString("pooling=true&poolsize=2")));

            Assert.NotSame(provider.Open(null), provider.Open(null));
        }
Exemplo n.º 30
0
        public void ReturnsDifferentConnections()
        {
            var provider = new QueuedConnectionProvider(ConnectionStringBuilder.Create("mongodb://localhost?pooling=true&poolsize=2"));

            Assert.NotSame(provider.Open(null), provider.Open(null));
        }