Skip to content

Commit d6d514d

Browse files
committed
Try to satisfy pmd executor service closing.
1 parent 7652b93 commit d6d514d

2 files changed

Lines changed: 26 additions & 29 deletions

File tree

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/KeyBufferTest.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818

1919
import java.io.ByteArrayOutputStream;
20+
import java.io.Closeable;
2021
import java.io.IOException;
2122
import java.io.PrintStream;
2223
import java.nio.channels.FileChannel;
@@ -228,12 +229,12 @@ void shouldNotLogWarningWhenNoOverflow() {
228229
}
229230

230231
@Test
231-
void shouldBeThreadSafeForDifferentKeys() throws InterruptedException {
232+
void shouldBeThreadSafeForDifferentKeys() throws InterruptedException, IOException {
232233
int threadCount = 10;
233234
int eventsPerThread = 100;
234235
KeyBuffer<String, String> largeBuffer = new KeyBuffer<>(10000, String::length);
235236
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
236-
try {
237+
try (Closeable close = executor::shutdown) {
237238
CountDownLatch latch = new CountDownLatch(threadCount);
238239

239240
// Each thread works with different key
@@ -260,18 +261,16 @@ void shouldBeThreadSafeForDifferentKeys() throws InterruptedException {
260261
.isNotNull()
261262
.hasSize(eventsPerThread);
262263
}
263-
} finally {
264-
executor.shutdown();
265264
}
266265
}
267266

268267
@Test
269-
void shouldBeThreadSafeForSameKey() throws InterruptedException {
268+
void shouldBeThreadSafeForSameKey() throws InterruptedException, IOException {
270269
int threadCount = 5;
271270
int eventsPerThread = 20;
272271
KeyBuffer<String, String> largeBuffer = new KeyBuffer<>(10000, String::length);
273272
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
274-
try {
273+
try (Closeable close = executor::shutdown) {
275274
CountDownLatch latch = new CountDownLatch(threadCount);
276275

277276
// All threads work with same key
@@ -294,8 +293,6 @@ void shouldBeThreadSafeForSameKey() throws InterruptedException {
294293
assertThat(events)
295294
.isNotNull()
296295
.hasSize(threadCount * eventsPerThread);
297-
} finally {
298-
executor.shutdown();
299296
}
300297
}
301298

powertools-logging/src/test/java/software/amazon/lambda/powertools/logging/internal/LoggingManagerRegistryTest.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import static org.assertj.core.api.Assertions.assertThat;
1818

1919
import java.io.ByteArrayOutputStream;
20+
import java.io.Closeable;
21+
import java.io.IOException;
2022
import java.io.PrintStream;
2123
import java.io.UnsupportedEncodingException;
2224
import java.util.ArrayList;
@@ -110,32 +112,30 @@ void testGetLoggingManager_shouldReturnSameInstance() {
110112
}
111113

112114
@Test
113-
void testGetLoggingManager_shouldBeThreadSafe() throws InterruptedException {
115+
void testGetLoggingManager_shouldBeThreadSafe() throws InterruptedException, IOException {
114116
// GIVEN
115117
int threadCount = 10;
116118
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
117-
CountDownLatch latch = new CountDownLatch(threadCount);
118-
AtomicReference<LoggingManager> sharedInstance = new AtomicReference<>();
119-
120-
// WHEN
121-
for (int i = 0; i < threadCount; i++) {
122-
executor.submit(() -> {
123-
try {
124-
LoggingManager instance = LoggingManagerRegistry.getLoggingManager();
125-
sharedInstance.compareAndSet(null, instance);
126-
assertThat(instance).isSameAs(sharedInstance.get());
127-
} finally {
128-
latch.countDown();
129-
}
130-
});
131-
}
132-
133-
// THEN
134-
try {
119+
try (Closeable close = executor::shutdown) {
120+
CountDownLatch latch = new CountDownLatch(threadCount);
121+
AtomicReference<LoggingManager> sharedInstance = new AtomicReference<>();
122+
123+
// WHEN
124+
for (int i = 0; i < threadCount; i++) {
125+
executor.submit(() -> {
126+
try {
127+
LoggingManager instance = LoggingManagerRegistry.getLoggingManager();
128+
sharedInstance.compareAndSet(null, instance);
129+
assertThat(instance).isSameAs(sharedInstance.get());
130+
} finally {
131+
latch.countDown();
132+
}
133+
});
134+
}
135+
136+
// THEN
135137
latch.await(5, TimeUnit.SECONDS);
136138
assertThat(sharedInstance.get()).isNotNull();
137-
} finally {
138-
executor.shutdown();
139139
}
140140
}
141141
}

0 commit comments

Comments
 (0)