|
4 | 4 | import org.javalite.activejdbc.test_models.Person;
|
5 | 5 | import org.junit.Test;
|
6 | 6 |
|
7 |
| -import java.util.concurrent.ConcurrentLinkedQueue; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.concurrent.Callable; |
| 9 | +import java.util.concurrent.ExecutionException; |
8 | 10 | import java.util.concurrent.ExecutorService;
|
9 | 11 | import java.util.concurrent.Executors;
|
10 |
| -import java.util.concurrent.TimeUnit; |
| 12 | +import java.util.concurrent.Future; |
11 | 13 |
|
12 | 14 | import static org.javalite.activejdbc.test.JdbcProperties.*;
|
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * @author Igor Polevoy: 4/4/12 2:40 PM
|
| 18 | + * @author Eric Nielsen |
16 | 19 | */
|
17 | 20 | public class RaceConditionTest extends ActiveJDBCTest{
|
18 | 21 |
|
19 | 22 | @Test
|
20 | 23 | //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 { |
27 | 28 | Base.open(driver(), url(), user(), password());
|
28 | 29 | Person p = new Person();
|
29 | 30 | p.set("name", "Igor");
|
30 | 31 | Base.close();
|
31 |
| - queue.add(1); |
| 32 | + return p; |
32 | 33 | }
|
33 | 34 | };
|
34 | 35 | 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(); |
37 | 42 | }
|
38 |
| - executor.shutdown(); |
39 |
| - executor.awaitTermination(1, TimeUnit.MINUTES); |
40 |
| - the(queue.size()).shouldEqual(10); |
41 | 43 | }
|
42 | 44 | }
|
0 commit comments