|
| 1 | +/* |
| 2 | + * Copyright 2009-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.ibatis.datasource.pooled; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.sql.Connection; |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | + |
| 28 | +import static org.junit.jupiter.api.Assertions.*; |
| 29 | + |
| 30 | +class PooledDataSourceTest { |
| 31 | + |
| 32 | + PooledDataSource dataSource; |
| 33 | + |
| 34 | + @BeforeEach |
| 35 | + void beforeEach() { |
| 36 | + dataSource = new PooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers", "sa", ""); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void shouldBlockUntilConnectionIsAvailableInPooledDataSource() throws Exception { |
| 41 | + dataSource.setPoolMaximumCheckoutTime(20000); |
| 42 | + |
| 43 | + List<Connection> connections = new ArrayList<>(); |
| 44 | + CountDownLatch latch = new CountDownLatch(1); |
| 45 | + |
| 46 | + for (int i = 0; i < dataSource.getPoolMaximumActiveConnections(); i++) { |
| 47 | + connections.add(dataSource.getConnection()); |
| 48 | + } |
| 49 | + |
| 50 | + new Thread(() -> { |
| 51 | + try { |
| 52 | + dataSource.getConnection(); |
| 53 | + latch.countDown(); |
| 54 | + } catch (Exception e) { |
| 55 | + throw new RuntimeException(e); |
| 56 | + } |
| 57 | + }).start(); |
| 58 | + |
| 59 | + assertFalse(latch.await(1000, TimeUnit.MILLISECONDS)); |
| 60 | + connections.get(0).close(); |
| 61 | + assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void PoppedConnectionShouldBeNotEqualToClosedConnection() throws Exception { |
| 66 | + Connection connectionToClose = dataSource.getConnection(); |
| 67 | + |
| 68 | + new Thread(() -> { |
| 69 | + try { |
| 70 | + assertNotEquals(connectionToClose, dataSource.getConnection()); |
| 71 | + } catch (Exception e) { |
| 72 | + throw new RuntimeException(e); |
| 73 | + } |
| 74 | + }).start(); |
| 75 | + |
| 76 | + connectionToClose.close(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void shouldEnsureCorrectIdleConnectionCount() throws Exception { |
| 81 | + dataSource.setPoolMaximumActiveConnections(10); |
| 82 | + dataSource.setPoolMaximumIdleConnections(5); |
| 83 | + |
| 84 | + PoolState poolState = dataSource.getPoolState(); |
| 85 | + List<Connection> connections = new ArrayList<>(); |
| 86 | + |
| 87 | + for (int i = 0; i < dataSource.getPoolMaximumActiveConnections(); i++) { |
| 88 | + connections.add(dataSource.getConnection()); |
| 89 | + } |
| 90 | + |
| 91 | + assertEquals(0, poolState.getIdleConnectionCount()); |
| 92 | + |
| 93 | + for (int i = 0; i < dataSource.getPoolMaximumActiveConnections(); i++) { |
| 94 | + connections.get(i).close(); |
| 95 | + } |
| 96 | + |
| 97 | + assertEquals(dataSource.getPoolMaximumIdleConnections(), poolState.getIdleConnectionCount()); |
| 98 | + |
| 99 | + for (int i = 0; i < dataSource.getPoolMaximumIdleConnections(); i++) { |
| 100 | + dataSource.getConnection(); |
| 101 | + } |
| 102 | + |
| 103 | + assertEquals(0, poolState.getIdleConnectionCount()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void connectionShouldBeAvailableAfterMaximumCheckoutTime() throws Exception { |
| 108 | + dataSource.setPoolMaximumCheckoutTime(1000); |
| 109 | + dataSource.setPoolTimeToWait(500); |
| 110 | + |
| 111 | + int poolMaximumActiveConnections = dataSource.getPoolMaximumActiveConnections(); |
| 112 | + CountDownLatch latch = new CountDownLatch(1); |
| 113 | + |
| 114 | + for (int i = 0; i < poolMaximumActiveConnections; i++) { |
| 115 | + dataSource.getConnection(); |
| 116 | + } |
| 117 | + |
| 118 | + new Thread(() -> { |
| 119 | + try { |
| 120 | + dataSource.getConnection(); |
| 121 | + latch.countDown(); |
| 122 | + } catch (Exception e) { |
| 123 | + throw new RuntimeException(e); |
| 124 | + } |
| 125 | + }).start(); |
| 126 | + |
| 127 | + assertTrue(latch.await(5000, TimeUnit.MILLISECONDS)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void forceCloseAllShouldRemoveAllActiveAndIdleConnection() throws SQLException { |
| 132 | + dataSource.setPoolMaximumActiveConnections(10); |
| 133 | + dataSource.setPoolMaximumIdleConnections(5); |
| 134 | + |
| 135 | + PoolState poolState = dataSource.getPoolState(); |
| 136 | + List<Connection> connections = new ArrayList<>(); |
| 137 | + |
| 138 | + for (int i = 0; i < dataSource.getPoolMaximumActiveConnections(); i++) { |
| 139 | + connections.add(dataSource.getConnection()); |
| 140 | + } |
| 141 | + |
| 142 | + for (int i = 0; i < dataSource.getPoolMaximumIdleConnections(); i++) { |
| 143 | + connections.get(i).close(); |
| 144 | + } |
| 145 | + |
| 146 | + assertEquals(dataSource.getPoolMaximumActiveConnections() - poolState.getIdleConnectionCount(), |
| 147 | + poolState.getActiveConnectionCount()); |
| 148 | + assertEquals(dataSource.getPoolMaximumIdleConnections(), |
| 149 | + poolState.getIdleConnectionCount()); |
| 150 | + |
| 151 | + dataSource.forceCloseAll(); |
| 152 | + |
| 153 | + assertEquals(0, poolState.getActiveConnectionCount()); |
| 154 | + assertEquals(0, poolState.getIdleConnectionCount()); |
| 155 | + } |
| 156 | +} |
0 commit comments