1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.block_app.test_setup;
1920import org.apache.giraph.block_app.framework.BlockUtils;
21import org.apache.giraph.conf.BulkConfigurator;
22import org.apache.giraph.block_app.framework.api.local.LocalBlockRunner;
23import org.apache.giraph.conf.BooleanConfOption;
24import org.apache.giraph.conf.GiraphConfiguration;
25import org.apache.giraph.function.Supplier;
26import org.apache.hadoop.io.DoubleWritable;
27import org.apache.hadoop.io.NullWritable;
28import org.apache.hadoop.io.Writable;
29import org.apache.hadoop.io.WritableComparable;
3031/**32 * Utility functions for running TestGraph unit tests.33 */34publicclassTestGraphUtils {
35/** modify locally for running full Digraph tests from IDE */36publicstaticfinal37BooleanConfOption USE_FULL_GIRAPH_ENV_IN_TESTS = newBooleanConfOption(
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");
4142// if you want to check stability of the test and make sure it passes always43// test it with larger number, like ~10.44privatestaticint TEST_REPEAT_TIMES = 1;
4546privateTestGraphUtils() { }
4748/**49 * Creates configuration using configurator, initializes the graph using50 * graphInitializer, and checks it via graphChecker.51 *52 * Supports using TEST_REPEAT_TIMES for running the same test multiple times.53 */54publicstatic55 <I extends WritableComparable, V extends Writable, E extends Writable>
56void runTest(
57final TestGraphModifier<? super I, ? super V, ? super E> graphInitializer,
58final TestGraphChecker<? super I, ? super V, ? super E> graphChecker,
59finalBulkConfigurator configurator) throws Exception {
60 repeat(
61 repeatTimes(),
62newOneTest() {
63 @Override
64publicvoid test() throws Exception {
65GiraphConfiguration conf = newGiraphConfiguration();
66 configurator.configure(conf);
67 BlockUtils.initAndCheckConfig(conf);
68 runTest(graphInitializer, graphChecker, conf);
69 }
70 });
71 }
7273/**74 * Uses provided configuration, initializes the graph using75 * graphInitializer, and checks it via graphChecker.76 */77publicstatic78 <I extends WritableComparable, E extends Writable, V extends Writable>
79void runTest(
80 TestGraphModifier<? super I, ? super V, ? super E> graphInitializer,
81 TestGraphChecker<? super I, ? super V, ? super E> graphChecker,
82GiraphConfiguration conf) throws Exception {
83 NumericTestGraph<I, V, E> graph = new NumericTestGraph<>(conf);
84 graphInitializer.modifyGraph((NumericTestGraph) graph);
85 runTest(graph, graphChecker);
86 }
8788/**89 * Base of runTest. Takes a created graph, a graph-checker and conf and runs90 * the test.91 */92publicstatic93 <I extends WritableComparable, E extends Writable, V extends Writable>
94void runTest(
95 NumericTestGraph<I, V, E> graph,
96 TestGraphChecker<? super I, ? super V, ? super E> graphChecker
97 ) throws Exception {
98 graph = new NumericTestGraph<I, V, E>(
99 LocalBlockRunner.runApp(
100 graph.getTestGraph(), useFullDigraphTests(graph.getConf())));
101if (graphChecker != null) {
102 graphChecker.checkOutput((NumericTestGraph) graph);
103 }
104 }
105106/**107 * Chain execution of multiple TestGraphModifier into one.108 */109 @SafeVarargs
110publicstatic111 <I extends WritableComparable, V extends Writable, E extends Writable>
112 TestGraphModifier<I, V, E> chainModifiers(
113final TestGraphModifier<I, V, E>... graphModifiers) {
114returnnew TestGraphModifier<I, V, E>() {
115 @Override
116publicvoid modifyGraph(
117 NumericTestGraph<I, V, E> graph) {
118for (TestGraphModifier<I, V, E> graphModifier : graphModifiers) {
119 graphModifier.modifyGraph(graph);
120 }
121 }
122 };
123 }
124125/**126 * Chain execution of multiple BulkConfigurators into one.127 *128 * Order might matter, if they are setting the same fields.129 * (later one will override what previous one already set).130 */131publicstaticBulkConfigurator chainConfigurators(
132final BulkConfigurator... configurators) {
133returnnewBulkConfigurator() {
134 @Override
135publicvoid configure(GiraphConfiguration conf) {
136for (BulkConfigurator configurator : configurators) {
137 configurator.configure(conf);
138 }
139 }
140 };
141 }
142143144publicstatic Supplier<DoubleWritable> doubleSupplier(finaldouble value) {
145returnnew Supplier<DoubleWritable>() {
146 @Override
147public DoubleWritable get() {
148returnnew DoubleWritable(value);
149 }
150 };
151 }
152153publicstatic Supplier<NullWritable> nullSupplier() {
154returnnew Supplier<NullWritable>() {
155 @Override
156public NullWritable get() {
157return NullWritable.get();
158 }
159 };
160 }
161162/** Interface for running a single test that can throw an exception */163interfaceOneTest {
164void test() throws Exception;
165 }
166167privatestaticvoid repeat(int times, OneTest test) throws Exception {
168if (times == 1) {
169 test.test();
170 } else {
171int failures = 0;
172 StringBuilder failureMsgs = new StringBuilder();
173 AssertionError firstError = null;
174for (int i = 0; i < times; i++) {
175try {
176 test.test();
177 } catch (AssertionError error) {
178 failures++;
179 failureMsgs.append("\n").append(error.getMessage());
180if (firstError == null) {
181 firstError = error;
182 }
183 }
184 }
185186if (failures > 0) {
187thrownew AssertionError(
188"Failed " + failures + " times out of " + times +
189" runs, messages: " + failureMsgs,
190 firstError);
191 }
192 }
193 }
194195privatestaticboolean useFullDigraphTests(GiraphConfiguration conf) {
196return USE_FULL_GIRAPH_ENV_IN_TESTS.get(conf) ||
197 System.getProperty("test_setup.UseFullGiraphEnvInTests") != null;
198 }
199200privatestaticint repeatTimes() {
201 String value = System.getProperty("test_setup.TestRepeatTimes");
202return value != null ? Integer.parseInt(value) : TEST_REPEAT_TIMES;
203 }
204205publicstaticvoid setTestRepeatTimes(int testRepeatTimes) {
206 TestGraphUtils.TEST_REPEAT_TIMES = testRepeatTimes;
207 }
208 }