forked from xerial/sqlite-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBusyHandlerTest.java
More file actions
167 lines (147 loc) · 5.26 KB
/
Copy pathBusyHandlerTest.java
File metadata and controls
167 lines (147 loc) · 5.26 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
package org.sqlite;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class BusyHandlerTest {
private Connection conn;
private Statement stat;
@BeforeEach
public void connect() throws Exception {
conn = DriverManager.getConnection("jdbc:sqlite:target/test.db");
stat = conn.createStatement();
}
@AfterEach
public void close() throws SQLException {
stat.close();
conn.close();
}
public class BusyWork extends Thread {
private final Connection conn;
private final Statement stat;
private final CountDownLatch lockedLatch = new CountDownLatch(1);
private final CountDownLatch completeLatch = new CountDownLatch(1);
public BusyWork() throws Exception {
conn = DriverManager.getConnection("jdbc:sqlite:target/test.db");
Function.create(
conn,
"wait_for_latch",
new Function() {
@Override
protected void xFunc() throws SQLException {
lockedLatch.countDown();
try {
completeLatch.await();
} catch (InterruptedException e) {
throw new SQLException("Interrupted");
}
result(100);
}
});
stat = conn.createStatement();
stat.setQueryTimeout(1);
}
@Override
public void run() {
try {
// Generate some work for the sqlite vm
stat.executeUpdate("drop table if exists foo;");
stat.executeUpdate("create table foo (id integer);");
stat.execute("insert into foo (id) values (wait_for_latch());");
} catch (SQLException ex) {
System.out.println("HERE" + ex.toString());
}
}
}
private void workWork() throws SQLException {
// Generate some work for the sqlite vm
int i = 0;
while (i < 5) {
stat.execute("insert into foo (id) values (" + i + ")");
i++;
}
}
@Test
public void basicBusyHandler() throws Exception {
final int[] calls = {0};
BusyHandler.setHandler(
conn,
new BusyHandler() {
@Override
protected int callback(int nbPrevInvok) throws SQLException {
assertEquals(nbPrevInvok, calls[0]);
calls[0]++;
if (nbPrevInvok <= 1) {
return 1;
} else {
return 0;
}
}
});
BusyWork busyWork = new BusyWork();
busyWork.start();
// let busyWork block inside insert
busyWork.lockedLatch.await();
try {
workWork();
fail("Should throw SQLITE_BUSY exception");
} catch (SQLException ex) {
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
}
busyWork.completeLatch.countDown();
busyWork.join();
assertEquals(3, calls[0]);
}
@Test
public void testUnregister() throws Exception {
final int[] calls = {0};
BusyHandler.setHandler(
conn,
new BusyHandler() {
@Override
protected int callback(int nbPrevInvok) throws SQLException {
assertEquals(nbPrevInvok, calls[0]);
calls[0]++;
if (nbPrevInvok <= 1) {
return 1;
} else {
return 0;
}
}
});
BusyWork busyWork = new BusyWork();
busyWork.start();
// let busyWork block inside insert
busyWork.lockedLatch.await();
try {
workWork();
fail("Should throw SQLITE_BUSY exception");
} catch (SQLException ex) {
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
}
busyWork.completeLatch.countDown();
busyWork.join();
assertEquals(3, calls[0]);
int totalCalls = calls[0];
BusyHandler.clearHandler(conn);
busyWork = new BusyWork();
busyWork.start();
// let busyWork block inside insert
busyWork.lockedLatch.await();
try {
workWork();
fail("Should throw SQLITE_BUSY exception");
} catch (SQLException ex) {
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
}
busyWork.completeLatch.countDown();
busyWork.join();
assertEquals(totalCalls, calls[0]);
}
}