1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.integration.gauss;
23
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.concurrent.ArrayBlockingQueue;
27 import java.util.concurrent.Callable;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.ThreadPoolExecutor;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.atomic.AtomicInteger;
33
34 import org.hipparchus.util.Binary64;
35 import org.hipparchus.util.Binary64Field;
36 import org.hipparchus.util.Pair;
37 import org.junit.Assert;
38 import org.junit.Test;
39
40
41
42
43
44 public class FieldRuleFactoryTest {
45
46
47
48
49 @Test
50 public void testConcurrentCreation() throws InterruptedException,
51 ExecutionException {
52
53 final int numTasks = 20;
54
55 final ThreadPoolExecutor exec
56 = new ThreadPoolExecutor(3, numTasks, 1, TimeUnit.SECONDS,
57 new ArrayBlockingQueue<Runnable>(2));
58
59 final List<Future<Pair<Binary64[], Binary64[]>>> results
60 = new ArrayList<Future<Pair<Binary64[], Binary64[]>>>();
61 for (int i = 0; i < numTasks; i++) {
62 results.add(exec.submit(new RuleBuilder()));
63 }
64
65
66 for (Future<Pair<Binary64[], Binary64[]>> f : results) {
67 f.get();
68 }
69
70
71 final int n = RuleBuilder.getNumberOfCalls();
72 Assert.assertEquals("Rule computation was called " + n + " times", 1, n);
73 }
74
75 private static class RuleBuilder implements Callable<Pair<Binary64[], Binary64[]>> {
76 private static final DummyRuleFactory factory = new DummyRuleFactory();
77
78 public Pair<Binary64[], Binary64[]> call() {
79 final int dummy = 2;
80 return factory.getRule(dummy);
81 }
82
83 public static int getNumberOfCalls() {
84 return factory.getNumberOfCalls();
85 }
86 }
87
88 private static class DummyRuleFactory extends FieldAbstractRuleFactory<Binary64> {
89
90 private static AtomicInteger nCalls = new AtomicInteger();
91
92
93 DummyRuleFactory() {
94 super(Binary64Field.getInstance());
95 }
96
97 @Override
98 protected Pair<Binary64[], Binary64[]> computeRule(int order) {
99
100 nCalls.getAndIncrement();
101
102 try {
103
104 Thread.sleep(20);
105 } catch (InterruptedException e) {
106 Assert.fail("Unexpected interruption");
107 }
108
109
110 final Binary64[] p = new Binary64[order];
111 final Binary64[] w = new Binary64[order];
112 for (int i = 0; i < order; i++) {
113 p[i] = new Binary64(i);
114 w[i] = new Binary64(i);
115 }
116 return new Pair<>(p, w);
117 }
118
119 public int getNumberOfCalls() {
120 return nCalls.get();
121 }
122
123 }
124
125 }