Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c7c5604

Browse filesBrowse files
snagoxerial
authored andcommitted
Fix some tests (xerial#377)
* BusyHandlerTest should now be deterministic. * Removed BusyHandlerTest.testInterrupt as it probably didn't test what it was supposed to. The other two tests should be enough. (The "expected" SQLException was a "query does not return ResultSet" error because of executeQuery for insert instead of execute.) * More verification in SQLiteJDBCLoaderTest. * Make tests faster (run all tests in less than 2s instead of 20s).
1 parent 2df4a6c commit c7c5604
Copy full SHA for c7c5604

3 files changed

+56-67Lines changed: 56 additions & 67 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/test/java/org/sqlite/BusyHandlerTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/sqlite/BusyHandlerTest.java
+49-67Lines changed: 49 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.sql.DriverManager;
99
import java.sql.SQLException;
1010
import java.sql.Statement;
11+
import java.util.concurrent.CountDownLatch;
12+
1113
import static org.junit.Assert.assertEquals;
1214

1315
import static org.junit.Assert.fail;
@@ -31,45 +33,43 @@ public void close() throws SQLException {
3133
public class BusyWork extends Thread {
3234
private final Connection conn;
3335
private final Statement stat;
36+
private final CountDownLatch lockedLatch = new CountDownLatch(1);
37+
private final CountDownLatch completeLatch = new CountDownLatch(1);
3438

3539
public BusyWork() throws Exception {
3640
conn = DriverManager.getConnection("jdbc:sqlite:target/test.db");
41+
Function.create(conn, "wait_for_latch", new Function() {
42+
@Override
43+
protected void xFunc() throws SQLException {
44+
lockedLatch.countDown();
45+
try {
46+
completeLatch.await();
47+
} catch (InterruptedException e) {
48+
throw new SQLException("Interrupted");
49+
}
50+
result(100);
51+
}
52+
});
3753
stat = conn.createStatement();
3854
stat.setQueryTimeout(1);
3955
}
4056

4157
@Override
4258
public void run(){
43-
4459
try {
45-
synchronized(this) {
46-
// Generate some work for the sqlite vm
47-
stat.executeUpdate("drop table if exists foo;");
48-
stat.executeUpdate("create table foo (id integer);");
49-
50-
int i = 0;
51-
while (i<10000) {
52-
String stmt = "insert into foo (id) values (" + i + ")";
53-
stat.addBatch(stmt);
54-
i++;
55-
}
56-
}
60+
// Generate some work for the sqlite vm
61+
stat.executeUpdate("drop table if exists foo;");
62+
stat.executeUpdate("create table foo (id integer);");
63+
stat.execute("insert into foo (id) values (wait_for_latch());");
5764
} catch (SQLException ex) {System.out.println("HERE"+ex.toString());}
58-
59-
try {
60-
stat.executeBatch();
61-
} catch (SQLException ex) {System.out.println("BLOB"+ex.toString());}
6265
}
6366
}
6467

65-
private void workWork() throws SQLException, InterruptedException {
66-
// I let busyWork inject first so it can busy the db
67-
Thread.sleep(1000);
68-
68+
private void workWork() throws SQLException {
6969
// Generate some work for the sqlite vm
7070
int i = 0;
7171
while (i<5) {
72-
stat.executeQuery("insert into foo (id) values (" + i + ")");
72+
stat.execute("insert into foo (id) values (" + i + ")");
7373
i++;
7474
}
7575
}
@@ -94,18 +94,18 @@ protected int callback(int nbPrevInvok) throws SQLException {
9494
BusyWork busyWork = new BusyWork();
9595
busyWork.start();
9696

97-
// I let busyWork prepare a huge insert
98-
Thread.sleep(1000);
97+
// let busyWork block inside insert
98+
busyWork.lockedLatch.await();
9999

100-
synchronized(busyWork){
101-
try{
102-
workWork();
103-
} catch(SQLException ex) {
104-
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
105-
}
100+
try{
101+
workWork();
102+
fail("Should throw SQLITE_BUSY exception");
103+
} catch(SQLException ex) {
104+
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
106105
}
107106

108-
busyWork.interrupt();
107+
busyWork.completeLatch.countDown();
108+
busyWork.join();
109109
assertEquals(3, calls[0]);
110110
}
111111

@@ -128,51 +128,33 @@ protected int callback(int nbPrevInvok) throws SQLException {
128128

129129
BusyWork busyWork = new BusyWork();
130130
busyWork.start();
131-
// I let busyWork prepare a huge insert
132-
Thread.sleep(1000);
133-
synchronized(busyWork){
134-
try{
135-
workWork();
136-
} catch(SQLException ex) {
137-
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
138-
}
131+
// let busyWork block inside insert
132+
busyWork.lockedLatch.await();
133+
try{
134+
workWork();
135+
fail("Should throw SQLITE_BUSY exception");
136+
} catch(SQLException ex) {
137+
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
139138
}
139+
busyWork.completeLatch.countDown();
140+
busyWork.join();
140141
assertEquals(3, calls[0]);
141142

142143
int totalCalls = calls[0];
143144
BusyHandler.clearHandler(conn);
144145
busyWork = new BusyWork();
145146
busyWork.start();
146-
// I let busyWork prepare a huge insert
147-
Thread.sleep(1000);
148-
synchronized(busyWork){
149-
try{
150-
workWork();
151-
} catch(SQLException ex) {
152-
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
153-
}
147+
// let busyWork block inside insert
148+
busyWork.lockedLatch.await();
149+
try{
150+
workWork();
151+
fail("Should throw SQLITE_BUSY exception");
152+
} catch(SQLException ex) {
153+
assertEquals(SQLiteErrorCode.SQLITE_BUSY.code, ex.getErrorCode());
154154
}
155155

156-
busyWork.interrupt();
156+
busyWork.completeLatch.countDown();
157+
busyWork.join();
157158
assertEquals(totalCalls, calls[0]);
158159
}
159-
160-
@Test
161-
public void testInterrupt() throws Exception {
162-
163-
try {
164-
BusyHandler.setHandler(conn, new BusyHandler() {
165-
@Override
166-
protected int callback(int nbPrevInvok) throws SQLException {
167-
return 1;
168-
}
169-
});
170-
workWork();
171-
} catch (SQLException ex) {
172-
// Expected error
173-
return;
174-
}
175-
// Progress function throws, not reached
176-
fail();
177-
}
178160
}
Collapse file

‎src/test/java/org/sqlite/SQLiteJDBCLoaderTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/sqlite/SQLiteJDBCLoaderTest.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.concurrent.ExecutorService;
3737
import java.util.concurrent.Executors;
3838
import java.util.concurrent.TimeUnit;
39+
import java.util.concurrent.atomic.AtomicInteger;
3940

4041
import org.junit.After;
4142
import org.junit.Assert;
@@ -117,6 +118,7 @@ public void version()
117118

118119
@Test
119120
public void test() throws Throwable {
121+
final AtomicInteger completedThreads = new AtomicInteger(0);
120122
ExecutorService pool = Executors.newFixedThreadPool(32);
121123
for (int i = 0; i < 32; i++) {
122124
final String connStr = "jdbc:sqlite:target/sample-" + i + ".db";
@@ -136,9 +138,12 @@ public void run() {
136138
e.printStackTrace();
137139
Assert.fail(e.getLocalizedMessage());
138140
}
141+
completedThreads.incrementAndGet();
139142
}
140143
});
141144
}
145+
pool.shutdown();
142146
pool.awaitTermination(3, TimeUnit.SECONDS);
147+
assertEquals(32, completedThreads.get());
143148
}
144149
}
Collapse file

‎src/test/java/org/sqlite/TransactionTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/org/sqlite/TransactionTest.java
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ private void failedUpdatedPreventedFutureRollback(boolean prepared) throws SQLEx
8080
// Second transaction starts and tries to complete but fails because first is still running
8181
boolean gotException = false;
8282
try {
83+
((SQLiteConnection) conn2).setBusyTimeout(10);
8384
conn2.setAutoCommit(false);
8485
if (pstat2 != null) {
8586
// The prepared case would fail regardless of whether this was "execute" or "executeUpdate"
@@ -308,6 +309,7 @@ public void secondConnMustTimeout() throws SQLException {
308309
ResultSet rs = stat1.executeQuery("select * from t;");
309310
assertTrue(rs.next());
310311

312+
((SQLiteConnection) conn2).setBusyTimeout(10);
311313
stat2.executeUpdate("insert into t values (3);"); // can't be done
312314
}
313315

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.