1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.block_app.test_setup; |
19 | |
|
20 | |
import org.apache.giraph.block_app.framework.BlockUtils; |
21 | |
import org.apache.giraph.conf.BulkConfigurator; |
22 | |
import org.apache.giraph.block_app.framework.api.local.LocalBlockRunner; |
23 | |
import org.apache.giraph.conf.BooleanConfOption; |
24 | |
import org.apache.giraph.conf.GiraphConfiguration; |
25 | |
import org.apache.giraph.function.Supplier; |
26 | |
import org.apache.hadoop.io.DoubleWritable; |
27 | |
import org.apache.hadoop.io.NullWritable; |
28 | |
import org.apache.hadoop.io.Writable; |
29 | |
import org.apache.hadoop.io.WritableComparable; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class TestGraphUtils { |
35 | |
|
36 | |
public static final |
37 | 0 | BooleanConfOption USE_FULL_GIRAPH_ENV_IN_TESTS = new BooleanConfOption( |
38 | |
"giraph.blocks.test_setup.use_full_giraph_env_in_tests", false, |
39 | |
"Whether to use full giraph environemnt for tests, " + |
40 | |
"or only local implementation"); |
41 | |
|
42 | |
|
43 | |
|
44 | 0 | private static int TEST_REPEAT_TIMES = 1; |
45 | |
|
46 | 0 | private TestGraphUtils() { } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static |
55 | |
<I extends WritableComparable, V extends Writable, E extends Writable> |
56 | |
void runTest( |
57 | |
final TestGraphModifier<? super I, ? super V, ? super E> graphInitializer, |
58 | |
final TestGraphChecker<? super I, ? super V, ? super E> graphChecker, |
59 | |
final BulkConfigurator configurator) throws Exception { |
60 | 0 | repeat( |
61 | 0 | repeatTimes(), |
62 | 0 | new OneTest() { |
63 | |
@Override |
64 | |
public void test() throws Exception { |
65 | 0 | GiraphConfiguration conf = new GiraphConfiguration(); |
66 | 0 | configurator.configure(conf); |
67 | 0 | BlockUtils.initAndCheckConfig(conf); |
68 | 0 | runTest(graphInitializer, graphChecker, conf); |
69 | 0 | } |
70 | |
}); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public static |
78 | |
<I extends WritableComparable, E extends Writable, V extends Writable> |
79 | |
void runTest( |
80 | |
TestGraphModifier<? super I, ? super V, ? super E> graphInitializer, |
81 | |
TestGraphChecker<? super I, ? super V, ? super E> graphChecker, |
82 | |
GiraphConfiguration conf) throws Exception { |
83 | 0 | NumericTestGraph<I, V, E> graph = new NumericTestGraph<>(conf); |
84 | 0 | graphInitializer.modifyGraph((NumericTestGraph) graph); |
85 | 0 | runTest(graph, graphChecker); |
86 | 0 | } |
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public static |
93 | |
<I extends WritableComparable, E extends Writable, V extends Writable> |
94 | |
void runTest( |
95 | |
NumericTestGraph<I, V, E> graph, |
96 | |
TestGraphChecker<? super I, ? super V, ? super E> graphChecker |
97 | |
) throws Exception { |
98 | 0 | graph = new NumericTestGraph<I, V, E>( |
99 | 0 | LocalBlockRunner.runApp( |
100 | 0 | graph.getTestGraph(), useFullDigraphTests(graph.getConf()))); |
101 | 0 | if (graphChecker != null) { |
102 | 0 | graphChecker.checkOutput((NumericTestGraph) graph); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
@SafeVarargs |
110 | |
public static |
111 | |
<I extends WritableComparable, V extends Writable, E extends Writable> |
112 | |
TestGraphModifier<I, V, E> chainModifiers( |
113 | |
final TestGraphModifier<I, V, E>... graphModifiers) { |
114 | 0 | return new TestGraphModifier<I, V, E>() { |
115 | |
@Override |
116 | |
public void modifyGraph( |
117 | |
NumericTestGraph<I, V, E> graph) { |
118 | 0 | for (TestGraphModifier<I, V, E> graphModifier : graphModifiers) { |
119 | 0 | graphModifier.modifyGraph(graph); |
120 | |
} |
121 | 0 | } |
122 | |
}; |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public static BulkConfigurator chainConfigurators( |
132 | |
final BulkConfigurator... configurators) { |
133 | 0 | return new BulkConfigurator() { |
134 | |
@Override |
135 | |
public void configure(GiraphConfiguration conf) { |
136 | 0 | for (BulkConfigurator configurator : configurators) { |
137 | 0 | configurator.configure(conf); |
138 | |
} |
139 | 0 | } |
140 | |
}; |
141 | |
} |
142 | |
|
143 | |
|
144 | |
public static Supplier<DoubleWritable> doubleSupplier(final double value) { |
145 | 0 | return new Supplier<DoubleWritable>() { |
146 | |
@Override |
147 | |
public DoubleWritable get() { |
148 | 0 | return new DoubleWritable(value); |
149 | |
} |
150 | |
}; |
151 | |
} |
152 | |
|
153 | |
public static Supplier<NullWritable> nullSupplier() { |
154 | 0 | return new Supplier<NullWritable>() { |
155 | |
@Override |
156 | |
public NullWritable get() { |
157 | 0 | return NullWritable.get(); |
158 | |
} |
159 | |
}; |
160 | |
} |
161 | |
|
162 | |
|
163 | |
interface OneTest { |
164 | |
void test() throws Exception; |
165 | |
} |
166 | |
|
167 | |
private static void repeat(int times, OneTest test) throws Exception { |
168 | 0 | if (times == 1) { |
169 | 0 | test.test(); |
170 | |
} else { |
171 | 0 | int failures = 0; |
172 | 0 | StringBuilder failureMsgs = new StringBuilder(); |
173 | 0 | AssertionError firstError = null; |
174 | 0 | for (int i = 0; i < times; i++) { |
175 | |
try { |
176 | 0 | test.test(); |
177 | 0 | } catch (AssertionError error) { |
178 | 0 | failures++; |
179 | 0 | failureMsgs.append("\n").append(error.getMessage()); |
180 | 0 | if (firstError == null) { |
181 | 0 | firstError = error; |
182 | |
} |
183 | 0 | } |
184 | |
} |
185 | |
|
186 | 0 | if (failures > 0) { |
187 | 0 | throw new AssertionError( |
188 | |
"Failed " + failures + " times out of " + times + |
189 | |
" runs, messages: " + failureMsgs, |
190 | |
firstError); |
191 | |
} |
192 | |
} |
193 | 0 | } |
194 | |
|
195 | |
private static boolean useFullDigraphTests(GiraphConfiguration conf) { |
196 | 0 | return USE_FULL_GIRAPH_ENV_IN_TESTS.get(conf) || |
197 | 0 | System.getProperty("test_setup.UseFullGiraphEnvInTests") != null; |
198 | |
} |
199 | |
|
200 | |
private static int repeatTimes() { |
201 | 0 | String value = System.getProperty("test_setup.TestRepeatTimes"); |
202 | 0 | return value != null ? Integer.parseInt(value) : TEST_REPEAT_TIMES; |
203 | |
} |
204 | |
|
205 | |
public static void setTestRepeatTimes(int testRepeatTimes) { |
206 | 0 | TestGraphUtils.TEST_REPEAT_TIMES = testRepeatTimes; |
207 | 0 | } |
208 | |
} |