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 TestDisconnectedConnectionsArentReturndToPool()
 {
     using (var pool = new PooledConnectionFactory(string.Empty))
     {
         var connection = pool.Open();
         connection.Dispose();
         pool.Close(connection);
         Assert.AreEqual(0, pool.PoolSize);
     }
 }
 public void TestClose()
 {
     using (var pool = new PooledConnectionFactory(string.Empty))
     {
         var connection1 = pool.Open();
         pool.Close(connection1);
         var connection2 = pool.Open();
         Assert.AreEqual(connection1, connection2);
         Assert.AreEqual(1, pool.PoolSize);
     }
 }
        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 TestIfConnectionLifetimeIsReachedItDoesNotReturndToPool()
        {
            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);
            }
        }