Skip to content

Commit

Permalink
use concurrent hash map for cache in enhanced random
Browse files Browse the repository at this point in the history
(cherry picked from commit 88a6c96)
  • Loading branch information
Cililing authored and mjureczko committed May 24, 2024
1 parent 131ed93 commit d85bd05
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jeasy.random.api.Randomizer;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand All @@ -30,7 +31,7 @@ public class EnhancedRandom extends Random {

final EasyRandom easyRandom;
private final Map<Class<?>, CustomArranger<?>> arrangers;
private final HashMap<Set<String>, EasyRandom> cache = new HashMap<>();
private final Map<Set<String>, EasyRandom> cache = new ConcurrentHashMap<>();
private final Supplier<EasyRandomParameters> parametersSupplier;


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ocadotechnology.gembus.bugfix.concurrency;

import com.ocadotechnology.gembus.test.Arranger;
import org.junit.jupiter.api.Test;

import java.util.Map;
import java.util.UUID;
import java.util.stream.IntStream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

public class ConcurrencyTest {

@Test
void concurrentSomeWithOverridesDoesNotThrow() {
assertDoesNotThrow(() -> {
IntStream.range(0, 10).parallel().mapToObj(i -> createCustomStruct()).toList();
});
}

private CustomStruct createCustomStruct() {
return Arranger.some(CustomStruct.class, Map.of("id", () -> "id", "uuid", UUID::randomUUID));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.ocadotechnology.gembus.bugfix.concurrency;

import java.util.UUID;

public record CustomStruct(String id, UUID uuid) {}

0 comments on commit d85bd05

Please sign in to comment.