Author: tgraves
Date: Tue Apr 24 14:01:00 2012
New Revision: 1329732
URL: http://svn.apache.org/viewvc?rev=1329732&view=rev
Log:
MAPREDUCE-4079. Allow MR AppMaster to limit ephemeral port range.(bobby via
tgraves)
Added:
hadoop/common/branches/branch-0.23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java
Added:
hadoop/common/branches/branch-0.23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java
URL:
http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java?rev=1329732&view=auto
==============================================================================
---
hadoop/common/branches/branch-0.23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java
(added)
+++
hadoop/common/branches/branch-0.23/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestServer.java
Tue Apr 24 14:01:00 2012
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.ipc;
+
+import static org.junit.Assert.*;
+
+import java.net.BindException;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.Test;
+
+/**
+ * This is intended to be a set of unit tests for the
+ * org.apache.hadoop.ipc.Server class.
+ */
+public class TestServer {
+
+ @Test
+ public void testBind() throws Exception {
+ Configuration conf = new Configuration();
+ ServerSocket socket = new ServerSocket();
+ InetSocketAddress address = new InetSocketAddress("0.0.0.0",0);
+ socket.bind(address);
+ try {
+ int min = socket.getLocalPort();
+ int max = min + 100;
+ conf.set("TestRange", min+"-"+max);
+
+
+ ServerSocket socket2 = new ServerSocket();
+ InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
+ Server.bind(socket2, address2, 10, conf, "TestRange");
+ try {
+ assertTrue(socket2.isBound());
+ assertTrue(socket2.getLocalPort() > min);
+ assertTrue(socket2.getLocalPort() <= max);
+ } finally {
+ socket2.close();
+ }
+ } finally {
+ socket.close();
+ }
+ }
+
+ @Test
+ public void testBindSimple() throws Exception {
+ ServerSocket socket = new ServerSocket();
+ InetSocketAddress address = new InetSocketAddress("0.0.0.0",0);
+ Server.bind(socket, address, 10);
+ try {
+ assertTrue(socket.isBound());
+ } finally {
+ socket.close();
+ }
+ }
+
+ @Test
+ public void testEmptyConfig() throws Exception {
+ Configuration conf = new Configuration();
+ conf.set("TestRange", "");
+
+
+ ServerSocket socket = new ServerSocket();
+ InetSocketAddress address = new InetSocketAddress("0.0.0.0", 0);
+ try {
+ Server.bind(socket, address, 10, conf, "TestRange");
+ assertTrue(socket.isBound());
+ } finally {
+ socket.close();
+ }
+ }
+
+
+ @Test
+ public void testBindError() throws Exception {
+ Configuration conf = new Configuration();
+ ServerSocket socket = new ServerSocket();
+ InetSocketAddress address = new InetSocketAddress("0.0.0.0",0);
+ socket.bind(address);
+ try {
+ int min = socket.getLocalPort();
+ conf.set("TestRange", min+"-"+min);
+
+
+ ServerSocket socket2 = new ServerSocket();
+ InetSocketAddress address2 = new InetSocketAddress("0.0.0.0", 0);
+ boolean caught = false;
+ try {
+ Server.bind(socket2, address2, 10, conf, "TestRange");
+ } catch (BindException e) {
+ caught = true;
+ } finally {
+ socket2.close();
+ }
+ assertTrue("Failed to catch the expected bind exception",caught);
+ } finally {
+ socket.close();
+ }
+ }
+}