This project has retired. For details please refer to its Attic page.
TryMultiIpcBindingPortsTest xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  
19  package org.apache.giraph.examples;
20  
21  import org.apache.giraph.combiner.MinimumIntMessageCombiner;
22  import org.apache.giraph.conf.GiraphConfiguration;
23  import org.apache.giraph.conf.GiraphConstants;
24  import org.apache.giraph.edge.ByteArrayEdges;
25  import org.apache.giraph.io.formats.IdWithValueTextOutputFormat;
26  import org.apache.giraph.io.formats.IntIntNullTextInputFormat;
27  import org.apache.giraph.utils.InternalVertexRunner;
28  import org.junit.Test;
29  
30  import com.google.common.base.Splitter;
31  import com.google.common.collect.HashMultimap;
32  import com.google.common.collect.Iterables;
33  import com.google.common.collect.SetMultimap;
34  
35  import java.util.Set;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.assertTrue;
39  
40  /**
41   *  Tests for {@link TryMultiIpcBindingPortsTest}
42   */
43  public class TryMultiIpcBindingPortsTest {
44  
45      /**
46       * A local integration test on toy data
47       */
48      @Test
49      public void testToyData() throws Exception {
50  
51          // a small graph with three components
52          String[] graph = new String[] {
53                  "1 2 3",
54                  "2 1 4 5",
55                  "3 1 4",
56                  "4 2 3 5 13",
57                  "5 2 4 12 13",
58                  "12 5 13",
59                  "13 4 5 12",
60  
61                  "6 7 8",
62                  "7 6 10 11",
63                  "8 6 10",
64                  "10 7 8 11",
65                  "11 7 10",
66  
67                  "9" };
68  
69          // run internally
70          // fail the first port binding attempt
71          GiraphConfiguration conf = new GiraphConfiguration();
72          GiraphConstants.FAIL_FIRST_IPC_PORT_BIND_ATTEMPT.set(conf, true);
73          conf.setComputationClass(ConnectedComponentsComputation.class);
74          conf.setOutEdgesClass(ByteArrayEdges.class);
75          conf.setMessageCombinerClass(MinimumIntMessageCombiner.class);
76          conf.setVertexInputFormatClass(IntIntNullTextInputFormat.class);
77          conf.setVertexOutputFormatClass(IdWithValueTextOutputFormat.class);
78  
79          Iterable<String> results = InternalVertexRunner.run(conf, graph);
80  
81          SetMultimap<Integer,Integer> components = parseResults(results);
82  
83          Set<Integer> componentIDs = components.keySet();
84          assertEquals(3, componentIDs.size());
85          assertTrue(componentIDs.contains(1));
86          assertTrue(componentIDs.contains(6));
87          assertTrue(componentIDs.contains(9));
88  
89          Set<Integer> componentOne = components.get(1);
90          assertEquals(7, componentOne.size());
91          assertTrue(componentOne.contains(1));
92          assertTrue(componentOne.contains(2));
93          assertTrue(componentOne.contains(3));
94          assertTrue(componentOne.contains(4));
95          assertTrue(componentOne.contains(5));
96          assertTrue(componentOne.contains(12));
97          assertTrue(componentOne.contains(13));
98  
99          Set<Integer> componentTwo = components.get(6);
100         assertEquals(5, componentTwo.size());
101         assertTrue(componentTwo.contains(6));
102         assertTrue(componentTwo.contains(7));
103         assertTrue(componentTwo.contains(8));
104         assertTrue(componentTwo.contains(10));
105         assertTrue(componentTwo.contains(11));
106 
107         Set<Integer> componentThree = components.get(9);
108         assertEquals(1, componentThree.size());
109         assertTrue(componentThree.contains(9));
110     }
111 
112     private SetMultimap<Integer,Integer> parseResults(
113             Iterable<String> results) {
114         SetMultimap<Integer,Integer> components = HashMultimap.create();
115         for (String result : results) {
116             Iterable<String> parts = Splitter.on('\t').split(result);
117             int vertex = Integer.parseInt(Iterables.get(parts, 0));
118             int component = Integer.parseInt(Iterables.get(parts, 1));
119             components.put(component, vertex);
120         }
121         return components;
122     }
123 }