HADOOP-8218. RPC.closeProxy shouldn't throw error when closing a mock. Contributed by Todd Lipcon.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.23@1306160 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Todd Lipcon 2012-03-28 04:52:51 +00:00
parent 0e3be7e01e
commit ddef0a75bd
7 changed files with 110 additions and 53 deletions

View File

@ -184,6 +184,9 @@ Release 0.23.3 - UNRELEASED
HADOOP-8202. RPC stopProxy() does not close the proxy correctly. HADOOP-8202. RPC stopProxy() does not close the proxy correctly.
(Hari Mankude via suresh) (Hari Mankude via suresh)
HADOOP-8218. RPC.closeProxy shouldn't throw error when closing a mock
(todd)
BREAKDOWN OF HADOOP-7454 SUBTASKS BREAKDOWN OF HADOOP-7454 SUBTASKS
HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh) HADOOP-7455. HA: Introduce HA Service Protocol Interface. (suresh)

View File

@ -17,7 +17,6 @@
*/ */
package org.apache.hadoop.ha; package org.apache.hadoop.ha;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
@ -195,9 +194,7 @@ class HealthMonitor {
} catch (Throwable t) { } catch (Throwable t) {
LOG.warn("Transport-level exception trying to monitor health of " + LOG.warn("Transport-level exception trying to monitor health of " +
targetToMonitor + ": " + t.getLocalizedMessage()); targetToMonitor + ": " + t.getLocalizedMessage());
if (proxy instanceof Closeable) {
RPC.stopProxy(proxy); RPC.stopProxy(proxy);
}
proxy = null; proxy = null;
enterState(State.SERVICE_NOT_RESPONDING); enterState(State.SERVICE_NOT_RESPONDING);
Thread.sleep(sleepAfterDisconnectMillis); Thread.sleep(sleepAfterDisconnectMillis);

View File

@ -604,6 +604,9 @@ public class RPC {
LOG.error("RPC.stopProxy called on non proxy.", e); LOG.error("RPC.stopProxy called on non proxy.", e);
} }
// If you see this error on a mock object in a unit test you're
// developing, make sure to use MockitoUtil.mockProtocol() to
// create your mock.
throw new HadoopIllegalArgumentException( throw new HadoopIllegalArgumentException(
"Cannot close proxy - is not Closeable or " "Cannot close proxy - is not Closeable or "
+ "does not provide closeable invocation handler " + "does not provide closeable invocation handler "

View File

@ -17,6 +17,7 @@
*/ */
package org.apache.hadoop.ha; package org.apache.hadoop.ha;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.ArrayList; import java.util.ArrayList;
@ -56,7 +57,40 @@ class DummyHAService extends HAServiceTarget {
} }
private HAServiceProtocol makeMock() { private HAServiceProtocol makeMock() {
return Mockito.spy(new HAServiceProtocol() { return Mockito.spy(new MockHAProtocolImpl());
}
@Override
public InetSocketAddress getAddress() {
return address;
}
@Override
public HAServiceProtocol getProxy(Configuration conf, int timeout)
throws IOException {
return proxy;
}
@Override
public NodeFencer getFencer() {
return fencer;
}
@Override
public void checkFencingConfigured() throws BadFencingConfigurationException {
}
@Override
public String toString() {
return "DummyHAService #" + index;
}
public static HAServiceTarget getInstance(int serial) {
return instances.get(serial - 1);
}
private class MockHAProtocolImpl implements
HAServiceProtocol, Closeable {
@Override @Override
public void monitorHealth() throws HealthCheckFailedException, public void monitorHealth() throws HealthCheckFailedException,
AccessControlException, IOException { AccessControlException, IOException {
@ -99,35 +133,9 @@ class DummyHAService extends HAServiceTarget {
throw new IOException("Connection refused (fake)"); throw new IOException("Connection refused (fake)");
} }
} }
});
}
@Override @Override
public InetSocketAddress getAddress() { public void close() throws IOException {
return address;
} }
@Override
public HAServiceProtocol getProxy(Configuration conf, int timeout)
throws IOException {
return proxy;
}
@Override
public NodeFencer getFencer() {
return fencer;
}
@Override
public void checkFencingConfigured() throws BadFencingConfigurationException {
}
@Override
public String toString() {
return "DummyHAService #" + index;
}
public static HAServiceTarget getInstance(int serial) {
return instances.get(serial - 1);
} }
} }

