Skip to content

Commit 5fc86a3

Browse files
committed
Use countdown latch instead of Thread.sleep for transformation manager tests.
1 parent bf40ad6 commit 5fc86a3

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

powertools-parameters/powertools-parameters-tests/src/test/java/software/amazon/lambda/powertools/parameters/transform/TransformationManagerTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static software.amazon.lambda.powertools.parameters.transform.Transformer.json;
2222

2323
import java.util.Base64;
24+
import java.util.concurrent.CountDownLatch;
2425

2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
@@ -124,11 +125,13 @@ public void unsetTransformer_shouldCleanUpThreadLocal() {
124125
public void setTransformer_concurrentCalls_shouldBeThreadSafe() throws InterruptedException {
125126
// GIVEN
126127
boolean[] success = new boolean[2];
128+
CountDownLatch latch = new CountDownLatch(2);
127129

128130
Thread thread1 = new Thread(() -> {
129131
try {
132+
latch.countDown();
133+
latch.await();
130134
manager.setTransformer(json);
131-
Thread.sleep(10); // Small delay to increase chance of thread interleaving
132135
// Thread 1 expects json transformer
133136
String result = manager.performComplexTransformation(
134137
"{\"foo\":\"Foo\", \"bar\":42, \"baz\":123456789}",
@@ -142,7 +145,8 @@ public void setTransformer_concurrentCalls_shouldBeThreadSafe() throws Interrupt
142145

143146
Thread thread2 = new Thread(() -> {
144147
try {
145-
Thread.sleep(5); // Start slightly after thread1
148+
latch.countDown();
149+
latch.await();
146150
manager.setTransformer(base64);
147151
// Thread 2 expects base64 transformer
148152
String result = manager.performBasicTransformation(
@@ -170,11 +174,13 @@ public void setTransformer_concurrentCalls_shouldBeThreadSafe() throws Interrupt
170174
public void unsetTransformer_concurrentCalls_shouldNotAffectOtherThreads() throws InterruptedException {
171175
// GIVEN
172176
boolean[] success = new boolean[2];
177+
CountDownLatch latch = new CountDownLatch(2);
173178

174179
Thread thread1 = new Thread(() -> {
175180
try {
181+
latch.countDown();
182+
latch.await();
176183
manager.setTransformer(json);
177-
Thread.sleep(10);
178184
// Thread 1 should still have json transformer even if thread 2 unsets
179185
assertThat(manager.shouldTransform()).isTrue();
180186
success[0] = true;
@@ -186,8 +192,9 @@ public void unsetTransformer_concurrentCalls_shouldNotAffectOtherThreads() throw
186192

187193
Thread thread2 = new Thread(() -> {
188194
try {
195+
latch.countDown();
196+
latch.await();
189197
manager.setTransformer(base64);
190-
Thread.sleep(5);
191198
manager.unsetTransformer();
192199
// Thread 2 should have no transformer after unset
193200
assertThat(manager.shouldTransform()).isFalse();

0 commit comments

Comments
 (0)