Repository navigation
Expand file tree
/
Copy pathSetterCorePoolLeakIT.java
More file actions
115 lines (102 loc) · 4.02 KB
/
Copy pathSetterCorePoolLeakIT.java
File metadata and controls
115 lines (102 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package dbProcs;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.IOException;
import java.sql.SQLException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import testUtils.TestProperties;
/**
* Verifies that high-frequency Setter core DB calls return connections to the Hikari pool (no
* unbounded growth in active connections). Mirrors GetterCorePoolLeakIT.
*/
public class SetterCorePoolLeakIT {
private static final Logger log = LogManager.getLogger(SetterCorePoolLeakIT.class);
private static boolean databaseAvailable = false;
private static final String applicationRoot = "";
@BeforeAll
public static void setup() throws IOException, SQLException {
TestProperties.setTestPropertiesFileDirectory(log);
TestProperties.createMysqlResource();
TestProperties.ensureSchemaReady(log);
TestProperties.reseedTestData();
try {
ConnectionPool.initialize();
Getter.getClassCount(applicationRoot);
databaseAvailable = ConnectionPool.isInitialized();
} catch (Exception e) {
log.warn("Database not available for SetterCorePoolLeakIT: {}", e.getMessage());
databaseAvailable = false;
}
}
@AfterAll
public static void cleanup() {
ConnectionPool.shutdown();
}
private void requireDatabase() {
assumeTrue(databaseAvailable, "Database not available");
}
/**
* resetBadSubmission is a representative core write: it borrows a connection, runs a stored
* procedure, returns. With a nonexistent userId the procedure is a no-op, so we can hammer it
* safely. Pre-fix, the success path called Database.closeConnection and returned the connection,
* but any thrown SQLException (or early return) skipped that line and leaked the connection —
* exactly the failure mode the try-with-resources conversion fixes.
*
* <p>The loop is serial, so a non-leaking pool must return active count to the pre-call baseline
* after every iteration. Asserting equality with baseline (typically 0) detects leaks
* deterministically — a hardcoded ceiling can still pass while the pool saturates.
*/
@Test
public void repeatedResetBadSubmissionDoesNotExhaustCorePool() {
requireDatabase();
int baseline = ConnectionPool.getCoreActiveConnections();
assertTrue(baseline >= 0);
for (int i = 0; i < 500; i++) {
Setter.resetBadSubmission(applicationRoot, "nonexistent_pool_leak_user_" + i);
}
int active = ConnectionPool.getCoreActiveConnections();
log.debug(
"SetterCorePoolLeakIT: baseline active={}, after 500 resetBadSubmission calls active={}",
baseline,
active);
assertEquals(
baseline,
active,
"Core pool active connections should return to baseline after a serial loop; got "
+ active
+ " (baseline "
+ baseline
+ ")");
}
/**
* suspendUser exercises a different code path (the catch-and-return-false branch on most calls
* since the userId does not exist). Verifies the exception path also returns the connection.
*/
@Test
public void repeatedSuspendUserDoesNotExhaustCorePool() {
requireDatabase();
int baseline = ConnectionPool.getCoreActiveConnections();
assertTrue(baseline >= 0);
for (int i = 0; i < 500; i++) {
Setter.suspendUser(applicationRoot, "nonexistent_pool_leak_user_" + i, 1);
}
int active = ConnectionPool.getCoreActiveConnections();
log.debug(
"SetterCorePoolLeakIT: baseline active={}, after 500 suspendUser calls active={}",
baseline,
active);
assertEquals(
baseline,
active,
"Core pool active connections should return to baseline after a serial loop; got "
+ active
+ " (baseline "
+ baseline
+ ")");
}
}