Skip to content

Commit ab577aa

Browse files
authored
remove old commented out test in FakeValuesTest (#1570)
The loading synchronization was later implemented in `LazyEvaluated`, and this commit adds a similar test `LazyEvaluatedTest`.
1 parent 73beb31 commit ab577aa

File tree

2 files changed

+57
-37
lines changed

2 files changed

+57
-37
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package net.datafaker.internal.helper;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.concurrent.ExecutorService;
6+
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.function.Supplier;
8+
9+
import static java.util.concurrent.Executors.newFixedThreadPool;
10+
import static java.util.concurrent.TimeUnit.SECONDS;
11+
import static org.assertj.core.api.Assertions.assertThat;
12+
13+
class LazyEvaluatedTest {
14+
private final AtomicInteger executions = new AtomicInteger(0);
15+
private final Supplier<Integer> slowLoader = () -> {
16+
sleep();
17+
return executions.incrementAndGet();
18+
};
19+
20+
private final LazyEvaluated<Integer> lazyEvaluated = new LazyEvaluated<>(slowLoader);
21+
22+
@Test
23+
void notInitializedUntilCalled() {
24+
assertThat(executions.get()).isEqualTo(0);
25+
}
26+
27+
@Test
28+
void initializedOnlyOnce() throws InterruptedException {
29+
int threadsCount = 5;
30+
ExecutorService threadPool = newFixedThreadPool(threadsCount);
31+
32+
for (int i = 0; i < threadsCount; i++) {
33+
threadPool.submit(() -> {
34+
try {
35+
assertThat(lazyEvaluated.get()).isEqualTo(1);
36+
} catch (Throwable e) {
37+
System.out.printf("Loading failed...%s%n", e);
38+
}
39+
});
40+
}
41+
42+
threadPool.shutdown();
43+
assertThat(threadPool.awaitTermination(10, SECONDS)).isTrue();
44+
45+
assertThat(executions.get())
46+
.as("The slowLoader should be executed only once, even if the value was asked in multiple parallel threads.")
47+
.isEqualTo(1);
48+
}
49+
50+
private static void sleep() {
51+
try {
52+
Thread.sleep(10);
53+
} catch (InterruptedException e) {
54+
throw new RuntimeException(e);
55+
}
56+
}
57+
}

src/test/java/net/datafaker/service/FakeValuesTest.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,6 @@ class FakeValuesTest {
1919
private static final String PATH = "address";
2020
private final FakeValues fakeValues = FakeValues.of(FakeValuesContext.of(Locale.ENGLISH, "address.yml", PATH));
2121

22-
/*
23-
Test case for for https://github.com/datafaker-net/datafaker/issues/574
24-
To test it need to change net.datafaker.service.FakeValues.loadValues to something from private
25-
Powermock can not test it because it requires JUnit4
26-
@Test
27-
void testLoadValues() {
28-
FakeValues fv = Mockito.spy(new FakeValues(Locale.ENGLISH));
29-
ExecutorService service = new ForkJoinPool(2);
30-
CountDownLatch latch = new CountDownLatch(2);
31-
service.submit(() -> {
32-
latch.countDown();
33-
try {
34-
latch.await(10, TimeUnit.SECONDS);
35-
} catch (InterruptedException e) {
36-
throw new RuntimeException(e);
37-
}
38-
fv.get("key");
39-
});
40-
service.submit(() -> {
41-
latch.countDown();
42-
try {
43-
latch.await(10, TimeUnit.SECONDS);
44-
} catch (InterruptedException e) {
45-
throw new RuntimeException(e);
46-
}
47-
fv.get("key");
48-
});
49-
service.shutdown();
50-
try {
51-
service.awaitTermination(10, TimeUnit.SECONDS);
52-
} catch (InterruptedException e) {
53-
throw new RuntimeException(e);
54-
}
55-
verify(fv, times(1)).loadValues();
56-
}
57-
*/
58-
5922
@Test
6023
void getAValueReturnsAValue() {
6124
assertThat(fakeValues.get(PATH)).isNotNull();

0 commit comments

Comments
 (0)