1
1
/*
2
- * Copyright IBM Corp. 2023
2
+ * Copyright IBM Corp. 2023, 2024
3
3
*
4
4
* Licensed under the Apache License 2.0 (the "License"). You may not use
5
5
* this file except in compliance with the License. You can obtain a copy
11
11
import java .util .ArrayList ;
12
12
import java .util .Collections ;
13
13
import java .util .List ;
14
+ import java .util .concurrent .Callable ;
14
15
import java .util .concurrent .CountDownLatch ;
15
16
import java .util .concurrent .ExecutorService ;
16
17
import java .util .concurrent .Executors ;
17
18
import java .util .concurrent .TimeUnit ;
19
+
18
20
import org .junit .runner .JUnitCore ;
19
21
import org .junit .runner .Request ;
22
+ import org .junit .runner .Result ;
23
+ import org .junit .runner .notification .Failure ;
24
+
20
25
import junit .framework .Test ;
21
26
import junit .framework .TestCase ;
22
27
import junit .framework .TestSuite ;
@@ -78,9 +83,11 @@ public class TestMultithread extends TestCase {
78
83
79
84
public TestMultithread () {}
80
85
81
- private void assertConcurrent (final String message , final Runnable runnable ,
86
+ private boolean assertConcurrent (final String message , final Callable < List < Failure >> callable ,
82
87
final int maxTimeoutSeconds ) throws InterruptedException {
88
+ boolean failed = false ;
83
89
final List <Throwable > exceptions = Collections .synchronizedList (new ArrayList <Throwable >());
90
+ final List <Failure > failures = Collections .synchronizedList (new ArrayList <Failure >());
84
91
final ExecutorService threadPool = Executors .newFixedThreadPool (numThreads );
85
92
try {
86
93
final CountDownLatch allExecutorThreadsReady = new CountDownLatch (numThreads );
@@ -92,7 +99,7 @@ public void run() {
92
99
allExecutorThreadsReady .countDown ();
93
100
try {
94
101
afterInitBlocker .await ();
95
- runnable . run ( );
102
+ failures . addAll ( callable . call () );
96
103
} catch (final Throwable e ) {
97
104
exceptions .add (e );
98
105
} finally {
@@ -112,10 +119,22 @@ public void run() {
112
119
} finally {
113
120
threadPool .shutdownNow ();
114
121
}
115
- assertTrue (message + "failed with exception(s)" + exceptions , exceptions .isEmpty ());
122
+ if (!exceptions .isEmpty ()) {
123
+ for (Throwable t : exceptions ) {
124
+ t .printStackTrace ();
125
+ }
126
+ }
127
+ failed = !exceptions .isEmpty ();
128
+
129
+ for (Failure failure : failures ) {
130
+ failure .getException ().printStackTrace ();
131
+ }
132
+ //failed = !failures.isEmpty();
133
+
134
+ return failed ;
116
135
}
117
136
118
- private Runnable testToRunnable (String classAndMethod ) {
137
+ private Callable < List < Failure >> testToCallable (String classAndMethod ) {
119
138
String [] classAndMethodList = classAndMethod .split ("#" );
120
139
try {
121
140
Request request = null ;
@@ -126,10 +145,10 @@ private Runnable testToRunnable(String classAndMethod) {
126
145
request = Request .aClass (Class .forName (classAndMethodList [0 ]));
127
146
}
128
147
final Request myrequest = request ;
129
- return new Runnable () {
130
- public void run () {
131
- new JUnitCore ().run (myrequest );
132
- //assertTrue( result.getFailureCount()== 0 );
148
+ return new Callable < List < Failure >> () {
149
+ public List < Failure > call () {
150
+ Result result = new JUnitCore ().run (myrequest );
151
+ return result .getFailures ( );
133
152
}
134
153
};
135
154
} catch (ClassNotFoundException ex ) {
@@ -141,16 +160,25 @@ public void run() {
141
160
public void testMultithread () {
142
161
System .out .println ("#threads=" + numThreads + " timeout=" + timeoutSec );
143
162
163
+ List <String > failedTests = new ArrayList <>();
164
+
144
165
for (String test : testList ) {
145
166
try {
146
167
System .out .println ("Test calling: " + test );
147
168
148
- assertConcurrent ("Test failed: " + test , testToRunnable (test ), timeoutSec );
169
+ boolean failed = assertConcurrent ("Test failed: " + test , testToCallable (test ), timeoutSec );
170
+ if (failed ) {
171
+ failedTests .add (test );
172
+ }
149
173
150
174
} catch (InterruptedException e ) {
151
175
//System.out.println("Test interrupted: " + e);
152
176
}
153
177
System .out .println ("Test finished: " + test );
178
+ if (!failedTests .isEmpty ()) {
179
+ String allFailedTests = String .join ("\n \t " , failedTests );
180
+ fail ("Failed tests:\n \t " + allFailedTests );
181
+ }
154
182
}
155
183
}
156
184
0 commit comments