You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.5 KiB
116 lines
4.5 KiB
|
|
|
|
/**
|
|
* Test class: Verify object pool manager functionality
|
|
* @author chuyang
|
|
* @version 1.0
|
|
*/
|
|
public class ConnectionPoolTest {
|
|
|
|
public ConnectionPoolManager m_ConnectionPoolManager;
|
|
|
|
public ConnectionPoolTest(){
|
|
|
|
}
|
|
|
|
@Override
|
|
public void finalize() throws Throwable {
|
|
|
|
}
|
|
/**
|
|
* Main test method
|
|
* @param args
|
|
*/
|
|
public static void main(String[] args) {
|
|
System.out.println("=== Start Object Pool Manager Test ===");
|
|
|
|
// 1. Test initializing connection pool
|
|
System.out.println("\n1. Initialize connection pool");
|
|
ConnectionPoolManager poolManager = new ConnectionPoolManager(2, 5, "jdbc:mysql://localhost:3306/testdb");
|
|
System.out.println(poolManager.getPoolStatus());
|
|
|
|
// 2. Test borrowing connections
|
|
System.out.println("\n2. Test borrowing connections");
|
|
DatabaseConnection conn1 = poolManager.borrowConnection();
|
|
DatabaseConnection conn2 = poolManager.borrowConnection();
|
|
DatabaseConnection conn3 = poolManager.borrowConnection(); // Should create new connection
|
|
System.out.println(poolManager.getPoolStatus());
|
|
|
|
// 3. Test returning connections
|
|
System.out.println("\n3. Test returning connections");
|
|
poolManager.returnConnection(conn1);
|
|
System.out.println(poolManager.getPoolStatus());
|
|
|
|
// 4. Test reusing connections
|
|
System.out.println("\n4. Test reusing connections");
|
|
DatabaseConnection conn4 = poolManager.borrowConnection(); // Should reuse just returned connection
|
|
System.out.println("Reused connection ID: " + conn4.getConnectionId());
|
|
System.out.println(poolManager.getPoolStatus());
|
|
|
|
// 5. Test pool full condition
|
|
System.out.println("\n5. Test pool full condition");
|
|
DatabaseConnection conn5 = poolManager.borrowConnection(); // Create 4th connection
|
|
DatabaseConnection conn6 = poolManager.borrowConnection(); // Create 5th connection (reach max capacity)
|
|
DatabaseConnection conn7 = poolManager.borrowConnection(); // Should return null as pool is full
|
|
System.out.println("Result of borrowing beyond max capacity: " + (conn7 == null ? "Pool is full, cannot borrow more connections" : "Successfully borrowed connection"));
|
|
System.out.println(poolManager.getPoolStatus());
|
|
|
|
// 6. Test shrinking pool
|
|
System.out.println("\n6. Test shrinking pool");
|
|
// Return some connections for shrinking
|
|
poolManager.returnConnection(conn2);
|
|
poolManager.returnConnection(conn3);
|
|
poolManager.returnConnection(conn5);
|
|
poolManager.returnConnection(conn6);
|
|
System.out.println("Before shrink " + poolManager.getPoolStatus());
|
|
poolManager.shrinkPool();
|
|
System.out.println("After shrink " + poolManager.getPoolStatus());
|
|
|
|
// 7. Test thread safety (simple test)
|
|
System.out.println("\n7. Simple thread safety test");
|
|
ConnectionPoolManager threadTestPool = new ConnectionPoolManager(3, 10, "jdbc:mysql://localhost:3306/testdb");
|
|
|
|
// Create several threads to borrow and return connections simultaneously
|
|
Thread[] threads = new Thread[5];
|
|
for (int i = 0; i < threads.length; i++) {
|
|
final int threadId = i;
|
|
threads[i] = new Thread(() -> {
|
|
try {
|
|
System.out.println("Thread " + threadId + " borrowing connection");
|
|
DatabaseConnection conn = threadTestPool.borrowConnection();
|
|
if (conn != null) {
|
|
System.out.println("Thread " + threadId + " got connection: " + conn.getConnectionId());
|
|
// Simulate using connection
|
|
Thread.sleep(200);
|
|
threadTestPool.returnConnection(conn);
|
|
System.out.println("Thread " + threadId + " returned connection: " + conn.getConnectionId());
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
threads[i].start();
|
|
}
|
|
|
|
// Wait for all threads to complete
|
|
for (Thread thread : threads) {
|
|
try {
|
|
thread.join();
|
|
} catch (InterruptedException e) {
|
|
Thread.currentThread().interrupt();
|
|
}
|
|
}
|
|
|
|
System.out.println("\nAfter thread test " + threadTestPool.getPoolStatus());
|
|
|
|
// 8. Test DatabaseConnection clone functionality
|
|
System.out.println("\n8. Test connection clone functionality");
|
|
DatabaseConnection originalConn = new DatabaseConnection("original-clone-test", "jdbc:mysql://localhost:3306/clonedb");
|
|
DatabaseConnection clonedConn = originalConn.clone();
|
|
System.out.println("Original connection ID: " + originalConn.getConnectionId());
|
|
System.out.println("Cloned connection ID: " + clonedConn.getConnectionId());
|
|
System.out.println("Cloned connection URL: " + clonedConn.getDatabaseUrl());
|
|
|
|
System.out.println("\n=== Object Pool Manager Test Completed ===");
|
|
}
|
|
} |