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 */1819package org.apache.giraph.comm;
2021import org.apache.giraph.comm.netty.NettyClient;
22import org.apache.giraph.comm.netty.NettyServer;
23import org.apache.giraph.comm.netty.handler.SaslServerHandler;
24import org.apache.giraph.comm.netty.handler.WorkerRequestServerHandler;
25import org.apache.giraph.conf.GiraphConfiguration;
26import org.apache.giraph.conf.GiraphConstants;
27import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
28import org.apache.giraph.utils.IntNoOpComputation;
29import org.apache.giraph.utils.MockUtils;
30import org.apache.giraph.worker.WorkerInfo;
31import org.apache.hadoop.io.IntWritable;
32import org.apache.hadoop.mapreduce.Mapper.Context;
33import org.junit.Before;
34import org.junit.Test;
35import org.mockito.Mockito;
3637import com.google.common.collect.Lists;
3839import java.io.IOException;
4041importstatic org.mockito.Mockito.mock;
42importstatic org.mockito.Mockito.when;
4344/**45 * Netty connection with mocked authentication.46 */47publicclassSaslConnectionTest {
48/** Class configuration */49private ImmutableClassesGiraphConfiguration conf;
5051 @Before
52publicvoid setUp() {
53 GiraphConfiguration tmpConfig = new GiraphConfiguration();
54 tmpConfig.setComputationClass(IntNoOpComputation.class);
55 GiraphConstants.AUTHENTICATE.set(tmpConfig, true);
56 conf = new ImmutableClassesGiraphConfiguration(tmpConfig);
57 }
5859/**60 * Test connecting a single client to a single server.61 *62 * @throws IOException63 */64 @Test
65publicvoid connectSingleClientServer() throws IOException {
66 @SuppressWarnings("rawtypes")
67 Context context = mock(Context.class);
68 when(context.getConfiguration()).thenReturn(conf);
6970 ServerData<IntWritable, IntWritable, IntWritable> serverData =
71 MockUtils.createNewServerData(conf, context);
7273 SaslServerHandler.Factory mockedSaslServerFactory =
74 Mockito.mock(SaslServerHandler.Factory.class);
7576 SaslServerHandler mockedSaslServerHandler =
77 Mockito.mock(SaslServerHandler.class);
78 when(mockedSaslServerFactory.newHandler(conf)).
79 thenReturn(mockedSaslServerHandler);
8081 WorkerInfo workerInfo = new WorkerInfo();
82 workerInfo.setTaskId(-1);
83 NettyServer server =
84new NettyServer(conf,
85new WorkerRequestServerHandler.Factory(serverData),
86 workerInfo,
87 context,
88 mockedSaslServerFactory,
89newMockExceptionHandler());
90 server.start();
91 workerInfo.setInetSocketAddress(server.getMyAddress(), server.getLocalHostOrIp());
9293 NettyClient client = new NettyClient(context, conf, new WorkerInfo(),
94newMockExceptionHandler());
95 server.setFlowControl(client.getFlowControl());
96 client.connectAllAddresses(Lists.<WorkerInfo>newArrayList(workerInfo));
9798 client.stop();
99 server.stop();
100 }
101 }