1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.simple;
18
19 import org.apache.commons.rng.UniformRandomProvider;
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.BeforeAll;
22 import org.junit.jupiter.api.Test;
23
24 import java.util.EnumSet;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.TimeUnit;
30 import java.util.concurrent.TimeoutException;
31
32
33
34
35 class ThreadLocalRandomSourceTest {
36
37
38
39
40 private static EnumSet<RandomSource> toIgnore;
41
42 @BeforeAll
43 public static void createToIgnoreSet() {
44 toIgnore = EnumSet.of(RandomSource.TWO_CMRES_SELECT);
45 }
46
47 @Test
48 void testCurrentThrowsForNullRandomSource() {
49 Assertions.assertThrows(IllegalArgumentException.class, () -> ThreadLocalRandomSource.current(null));
50 }
51
52 @Test
53 void testCurrentThrowsForRandomSourceWithDataArguments() {
54 Assertions.assertThrows(IllegalArgumentException.class,
55 () -> ThreadLocalRandomSource.current(RandomSource.TWO_CMRES_SELECT));
56 }
57
58 @Test
59 void testCurrentForAllRandomSources()
60 throws InterruptedException, ExecutionException, TimeoutException {
61 final RandomSource[] sources = RandomSource.values();
62 final UniformRandomProvider[] rngs = new UniformRandomProvider[sources.length];
63
64 for (int i = 0; i < sources.length; i++) {
65 final RandomSource source = sources[i];
66 if (toIgnore.contains(source)) {
67 continue;
68 }
69 final UniformRandomProvider rng = getCurrent(source);
70 Assertions.assertNotNull(rng, () -> "Failed to create source: " + source);
71 rngs[i] = rng;
72 }
73 for (int i = 0; i < sources.length; i++) {
74 final RandomSource source = sources[i];
75 if (toIgnore.contains(source)) {
76 continue;
77 }
78 final UniformRandomProvider rng = getCurrent(source);
79 Assertions.assertSame(rngs[i], rng, () -> "Failed to return same source: " + source);
80 }
81
82
83 final UniformRandomProvider[] rngs2 = new UniformRandomProvider[rngs.length];
84 final ExecutorService executor = Executors.newFixedThreadPool(1);
85 final Future<?> future = executor.submit(
86 new Runnable() {
87 @Override
88 public void run() {
89 for (int i = 0; i < sources.length; i++) {
90 if (toIgnore.contains(sources[i])) {
91 continue;
92 }
93 rngs2[i] = getCurrent(sources[i]);
94 }
95 }
96 });
97
98
99 executor.shutdown();
100 future.get(30, TimeUnit.SECONDS);
101
102
103 for (int i = 0; i < sources.length; i++) {
104 final RandomSource source = sources[i];
105 if (toIgnore.contains(source)) {
106 continue;
107 }
108 Assertions.assertNotSame(rngs[i], rngs2[i], () -> "Failed to return different source: " + source);
109 }
110 }
111
112 private static UniformRandomProvider getCurrent(RandomSource source) {
113 try {
114 return ThreadLocalRandomSource.current(source);
115 } catch (final RuntimeException ex) {
116 throw new RuntimeException("Failed to get current: " + source, ex);
117 }
118 }
119 }