Skip to content

Commit

Permalink
Enabled a disabled test
Browse files Browse the repository at this point in the history
Moved the test to a new file to add `@Tag`.
  • Loading branch information
harawata committed Jul 24, 2021
1 parent 335a72c commit ca897f8
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2009-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.datasource.pooled;

import static org.junit.jupiter.api.Assertions.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;

import org.apache.ibatis.testcontainers.MysqlContainer;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("TestcontainersTests")
public class MysqlTimeoutTest {

@Test
void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
// See #748
PooledDataSource ds = MysqlContainer.getPooledDataSource();
ds.setPoolMaximumActiveConnections(1);
ds.setPoolMaximumIdleConnections(1);
ds.setPoolTimeToWait(1000);
ds.setPoolMaximumCheckoutTime(2000);
ds.setPoolPingEnabled(true);
ds.setPoolPingQuery("select 1");
ds.setDefaultAutoCommit(true);
// MySQL wait_timeout * 1000 or less. (unit:ms)
ds.setPoolPingConnectionsNotUsedFor(1000);

Connection con1 = ds.getConnection();

Statement stmt = con1.createStatement();
stmt.execute("set session wait_timeout = 3");

executeQuery(con1);
// Simulate connection leak by not closing.
// con1.close();

// Wait for disconnected from mysql...
Thread.sleep(TimeUnit.SECONDS.toMillis(3));

// Should return usable connection.
Connection con2 = ds.getConnection();
executeQuery(con2);

con1.close();
con2.close();
}

private void executeQuery(Connection con) throws SQLException {
try (PreparedStatement st = con.prepareStatement("select 1");
ResultSet rs = st.executeQuery()) {
while (rs.next()) {
assertEquals(1, rs.getInt(1));
}
}
}

}
59 changes: 0 additions & 59 deletions src/test/java/org/apache/ibatis/jdbc/PooledDataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,13 @@
import static org.junit.jupiter.api.Assertions.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.hsqldb.jdbc.JDBCConnection;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

class PooledDataSourceTest extends BaseDataTest {
Expand Down Expand Up @@ -90,58 +85,4 @@ void ShouldReturnRealConnection() throws Exception {
JDBCConnection realConnection = (JDBCConnection) PooledDataSource.unwrapConnection(c);
c.close();
}

@Disabled("See the comments")
@Test
void shouldReconnectWhenServerKilledLeakedConnection() throws Exception {
// See #748
// Requirements:
// 1. MySQL JDBC driver dependency.
// 2. MySQL server instance with the following.
// - CREATE DATABASE `test`;
// - SET GLOBAL wait_timeout=3;
// 3. Tweak the connection info below.
final String URL = "jdbc:mysql://localhost:3306/test";
final String USERNAME = "admin";
final String PASSWORD = "";

PooledDataSource ds = new PooledDataSource();
ds.setDriver("com.mysql.jdbc.Driver");
ds.setUrl(URL);
ds.setUsername(USERNAME);
ds.setPassword(PASSWORD);
ds.setPoolMaximumActiveConnections(1);
ds.setPoolMaximumIdleConnections(1);
ds.setPoolTimeToWait(1000);
ds.setPoolMaximumCheckoutTime(2000);
ds.setPoolPingEnabled(true);
ds.setPoolPingQuery("select 1");
ds.setDefaultAutoCommit(true);
// MySQL wait_timeout * 1000 or less. (unit:ms)
ds.setPoolPingConnectionsNotUsedFor(1000);

Connection con = ds.getConnection();
executeQuery(con);
// Simulate connection leak by not closing.
// con.close();

// Wait for disconnected from mysql...
Thread.sleep(TimeUnit.SECONDS.toMillis(3));

con.close();

// Should return usable connection.
con = ds.getConnection();
executeQuery(con);
con.close();
}

private void executeQuery(Connection con) throws SQLException {
try (PreparedStatement st = con.prepareStatement("select 1");
ResultSet rs = st.executeQuery()) {
while (rs.next()) {
assertEquals(1, rs.getInt(1));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import javax.sql.DataSource;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.testcontainers.containers.MySQLContainer;

Expand All @@ -42,6 +43,11 @@ public static DataSource getUnpooledDataSource() {
MysqlContainer.PASSWORD);
}

public static PooledDataSource getPooledDataSource() {
return new PooledDataSource(MysqlContainer.DRIVER, INSTANCE.getJdbcUrl(), MysqlContainer.USERNAME,
MysqlContainer.PASSWORD);
}

private MysqlContainer() {
super();
}
Expand Down

0 comments on commit ca897f8

Please sign in to comment.