3737import java .util .Collections ;
3838import java .util .Random ;
3939import java .util .concurrent .Callable ;
40+ import java .util .concurrent .CountDownLatch ;
4041import java .util .concurrent .ExecutorService ;
4142import java .util .concurrent .Executors ;
4243import java .util .concurrent .TimeUnit ;
@@ -125,9 +126,6 @@ void rejectsInvalidXmlUsingXsiSchemaLocation() {
125126
126127 @ Test
127128 void validatesMultipleXmlsInThreads () throws Exception {
128- final int timeout = 10 ;
129- final int numrun = 100 ;
130- final int loop = 50 ;
131129 final XSD xsd = new XSDDocument (
132130 StringUtils .join (
133131 "<xs:schema xmlns:xs ='http://www.w3.org/2001/XMLSchema' >" ,
@@ -145,9 +143,9 @@ void validatesMultipleXmlsInThreads() throws Exception {
145143 Iterables .concat (
146144 Collections .singleton ("<r>" ),
147145 Iterables .transform (
148- Collections .nCopies (timeout , 0 ),
146+ Collections .nCopies (10 , 0 ),
149147 pos -> String .format (
150- "<x>%d</x>" , rnd .nextInt (numrun )
148+ "<x>%d</x>" , rnd .nextInt (100 )
151149 )
152150 ),
153151 Collections .singleton ("<x>101</x></r>" )
@@ -156,25 +154,33 @@ void validatesMultipleXmlsInThreads() throws Exception {
156154 )
157155 );
158156 final AtomicInteger done = new AtomicInteger ();
157+ final int threads = 50 ;
158+ final CountDownLatch latch = new CountDownLatch (threads );
159159 final Callable <Void > callable = () -> {
160160 try {
161161 new StrictXML (xml , xsd );
162162 } catch (final IllegalArgumentException ex ) {
163163 done .incrementAndGet ();
164+ } finally {
165+ latch .countDown ();
164166 }
165167 return null ;
166168 };
167169 final ExecutorService service = Executors .newFixedThreadPool (5 );
168- for (int count = 0 ; count < loop ; count += 1 ) {
169- service .submit (callable );
170+ try {
171+ for (int count = 0 ; count < threads ; count += 1 ) {
172+ service .submit (callable );
173+ }
174+ latch .await (1L , TimeUnit .SECONDS );
175+ MatcherAssert .assertThat (done .get (), Matchers .equalTo (threads ));
176+ } finally {
177+ service .shutdown ();
178+ MatcherAssert .assertThat (
179+ service .awaitTermination (10L , TimeUnit .SECONDS ),
180+ Matchers .is (true )
181+ );
182+ service .shutdownNow ();
170183 }
171- service .shutdown ();
172- MatcherAssert .assertThat (
173- service .awaitTermination (timeout , TimeUnit .SECONDS ),
174- Matchers .is (true )
175- );
176- service .shutdownNow ();
177- MatcherAssert .assertThat (done .get (), Matchers .equalTo (loop ));
178184 }
179185
180186 @ Test
0 commit comments