Skip to content

Commit 6dfff94

Browse files
committed
Changed test to use ExecutorService instead.
1 parent f92f8fe commit 6dfff94

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

google-auth-library-java/cab-token-generator/javatests/com/google/auth/credentialaccessboundary/ClientSideCredentialAccessBoundaryFactoryTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,30 +1011,30 @@ void generateToken_freshInstance_concurrent_noNpe() throws Exception {
10111011
.build();
10121012

10131013
int numThreads = 5;
1014-
Thread[] threads = new Thread[numThreads];
10151014
CountDownLatch latch = new CountDownLatch(numThreads);
10161015
java.util.concurrent.atomic.AtomicInteger npeCount =
10171016
new java.util.concurrent.atomic.AtomicInteger();
1017+
java.util.concurrent.ExecutorService executor =
1018+
java.util.concurrent.Executors.newFixedThreadPool(numThreads);
10181019

1019-
for (int i = 0; i < numThreads; i++) {
1020-
threads[i] =
1021-
new Thread(
1022-
() -> {
1023-
try {
1024-
latch.countDown();
1025-
latch.await();
1026-
factory.generateToken(accessBoundary);
1027-
} catch (NullPointerException e) {
1028-
npeCount.incrementAndGet();
1029-
} catch (Exception e) {
1030-
// Ignore other exceptions for the sake of the race reproduction
1031-
}
1032-
});
1033-
threads[i].start();
1034-
}
1035-
1036-
for (Thread thread : threads) {
1037-
thread.join();
1020+
try {
1021+
for (int i = 0; i < numThreads; i++) {
1022+
executor.submit(
1023+
() -> {
1024+
try {
1025+
latch.countDown();
1026+
latch.await();
1027+
factory.generateToken(accessBoundary);
1028+
} catch (NullPointerException e) {
1029+
npeCount.incrementAndGet();
1030+
} catch (Exception e) {
1031+
// Ignore other exceptions for the sake of the race reproduction
1032+
}
1033+
});
1034+
}
1035+
} finally {
1036+
executor.shutdown();
1037+
executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS);
10381038
}
10391039

10401040
org.junit.jupiter.api.Assertions.assertEquals(

0 commit comments

Comments
 (0)