1
1
import datadog.trace.agent.test.AgentTestRunner
2
2
import com.zaxxer.hikari.HikariConfig
3
3
import com.zaxxer.hikari.HikariDataSource
4
+ import org.apache.commons.dbcp2.BasicDataSource
4
5
import test.TestDataSource
6
+ import test.TestDriver
5
7
8
+ import javax.sql.DataSource
9
+ import java.sql.SQLException
6
10
import java.sql.SQLTimeoutException
7
11
import java.sql.SQLTransientConnectionException
12
+ import java.time.Duration
8
13
9
14
/**
10
15
* Ideas taken from Hikari's com.zaxxer.hikari.pool.TestSaturatedPool830.
11
16
*/
12
17
class SaturatedPoolBlockingTest extends AgentTestRunner {
13
- def " saturated pool test" (int connectionTimeout, Long exhaustPoolForMillis, int expectedWaitingSpans, boolean expectedTimeout) {
18
+ public static final int CONNECTION_TIMEOUT = 1000
19
+
20
+ def " saturated pool test" (Closure<DataSource > createDataSource, Long exhaustPoolForMillis, int expectedWaitingSpans, boolean expectedTimeout) {
14
21
setup :
15
22
TEST_WRITER . setFilter((trace) -> trace. get(0 ). getOperationName() == " test.when" )
16
-
17
- final HikariConfig config = new HikariConfig ()
18
- config. setPoolName(" testPool" )
19
- config. setMaximumPoolSize(1 )
20
- config. setConnectionTimeout(connectionTimeout)
21
- config. setDataSourceClassName(TestDataSource . class. getName())
22
- final HikariDataSource ds = new HikariDataSource (config)
23
+ final DataSource ds = createDataSource()
23
24
24
25
when :
25
26
if (exhaustPoolForMillis != null ) {
@@ -43,6 +44,12 @@ class SaturatedPoolBlockingTest extends AgentTestRunner {
43
44
}
44
45
} catch (SQLTimeoutException ignored) { // Hikari, older
45
46
timedOut = true
47
+ } catch (SQLException e) {
48
+ if (e. getMessage(). contains(" pool error Timeout waiting for idle object" )) { // dbcp2
49
+ timedOut = true
50
+ } else {
51
+ throw e
52
+ }
46
53
}
47
54
span. finish()
48
55
@@ -59,10 +66,32 @@ class SaturatedPoolBlockingTest extends AgentTestRunner {
59
66
}
60
67
61
68
where :
62
- connectionTimeout | exhaustPoolForMillis | expectedWaitingSpans | expectedTimeout
63
- 1000 | null | 0 | false
64
- 1000 | null | 0 | false
65
- 1000 | 500 | 1 | false
66
- 1000 | 1500 | 1 | true
69
+ createDataSource | exhaustPoolForMillis | expectedWaitingSpans | expectedTimeout
70
+ this . &hikariDataSource | null | 0 | false
71
+ this . &hikariDataSource | null | 0 | false
72
+ this . &hikariDataSource | 500 | 1 | false
73
+ this . &hikariDataSource | 1500 | 1 | true
74
+ this . &dbcp2DataSource | null | 0 | false
75
+ this . &dbcp2DataSource | null | 0 | false
76
+ this . &dbcp2DataSource | 500 | 1 | false
77
+ this . &dbcp2DataSource | 1500 | 1 | true
78
+ }
79
+
80
+ private static DataSource hikariDataSource () {
81
+ final HikariConfig config = new HikariConfig ()
82
+ config. setPoolName(" testPool" )
83
+ config. setMaximumPoolSize(1 )
84
+ config. setConnectionTimeout(CONNECTION_TIMEOUT )
85
+ config. setDataSourceClassName(TestDataSource . class. getName())
86
+ return new HikariDataSource (config)
87
+ }
88
+
89
+ private static DataSource dbcp2DataSource () {
90
+ final BasicDataSource ds = new BasicDataSource ()
91
+ ds. setMaxTotal(1 )
92
+ ds. setMaxWait(Duration . ofMillis(CONNECTION_TIMEOUT ))
93
+ ds. setDriverClassName(TestDriver . class. getName())
94
+ ds. start()
95
+ return ds
67
96
}
68
97
}
0 commit comments