|
5 | 5 |
|
6 | 6 | import static org.junit.Assume.assumeTrue; |
7 | 7 |
|
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | +import java.util.concurrent.atomic.AtomicReference; |
| 11 | + |
8 | 12 | public class TestThreads { |
9 | 13 |
|
10 | | - public static final int THREAD_COUNT = 200; |
| 14 | + public static final int THREAD_COUNT = Integer.getInteger("com.sri.yices.testThreads.count", 32); |
| 15 | + public static final long THREAD_TIMEOUT_SECONDS = Long.getLong("com.sri.yices.testThreads.timeoutSeconds", 60L); |
11 | 16 |
|
12 | 17 | public static final String COUNTER_PREFIX = "c@"; |
13 | 18 | public static final String CHOICE_PREFIX = "i@"; |
14 | 19 |
|
15 | | - private static final Object lock = new Object(); |
16 | | - private static int waiting = 0; |
17 | | - |
18 | 20 | @Test |
19 | 21 | public void testVersion() { |
20 | 22 | assumeTrue(TestAssumptions.IS_YICES_INSTALLED); |
@@ -43,16 +45,9 @@ private int makeConstraint(int index, int increment, int selector){ |
43 | 45 | return Terms.and(selector, Terms.eq(t1, Terms.add(t2, Terms.intConst(increment)))); |
44 | 46 | } |
45 | 47 |
|
46 | | - private void threadMain(int index, Status[] answers){ |
47 | | - |
48 | | - synchronized(lock){ |
49 | | - waiting++; |
50 | | - try { |
51 | | - lock.wait(); |
52 | | - } catch(InterruptedException e){ |
53 | | - System.err.println(e.getMessage()); |
54 | | - } |
55 | | - } |
| 48 | + private void threadMain(int index, Status[] answers, CountDownLatch readyGate, CountDownLatch startGate) throws InterruptedException { |
| 49 | + readyGate.countDown(); |
| 50 | + Assert.assertTrue("timed out waiting to start worker threads", startGate.await(THREAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)); |
56 | 51 |
|
57 | 52 | try (Config cfg = new Config()) { |
58 | 53 | cfg.set("solver-type", "dpllt"); |
@@ -82,54 +77,63 @@ private void threadMain(int index, Status[] answers){ |
82 | 77 | } |
83 | 78 | } |
84 | 79 |
|
85 | | - private Thread makeThread(final int index, final Status[] answers){ |
| 80 | + private Thread makeThread(final int index, final Status[] answers, final CountDownLatch readyGate, |
| 81 | + final CountDownLatch startGate, final AtomicReference<Throwable> failure){ |
86 | 82 | Runnable runnable = new Runnable(){ |
87 | 83 | public void run(){ |
88 | | - threadMain(index, answers); |
| 84 | + try { |
| 85 | + threadMain(index, answers, readyGate, startGate); |
| 86 | + } catch (Throwable error) { |
| 87 | + failure.compareAndSet(null, error); |
| 88 | + } |
89 | 89 | } |
90 | 90 | }; |
91 | | - return new Thread(runnable); |
| 91 | + Thread thread = new Thread(runnable, "yices-test-thread-" + index); |
| 92 | + thread.setDaemon(true); |
| 93 | + return thread; |
92 | 94 | } |
93 | 95 |
|
94 | 96 |
|
95 | | - @Test |
| 97 | + @Test(timeout = 120000) |
96 | 98 | public void testThreads() { |
97 | 99 | assumeTrue(TestAssumptions.IS_YICES_INSTALLED); |
98 | 100 | assumeTrue(Yices.isThreadSafe()); |
99 | 101 |
|
100 | 102 | Thread[] threads = new Thread[THREAD_COUNT]; |
101 | 103 | Status[] answers = new Status[THREAD_COUNT]; |
| 104 | + CountDownLatch readyGate = new CountDownLatch(THREAD_COUNT); |
| 105 | + CountDownLatch startGate = new CountDownLatch(1); |
| 106 | + AtomicReference<Throwable> failure = new AtomicReference<Throwable>(); |
102 | 107 |
|
103 | 108 | for(int i = 0; i < THREAD_COUNT; i++){ |
104 | | - threads[i] = makeThread(i, answers); |
| 109 | + threads[i] = makeThread(i, answers, readyGate, startGate, failure); |
105 | 110 | } |
106 | 111 |
|
107 | 112 | for(int i = 0; i < THREAD_COUNT; i++){ |
108 | 113 | threads[i].start(); |
109 | 114 | } |
110 | 115 |
|
111 | | - Thread self = Thread.currentThread(); |
112 | | - |
113 | | - while(true){ |
114 | | - synchronized(lock){ |
115 | | - if (waiting == THREAD_COUNT){ |
116 | | - lock.notifyAll(); |
117 | | - break; |
118 | | - } |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - |
123 | 116 | try { |
| 117 | + Assert.assertTrue("timed out waiting for worker threads to become ready", |
| 118 | + readyGate.await(THREAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)); |
| 119 | + startGate.countDown(); |
124 | 120 | for(int i = 0; i < THREAD_COUNT; i++){ |
125 | | - threads[i].join(); |
| 121 | + threads[i].join(TimeUnit.SECONDS.toMillis(THREAD_TIMEOUT_SECONDS)); |
| 122 | + Assert.assertFalse("worker thread did not finish: " + threads[i].getName(), threads[i].isAlive()); |
126 | 123 | } |
127 | 124 | } catch (InterruptedException error){ |
128 | | - System.out.println(error.getMessage()); |
| 125 | + Thread.currentThread().interrupt(); |
| 126 | + Assert.fail(error.getMessage()); |
| 127 | + } |
| 128 | + |
| 129 | + if (failure.get() != null) { |
| 130 | + AssertionError error = new AssertionError("worker thread failed"); |
| 131 | + error.initCause(failure.get()); |
| 132 | + throw error; |
129 | 133 | } |
130 | 134 |
|
131 | 135 | for (int i = 0; i < THREAD_COUNT; i++){ |
132 | | - Assert.assertEquals(answers[i], Status.SAT); |
| 136 | + Assert.assertEquals(Status.SAT, answers[i]); |
133 | 137 | } |
134 | 138 |
|
135 | 139 |
|
|
0 commit comments