Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
lizk886 committed Aug 1, 2024
1 parent c999449 commit cac301e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public int getTotalSites() {
}

public List<S3Key> getKeys(int siteId) {
//for s3 encryption keys retrieval
return siteToKeysMap.getOrDefault(siteId, new ArrayList<>());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
import org.mockito.MockitoAnnotations;

import java.time.Instant;
import java.util.*;
import java.util.concurrent.*;
import java.util.Set;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -467,44 +472,4 @@ void testGetEncryptionKeyForSiteWithNoActiveKeys() {

assertThrows(IllegalStateException.class, () -> rotatingS3KeyProvider.getEncryptionKeyForSite(123));
}

@Test
public void testRaceCondition() throws InterruptedException, ExecutionException {
int threadCount = 100;
ExecutorService executorService = Executors.newFixedThreadPool(threadCount);
CountDownLatch latch = new CountDownLatch(1);
Map<Integer, S3Key> keysMap = new HashMap<>();
keysMap.put(1, new S3Key(1, 88, 1687635529, 1687808329, "secret1"));
keysMap.put(2, new S3Key(2, 88, 1687635530, 1687808330, "secret2"));
keysMap.put(3, new S3Key(3, 88, 1687635531, 1687808331, "secret3"));
keysMap.put(4, new S3Key(4, 89, 1687635532, 1687808332, "secret4"));

when(reader.getSnapshot()).thenReturn(keysMap);
List<Callable<Void>> tasks = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
tasks.add(() -> {
latch.await();
if (ThreadLocalRandom.current().nextBoolean()) {
rotatingS3KeyProvider.updateSiteToKeysMapping();
} else {
rotatingS3KeyProvider.getKeysForSite(88);
}
return null;
});
}

// Start all tasks simultaneously
for (Callable<Void> task : tasks) {
executorService.submit(task);
}
latch.countDown(); // Allow all threads to proceed

executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);

List<S3Key> keysForSite88 = rotatingS3KeyProvider.getKeysForSite(88).stream()
.filter(key -> key.getSiteId() == 88)
.collect(Collectors.toList());
assertEquals(3, keysForSite88.size());
}
}

0 comments on commit cac301e

Please sign in to comment.