This project has retired. For details please refer to its Attic page.
SaslConnectionTest 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.comm;
20  
21  import org.apache.giraph.comm.netty.NettyClient;
22  import org.apache.giraph.comm.netty.NettyServer;
23  import org.apache.giraph.comm.netty.handler.SaslServerHandler;
24  import org.apache.giraph.comm.netty.handler.WorkerRequestServerHandler;
25  import org.apache.giraph.conf.GiraphConfiguration;
26  import org.apache.giraph.conf.GiraphConstants;
27  import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
28  import org.apache.giraph.utils.IntNoOpComputation;
29  import org.apache.giraph.utils.MockUtils;
30  import org.apache.giraph.worker.WorkerInfo;
31  import org.apache.hadoop.io.IntWritable;
32  import org.apache.hadoop.mapreduce.Mapper.Context;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.mockito.Mockito;
36  
37  import com.google.common.collect.Lists;
38  
39  import java.io.IOException;
40  
41  import static org.mockito.Mockito.mock;
42  import static org.mockito.Mockito.when;
43  
44  /**
45   * Netty connection with mocked authentication.
46   */
47  public class SaslConnectionTest {
48    /** Class configuration */
49    private ImmutableClassesGiraphConfiguration conf;
50  
51    @Before
52    public void setUp() {
53      GiraphConfiguration tmpConfig = new GiraphConfiguration();
54      tmpConfig.setComputationClass(IntNoOpComputation.class);
55      GiraphConstants.AUTHENTICATE.set(tmpConfig, true);
56      conf = new ImmutableClassesGiraphConfiguration(tmpConfig);
57    }
58  
59    /**
60     * Test connecting a single client to a single server.
61     *
62     * @throws IOException
63     */
64    @Test
65    public void connectSingleClientServer() throws IOException {
66      @SuppressWarnings("rawtypes")
67      Context context = mock(Context.class);
68      when(context.getConfiguration()).thenReturn(conf);
69  
70      ServerData<IntWritable, IntWritable, IntWritable> serverData =
71          MockUtils.createNewServerData(conf, context);
72  
73      SaslServerHandler.Factory mockedSaslServerFactory =
74          Mockito.mock(SaslServerHandler.Factory.class);
75  
76      SaslServerHandler mockedSaslServerHandler =
77          Mockito.mock(SaslServerHandler.class);
78      when(mockedSaslServerFactory.newHandler(conf)).
79          thenReturn(mockedSaslServerHandler);
80  
81      WorkerInfo workerInfo = new WorkerInfo();
82      workerInfo.setTaskId(-1);
83      NettyServer server =
84          new NettyServer(conf,
85              new WorkerRequestServerHandler.Factory(serverData),
86              workerInfo,
87              context,
88              mockedSaslServerFactory,
89              new MockExceptionHandler());
90      server.start();
91      workerInfo.setInetSocketAddress(server.getMyAddress(), server.getLocalHostOrIp());
92  
93      NettyClient client = new NettyClient(context, conf, new WorkerInfo(),
94          new MockExceptionHandler());
95      server.setFlowControl(client.getFlowControl());
96      client.connectAllAddresses(Lists.<WorkerInfo>newArrayList(workerInfo));
97  
98      client.stop();
99      server.stop();
100   }
101 }