Ejemplo n.º 1
0
        public void ReturnConnection_ExceedPoolSize_GetConnectionShouldBlockAndReturnPreviousConnection()
        {
            // 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.ReturnConnection(connection1);
            });
            var sw = new Stopwatch();

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

            sw.Stop();

            // Assert
            sw.ElapsedMilliseconds.Should().BeGreaterThan(100);
            connection2.Should().NotBeNull();
            connection2.Should().Be(connection1);

            connection1.Dispose();
            connection2.Dispose();
            pool.Dispose();
        }
Ejemplo n.º 2
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));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }