Skip to content

Commit

Permalink
Merge pull request #65 from Cililing/pmaterna/concurrentFix/1
Browse files Browse the repository at this point in the history
Use concurrent hash map for cache in enhanced random
  • Loading branch information
mjureczko authored May 24, 2024
2 parents 48f6166 + 88a6c96 commit e0031ac
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 e0031ac

Please sign in to comment.