Skip to content

Commit d78769b

Browse files
committed
#381 Better handling of threads in RaceConditionTest
1 parent 33b3c9a commit d78769b

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

activejdbc/src/test/java/org/javalite/activejdbc/RaceConditionTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,41 @@
44
import org.javalite.activejdbc.test_models.Person;
55
import org.junit.Test;
66

7-
import java.util.concurrent.ConcurrentLinkedQueue;
7+
import java.util.Collections;
8+
import java.util.concurrent.Callable;
9+
import java.util.concurrent.ExecutionException;
810
import java.util.concurrent.ExecutorService;
911
import java.util.concurrent.Executors;
10-
import java.util.concurrent.TimeUnit;
12+
import java.util.concurrent.Future;
1113

1214
import static org.javalite.activejdbc.test.JdbcProperties.*;
1315

1416
/**
1517
* @author Igor Polevoy: 4/4/12 2:40 PM
18+
* @author Eric Nielsen
1619
*/
1720
public class RaceConditionTest extends ActiveJDBCTest{
1821

1922
@Test
2023
//TODO: what is test testing?
21-
public void shouldNotGetRaceCondition() throws InterruptedException {
22-
23-
final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>();
24-
25-
Runnable r = new Runnable() {
26-
@Override public void run() {
24+
public void shouldNotGetRaceCondition() throws ExecutionException, InterruptedException {
25+
Callable<Person> task = new Callable<Person>() {
26+
@Override
27+
public Person call() throws Exception {
2728
Base.open(driver(), url(), user(), password());
2829
Person p = new Person();
2930
p.set("name", "Igor");
3031
Base.close();
31-
queue.add(1);
32+
return p;
3233
}
3334
};
3435
ExecutorService executor = Executors.newFixedThreadPool(10);
35-
for (int i = 0; i < 10; i++) {
36-
executor.execute(r);
36+
try {
37+
for (Future<Person> future : executor.invokeAll(Collections.nCopies(10, task))) {
38+
future.get();
39+
}
40+
} finally {
41+
executor.shutdownNow();
3742
}
38-
executor.shutdown();
39-
executor.awaitTermination(1, TimeUnit.MINUTES);
40-
the(queue.size()).shouldEqual(10);
4143
}
4244
}

0 commit comments

Comments
 (0)