View File

@ -17,6 +17,7 @@
*/ */
package org.apache.hadoop.ha; package org.apache.hadoop.ha;
import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ -33,7 +34,6 @@ import org.apache.hadoop.security.AccessControlException;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.ThrowsException; import org.mockito.internal.stubbing.answers.ThrowsException;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -228,7 +228,10 @@ public class TestFailoverController {
// Getting a proxy to a dead server will throw IOException on call, // Getting a proxy to a dead server will throw IOException on call,
// not on creation of the proxy. // not on creation of the proxy.
HAServiceProtocol errorThrowingProxy = Mockito.mock(HAServiceProtocol.class, HAServiceProtocol errorThrowingProxy = Mockito.mock(HAServiceProtocol.class,
new ThrowsException(new IOException("Could not connect to host"))); Mockito.withSettings()
.defaultAnswer(new ThrowsException(
new IOException("Could not connect to host")))
.extraInterfaces(Closeable.class));
Mockito.doReturn(errorThrowingProxy).when(svc1).getProxy(); Mockito.doReturn(errorThrowingProxy).when(svc1).getProxy();
DummyHAService svc2 = new DummyHAService(HAServiceState.STANDBY, svc2Addr); DummyHAService svc2 = new DummyHAService(HAServiceState.STANDBY, svc2Addr);
svc1.fencer = svc2.fencer = setupFencer(AlwaysSucceedFencer.class.getName()); svc1.fencer = svc2.fencer = setupFencer(AlwaysSucceedFencer.class.getName());

View File

@ -50,6 +50,7 @@ import org.apache.hadoop.security.token.SecretManager;
import org.apache.hadoop.security.token.TokenIdentifier; import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.security.AccessControlException; import org.apache.hadoop.security.AccessControlException;
import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.test.MockitoUtil;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -58,8 +59,6 @@ import com.google.protobuf.DescriptorProtos.EnumDescriptorProto;
import static org.apache.hadoop.test.MetricsAsserts.*; import static org.apache.hadoop.test.MetricsAsserts.*;
import static org.mockito.Mockito.*;
/** Unit tests for RPC. */ /** Unit tests for RPC. */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class TestRPC { public class TestRPC {
@ -587,10 +586,18 @@ public class TestRPC {
*/ */
@Test(expected=HadoopIllegalArgumentException.class) @Test(expected=HadoopIllegalArgumentException.class)
public void testStopNonRegisteredProxy() throws Exception { public void testStopNonRegisteredProxy() throws Exception {
RPC.stopProxy(mock(TestProtocol.class));
RPC.stopProxy(null); RPC.stopProxy(null);
} }
/**
* Test that the mockProtocol helper returns mock proxies that can
* be stopped without error.
*/
@Test
public void testStopMockObject() throws Exception {
RPC.stopProxy(MockitoUtil.mockProtocol(TestProtocol.class));
}
@Test @Test
public void testStopProxy() throws IOException { public void testStopProxy() throws IOException {
StoppedProtocol proxy = (StoppedProtocol) RPC.getProxy(StoppedProtocol.class, StoppedProtocol proxy = (StoppedProtocol) RPC.getProxy(StoppedProtocol.class,

View File

@ -0,0 +1,36 @@
/**
* 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.test;
import java.io.Closeable;
import org.mockito.Mockito;
public abstract class MockitoUtil {
/**
* Return a mock object for an IPC protocol. This special
* method is necessary, since the IPC proxies have to implement
* Closeable in addition to their protocol interface.
* @param clazz the protocol class
*/
public static <T> T mockProtocol(Class<T> clazz) {
return Mockito.mock(clazz,
Mockito.withSettings().extraInterfaces(Closeable.class));
}
}