예제 #1
0
        public void ForgetConnection_ExceedPoolSize_GetConnectionShouldBlockAndReturnNewConnection()
        {
            // Arrange
            var pool = new SapConnectionPool(
                ConnectionParameters,
                poolSize: 1,
                connectionFactory: _ => Mock.Of <ISapConnection>());

            ISapConnection connection1 = pool.GetConnection();

            // Act
            Task.Run(async() =>
            {
                await Task.Delay(150);
                pool.ForgetConnection(connection1);
            });
            var sw = new Stopwatch();

            sw.Start();
            ISapConnection connection2 = pool.GetConnection();

            sw.Stop();

            // Assert
            sw.ElapsedMilliseconds.Should().BeGreaterThan(100);
            connection2.Should().NotBeNull();
            connection2.Should().NotBe(connection1);
        }
예제 #2
0
        public void GetConnection_ShouldOpenConnection()
        {
            // Arrange
            var connectionMock = new Mock <ISapConnection>();
            var pool           = new SapConnectionPool(ConnectionParameters, connectionFactory: _ => connectionMock.Object);

            // Act
            ISapConnection connection = pool.GetConnection();

            // Assert
            connection.Should().Be(connectionMock.Object);
            connectionMock.Verify(x => x.Connect(), Times.Once);
        }
예제 #3
0
        public void ForgetConnection_ShouldDisposeConnection()
        {
            // Arrange
            var            connectionMock = new Mock <ISapConnection>();
            var            pool           = new SapConnectionPool(ConnectionParameters, connectionFactory: _ => connectionMock.Object);
            ISapConnection connection     = pool.GetConnection();

            // Act
            pool.ForgetConnection(connection);

            // Assert
            connectionMock.Verify(x => x.Dispose(), Times.Once);
        }
예제 #4
0
        public void GetConnection_ShouldReturnDifferentConnectionsUpToPoolSize()
        {
            // Arrange
            var pool = new SapConnectionPool(
                ConnectionParameters,
                poolSize: 2,
                connectionFactory: _ => Mock.Of <ISapConnection>());

            // Act
            ISapConnection connection1 = pool.GetConnection();
            ISapConnection connection2 = pool.GetConnection();

            // Assert
            connection1.Should().NotBeNull();
            connection2.Should().NotBeNull();
            connection1.Should().NotBe(connection2);
        }
예제 #5
0
        public void Dispose_ShouldDisposeIdleConnections()
        {
            // Arrange
            var            connectionMock = new Mock <ISapConnection>();
            var            pool           = new SapConnectionPool(ConnectionParameters, connectionFactory: _ => connectionMock.Object);
            ISapConnection connection1    = pool.GetConnection();
            ISapConnection connection2    = pool.GetConnection();

            pool.ReturnConnection(connection1);
            pool.ReturnConnection(connection2);

            // Act
            pool.Dispose();

            // Assert
            connectionMock.Verify(x => x.Dispose(), Times.Exactly(2));
        }
예제 #6
0
        public void GetConnection_AfterReturnConnection_ShouldReturnSameConnection()
        {
            // Arrange
            var pool = new SapConnectionPool(
                ConnectionParameters,
                poolSize: 3,
                connectionFactory: _ => Mock.Of <ISapConnection>());

            // Act
            ISapConnection connection1 = pool.GetConnection();

            pool.ReturnConnection(connection1);
            ISapConnection connection2 = pool.GetConnection();

            // Assert
            connection1.Should().NotBeNull();
            connection2.Should().Be(connection1);
        }
예제 #7
0
        public void Wait_ShouldDisposeIdleConnections()
        {
            // Arrange
            var connectionMock = new Mock <ISapConnection>();
            var pool           = new SapConnectionPool(
                ConnectionParameters,
                connectionIdleTimeout: TimeSpan.FromMilliseconds(150),
                idleDetectionInterval: TimeSpan.FromMilliseconds(25),
                connectionFactory: _ => connectionMock.Object);

            pool.ReturnConnection(pool.GetConnection());

            // Assert
            connectionMock.Verify(x => x.Dispose(), Times.Never);

            // Act
            Thread.Sleep(200);

            // Assert
            connectionMock.Verify(x => x.Dispose(), Times.Once);
        }
예제 #8
0
        public void GetConnection_ConnectionFactoryReturnsNullFirst_ShouldRetryUntilFactoryReturnsConnection()
        {
            // Arrange
            ISapConnection firstConnection       = null;
            ISapConnection secondConnection      = Mock.Of <ISapConnection>();
            var            connectionFactoryMock = new Mock <Func <SapConnectionParameters, ISapConnection> >();

            connectionFactoryMock.SetupSequence(x => x(It.IsAny <SapConnectionParameters>())).Returns(firstConnection);
            connectionFactoryMock.SetupSequence(x => x(It.IsAny <SapConnectionParameters>())).Returns(secondConnection);

            var pool = new SapConnectionPool(
                ConnectionParameters,
                poolSize: 1,
                connectionFactory: connectionFactoryMock.Object);

            // Act
            ISapConnection connection = pool.GetConnection();

            // Assert
            connection.Should().Be(secondConnection);
        }
예제 #9
0
        public void ReturnConnection_ExceedPoolSize_GetConnectionShouldBlockAndReturnFirstReturnedConnection()
        {
            // Arrange
            var pool = new SapConnectionPool(
                ConnectionParameters,
                poolSize: 2,
                connectionFactory: _ => Mock.Of <ISapConnection>());

            ISapConnection connection1 = pool.GetConnection();
            ISapConnection connection2 = pool.GetConnection();

            // Act
            Task.Run(async() =>
            {
                await Task.Delay(150);
                pool.ReturnConnection(connection1);
                await Task.Delay(150);
                pool.ReturnConnection(connection2);
            });
            var sw = new Stopwatch();

            sw.Start();
            ISapConnection connection3 = pool.GetConnection();

            sw.Stop();

            // Assert
            sw.ElapsedMilliseconds.Should().BeGreaterThan(100).And.BeLessThan(250);
            connection3.Should().NotBeNull();
            connection3.Should().Be(connection1);

            connection1.Dispose();
            connection2.Dispose();
            connection3.Dispose();
            pool.Dispose();
        }