This project has retired. For details please refer to its Attic page.
SendingMessagesTest 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  package org.apache.giraph.block_app.framework;
19  
20  
21  import org.apache.giraph.block_app.framework.block.Block;
22  import org.apache.giraph.block_app.library.Pieces;
23  import org.apache.giraph.block_app.library.VertexSuppliers;
24  import org.apache.giraph.block_app.test_setup.NumericTestGraph;
25  import org.apache.giraph.block_app.test_setup.TestGraphChecker;
26  import org.apache.giraph.block_app.test_setup.TestGraphModifier;
27  import org.apache.giraph.block_app.test_setup.TestGraphUtils;
28  import org.apache.giraph.conf.BulkConfigurator;
29  import org.apache.giraph.conf.GiraphConfiguration;
30  import org.apache.giraph.conf.GiraphConstants;
31  import org.apache.giraph.function.vertex.ConsumerWithVertex;
32  import org.apache.giraph.graph.Vertex;
33  import org.apache.hadoop.io.LongWritable;
34  import org.apache.hadoop.io.Writable;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  public class SendingMessagesTest {
39    @Test
40    public void createVertexOnMsgsTest() throws Exception {
41      TestGraphUtils.runTest(
42          new TestGraphModifier<LongWritable, LongWritable, Writable>() {
43            @Override
44            public void modifyGraph(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
45              graph.addEdge(1, 2);
46            }
47          },
48          new TestGraphChecker<LongWritable, LongWritable, Writable>() {
49            @Override
50            public void checkOutput(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
51              Assert.assertEquals(1, graph.getValue(2).get());
52              Assert.assertEquals(0, graph.getValue(1).get());
53            }
54          },
55          new BulkConfigurator() {
56            @Override
57            public void configure(GiraphConfiguration conf) {
58              BlockUtils.setBlockFactoryClass(conf, SendingMessagesToNeighborsBlockFactory.class);
59            }
60          });
61    }
62  
63    @Test
64    public void doNotCreateVertexOnMsgsTest() throws Exception {
65      TestGraphUtils.runTest(
66          new TestGraphModifier<LongWritable, LongWritable, Writable>() {
67            @Override
68            public void modifyGraph(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
69              graph.addEdge(1, 2);
70            }
71          },
72          new TestGraphChecker<LongWritable, LongWritable, Writable>() {
73            @Override
74            public void checkOutput(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
75              Assert.assertNull(graph.getVertex(2));
76              Assert.assertEquals(0, graph.getValue(1).get());
77            }
78          },
79          new BulkConfigurator() {
80            @Override
81            public void configure(GiraphConfiguration conf) {
82              BlockUtils.setBlockFactoryClass(conf, SendingMessagesToNeighborsBlockFactory.class);
83              GiraphConstants.RESOLVER_CREATE_VERTEX_ON_MSGS.set(conf, false);
84            }
85          });
86    }
87  
88    @Test
89    public void createMultiMsgs() throws Exception {
90      TestGraphUtils.runTest(
91          new TestGraphModifier<LongWritable, LongWritable, Writable>() {
92            @Override
93            public void modifyGraph(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
94              graph.addSymmetricEdge(1, 2);
95              graph.addSymmetricEdge(3, 2);
96            }
97          },
98          new TestGraphChecker<LongWritable, LongWritable, Writable>() {
99            @Override
100           public void checkOutput(NumericTestGraph<LongWritable, LongWritable, Writable> graph) {
101             Assert.assertEquals(3, graph.getValue(2).get());
102             Assert.assertEquals(2, graph.getValue(1).get());
103             Assert.assertEquals(2, graph.getValue(3).get());
104           }
105         },
106         new BulkConfigurator() {
107           @Override
108           public void configure(GiraphConfiguration conf) {
109             BlockUtils.setBlockFactoryClass(conf, SendingMessagesToNeighborsBlockFactory.class);
110           }
111         });
112   }
113 
114   public static class SendingMessagesToNeighborsBlockFactory extends TestLongNullNullBlockFactory {
115     @Override
116     protected Class<? extends Writable> getVertexValueClass(GiraphConfiguration conf) {
117       return LongWritable.class;
118     }
119 
120     @Override
121     public Block createBlock(GiraphConfiguration conf) {
122       return Pieces.sendMessageToNeighbors(
123           "SendToNeighbors",
124           LongWritable.class,
125           VertexSuppliers.<LongWritable, LongWritable, Writable>vertexIdSupplier(),
126           new ConsumerWithVertex<LongWritable, LongWritable, Writable, Iterable<LongWritable>>() {
127             @Override
128             public void apply(Vertex<LongWritable, LongWritable, Writable> vertex,
129                 Iterable<LongWritable> messages) {
130               long max = 0;
131               for (LongWritable v : messages) {
132                 max = Math.max(max, v.get());
133               }
134               vertex.getValue().set(max);
135             }
136           });
137     }
138   }
139 }