forked from duckdb/duckdb-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestClosure.java
More file actions
343 lines (324 loc) · 13.6 KB
/
TestClosure.java
File metadata and controls
343 lines (324 loc) · 13.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package org.duckdb;
import static org.duckdb.TestDuckDBJDBC.JDBC_URL;
import static org.duckdb.test.Assertions.*;
import java.io.File;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestClosure {
// https://github.com/duckdb/duckdb-java/issues/101
public static void test_unclosed_statement_does_not_hang() throws Exception {
String dbName = "test_issue_101.db";
String url = JDBC_URL + dbName;
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
stmt.execute("select 42");
// statement not closed explicitly
conn.close();
assertTrue(stmt.isClosed());
Connection connOther = DriverManager.getConnection(url);
connOther.close();
assertTrue(new File(dbName).delete());
}
public static void test_result_set_auto_closed() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
Statement stmt = conn.createStatement();
ResultSet rs1 = stmt.executeQuery("select 42");
ResultSet rs2 = stmt.executeQuery("select 43");
assertTrue(rs1.isClosed());
stmt.close();
assertTrue(rs2.isClosed());
}
}
public static void test_statements_auto_closed_on_conn_close() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt1 = conn.createStatement();
stmt1.execute("select 42");
PreparedStatement stmt2 = conn.prepareStatement("select 43");
stmt2.execute();
Statement stmt3 = conn.createStatement();
stmt3.execute("select 44");
stmt3.close();
conn.close();
assertTrue(stmt1.isClosed());
assertTrue(stmt2.isClosed());
}
public static void test_appender_auto_closed_on_conn_close() throws Exception {
DuckDBConnection conn = DriverManager.getConnection(JDBC_URL).unwrap(DuckDBConnection.class);
try (Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE tab1(col1 INT, col2 VARCHAR)");
}
DuckDBAppender appender = conn.createAppender("tab1");
appender.beginRow().append(42).append("foo").endRow().flush();
conn.close();
assertTrue(appender.isClosed());
}
public static void test_results_auto_closed_on_conn_close() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select 42");
rs.next();
conn.close();
assertTrue(rs.isClosed());
assertTrue(stmt.isClosed());
}
public static void test_statement_auto_closed_on_completion() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
Statement stmt = conn.createStatement();
stmt.closeOnCompletion();
assertTrue(stmt.isCloseOnCompletion());
try (ResultSet rs = stmt.executeQuery("select 42")) {
rs.next();
}
assertTrue(stmt.isClosed());
}
}
public static void test_long_query_conn_close() throws Exception {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE test_fib1(i bigint, p double, f double)");
stmt.execute("INSERT INTO test_fib1 values(1, 0, 1)");
long start = System.currentTimeMillis();
Thread th = new Thread(() -> {
try {
Thread.sleep(1000);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
});
th.start();
assertThrows(
()
-> stmt.executeQuery(
"WITH RECURSIVE cte AS ("
+
"SELECT * from test_fib1 UNION ALL SELECT cte.i + 1, cte.f, cte.p + cte.f from cte WHERE cte.i < 150000) "
+ "SELECT avg(f) FROM cte"),
SQLException.class);
th.join();
long elapsed = System.currentTimeMillis() - start;
assertTrue(elapsed < 2000);
assertTrue(stmt.isClosed());
assertTrue(conn.isClosed());
}
public static void test_long_query_stmt_close() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
Statement stmt = conn.createStatement();
stmt.execute("CREATE TABLE test_fib1(i bigint, p double, f double)");
stmt.execute("INSERT INTO test_fib1 values(1, 0, 1)");
long start = System.currentTimeMillis();
Thread th = new Thread(() -> {
try {
Thread.sleep(1000);
stmt.cancel();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
});
th.start();
assertThrows(
()
-> stmt.executeQuery(
"WITH RECURSIVE cte AS ("
+
"SELECT * from test_fib1 UNION ALL SELECT cte.i + 1, cte.f, cte.p + cte.f from cte WHERE cte.i < 150000) "
+ "SELECT avg(f) FROM cte"),
SQLException.class);
th.join();
long elapsed = System.currentTimeMillis() - start;
assertTrue(elapsed < 2000);
assertTrue(stmt.isClosed());
assertFalse(conn.isClosed());
}
}
public static void test_conn_close_no_crash() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 1 << 7; i++) {
Connection conn = DriverManager.getConnection(JDBC_URL);
Statement stmt = conn.createStatement();
Future<?> future = executor.submit(() -> {
try {
conn.close();
} catch (SQLException e) {
fail();
}
});
try {
stmt.executeQuery("select 42");
} catch (SQLException e) {
}
future.get();
}
}
public static void test_stmt_close_no_crash() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try (Connection conn = DriverManager.getConnection(JDBC_URL)) {
for (int i = 0; i < 1 << 10; i++) {
Statement stmt = conn.createStatement();
Future<?> future = executor.submit(() -> {
try {
stmt.close();
} catch (SQLException e) {
fail();
}
});
try {
stmt.executeQuery("select 42");
} catch (SQLException e) {
}
future.get();
}
}
}
public static void test_results_close_no_crash() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
for (int i = 0; i < 1 << 12; i++) {
ResultSet rs = stmt.executeQuery("select 42");
Future<?> future = executor.submit(() -> {
try {
rs.close();
} catch (SQLException e) {
fail();
}
});
try {
rs.next();
} catch (SQLException e) {
}
future.get();
}
}
}
public static void test_results_close_prepared_stmt_no_crash() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
try (Connection conn = DriverManager.getConnection(JDBC_URL);
PreparedStatement stmt = conn.prepareStatement("select 42")) {
for (int i = 0; i < 1 << 12; i++) {
ResultSet rs = stmt.executeQuery();
Future<?> future = executor.submit(() -> {
try {
rs.close();
} catch (SQLException e) {
fail();
}
});
try {
rs.next();
} catch (SQLException e) {
}
future.get();
}
}
}
@SuppressWarnings("try")
public static void test_results_fetch_no_hang() throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
Properties config = new Properties();
config.put(DuckDBDriver.JDBC_STREAM_RESULTS, true);
long rowsCount = 1 << 24;
int iterations = 1;
for (int i = 0; i < iterations; i++) {
try (Connection conn = DriverManager.getConnection(JDBC_URL, config);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT i, i::VARCHAR FROM range(0, " + rowsCount + ") AS t(i)")) {
executor.submit(() -> {
try {
Thread.sleep(100);
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
});
long[] resultsCount = new long[1];
assertThrows(() -> {
while (rs.next()) {
resultsCount[0]++;
}
}, SQLException.class);
assertTrue(resultsCount[0] > 0);
assertTrue(resultsCount[0] < rowsCount);
}
}
}
public static void test_stmt_can_only_cancel_self() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt1 = conn.createStatement();
Statement stmt2 = conn.createStatement()) {
stmt1.execute("DROP TABLE IF EXISTS test_fib1");
stmt1.execute("CREATE TABLE test_fib1(i bigint, p double, f double)");
stmt1.execute("INSERT INTO test_fib1 values(1, 0, 1)");
long start = System.currentTimeMillis();
Thread th = new Thread(() -> {
try {
Thread.sleep(200);
stmt1.cancel();
} catch (Exception e) {
e.printStackTrace();
}
});
th.start();
try (
ResultSet rs = stmt2.executeQuery(
"WITH RECURSIVE cte AS ("
+
"SELECT * from test_fib1 UNION ALL SELECT cte.i + 1, cte.f, cte.p + cte.f from cte WHERE cte.i < 50000) "
+ "SELECT avg(f) FROM cte")) {
rs.next();
assertTrue(rs.getDouble(1) > 0);
}
th.join();
long elapsed = System.currentTimeMillis() - start;
assertTrue(elapsed > 1000);
assertFalse(conn.isClosed());
assertFalse(stmt1.isClosed());
assertFalse(stmt2.isClosed());
}
}
public static void test_stmt_query_timeout() throws Exception {
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(1);
stmt.execute("CREATE TABLE test_fib1(i bigint, p double, f double)");
stmt.execute("INSERT INTO test_fib1 values(1, 0, 1)");
long start = System.currentTimeMillis();
assertThrows(
()
-> stmt.executeQuery(
"WITH RECURSIVE cte AS ("
+
"SELECT * from test_fib1 UNION ALL SELECT cte.i + 1, cte.f, cte.p + cte.f from cte WHERE cte.i < 150000) "
+ "SELECT avg(f) FROM cte"),
SQLTimeoutException.class);
long elapsed = System.currentTimeMillis() - start;
assertTrue(elapsed < 1500);
assertFalse(conn.isClosed());
assertTrue(stmt.isClosed());
assertEquals(DuckDBDriver.scheduler.getQueue().size(), 0);
}
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(1);
assertThrows(() -> { stmt.execute("FAIL"); }, SQLException.class);
assertEquals(DuckDBDriver.scheduler.getQueue().size(), 0);
}
}
public static void manual_test_set_query_timeout_wo_scheduler() throws Exception {
assertTrue(DuckDBDriver.shutdownQueryCancelScheduler());
assertFalse(DuckDBDriver.shutdownQueryCancelScheduler());
try (Connection conn = DriverManager.getConnection(JDBC_URL); Statement stmt = conn.createStatement()) {
stmt.setQueryTimeout(1);
stmt.execute("CREATE TABLE test_fib1(i bigint, p double, f double)");
stmt.execute("INSERT INTO test_fib1 values(1, 0, 1)");
long start = System.currentTimeMillis();
stmt.executeQuery(
"WITH RECURSIVE cte AS ("
+
"SELECT * from test_fib1 UNION ALL SELECT cte.i + 1, cte.f, cte.p + cte.f from cte WHERE cte.i < 50000) "
+ "SELECT avg(f) FROM cte");
long elapsed = System.currentTimeMillis() - start;
assertTrue(elapsed > 1500);
}
}
}