示例#1
0
        public void GetConnection_FirstTimeWithMinPoolSize_InitializesConnectionPool()
        {
            //prepare
            _factory.As <IPooledConnectionFactoryConfiguration>().MinPoolSizeIs(2);

            //act
            _factory.GetConnection();

            //assert
            _factory.FieldValueEx <Dictionary <LdapConnection, DateTime> >("_availableConnections")
            .Should().Have.Count.EqualTo(1);
            _factory.FieldValueEx <List <LdapConnection> >("_inUseConnections")
            .Should().Have.Count.EqualTo(1);
        }
        public void ReInitializePool_NotDispose_ReInitializes()
        {
            //prepare
            _factory = new PooledLdapConnectionFactory("localhost");
            _factory.As <IPooledConnectionFactoryConfiguration>().MinPoolSizeIs(2);
            var logger = new Mock <ILinqToLdapLogger>();

            logger.SetupGet(l => l.TraceEnabled)
            .Returns(true);
            _factory.Logger = logger.Object;
            _factory.GetConnection();

            //act
            _factory.Reinitialize();

            //assert
            logger.Verify(l => l.Trace("Scavenge Timer Stopped."), Times.Once());
            logger.Verify(l => l.Trace("Initializing Connection Pool."), Times.Exactly(2));
            _factory.FieldValueEx <Timer>("_timer").Enabled.Should().Be.False();
            _factory.FieldValueEx <bool>("_isFirstRequest").Should().Be.True();
            _factory.FieldValueEx <Dictionary <LdapConnection, TwoTuple <DateTime, DateTime> > >("_availableConnections")
            .Should().Have.Count.EqualTo(0);
            _factory.FieldValueEx <Dictionary <LdapConnection, DateTime> >("_inUseConnections")
            .Should().Have.Count.EqualTo(0);
        }
        public void GetConnection_MaxAge_Exceeded_DisposesConnections()
        {
            //prepare
            _factory = new PooledLdapConnectionFactory("localhost");
            _factory.As <IPooledConnectionFactoryConfiguration>().ScavengeIntervalIs(1000).MaxConnectionAgeIs(TimeSpan.FromMilliseconds(1000));
            var connection = _factory.GetConnection();

            connection.Should().Not.Be.Null();
            _factory.ReleaseConnection(connection);
            var same = _factory.GetConnection();

            same.Should().Be.SameInstanceAs(connection);
            _factory.ReleaseConnection(same);

            System.Threading.Thread.Sleep(1000);

            //act
            var connection2 = _factory.GetConnection();

            //assert
            connection2.Should().Not.Be.SameInstanceAs(connection);
            _factory.ReleaseConnection(connection2);
        }