You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by st...@apache.org on 2009/08/29 00:13:23 UTC
svn commit: r809044 - in /hadoop/common/branches/HADOOP-6194/src:
java/org/apache/hadoop/http/HttpServer.java
java/org/apache/hadoop/util/MockService.java
java/org/apache/hadoop/util/Service.java
test/core/org/apache/hadoop/util/TestServiceLifecycle.java
Author: stevel
Date: Fri Aug 28 22:13:23 2009
New Revision: 809044
URL: http://svn.apache.org/viewvc?rev=809044&view=rev
Log:
HADOOP-6194 some more diagnostics and tests
Modified:
hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/http/HttpServer.java
hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/MockService.java
hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/Service.java
hadoop/common/branches/HADOOP-6194/src/test/core/org/apache/hadoop/util/TestServiceLifecycle.java
Modified: hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/http/HttpServer.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/http/HttpServer.java?rev=809044&r1=809043&r2=809044&view=diff
==============================================================================
--- hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/http/HttpServer.java (original)
+++ hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/http/HttpServer.java Fri Aug 28 22:13:23 2009
@@ -512,6 +512,17 @@
}
/**
+ * Return the host and port of the HttpServer, if live
+ * @return the classname and any HTTP URL
+ */
+ @Override
+ public String toString() {
+ return listener != null ?
+ ("HttpServer at http://" + listener.getHost() + ":" + listener.getLocalPort() + "/")
+ : "Inactive HttpServer";
+ }
+
+ /**
* A very simple servlet to serve up a text representation of the current
* stack traces. It both returns the stacks to the caller and logs them.
* Currently the stack traces are done sequentially rather than exactly the
Modified: hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/MockService.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/MockService.java?rev=809044&r1=809043&r2=809044&view=diff
==============================================================================
--- hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/MockService.java (original)
+++ hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/MockService.java Fri Aug 28 22:13:23 2009
@@ -37,7 +37,7 @@
/**
* Build from a configuration file
- * @param conf
+ * @param conf configuration
*/
public MockService(Configuration conf) {
super(conf);
@@ -144,4 +144,20 @@
super(message);
}
}
+
+ /**
+ * To test lifecycle events, counts up every time something happens
+ */
+ public static class LifecycleEventCount implements StateChangeListener {
+ private int count = 0;
+
+ @Override
+ public synchronized void onStateChange(Service service, ServiceState oldState, ServiceState newState) {
+ count++;
+ }
+
+ public synchronized int getCount() {
+ return count;
+ }
+ }
}
Modified: hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/Service.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/Service.java?rev=809044&r1=809043&r2=809044&view=diff
==============================================================================
--- hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/Service.java (original)
+++ hadoop/common/branches/HADOOP-6194/src/java/org/apache/hadoop/util/Service.java Fri Aug 28 22:13:23 2009
@@ -251,7 +251,7 @@
*
* @throws IOException if an I/O error occurs
*/
- public void close() throws IOException {
+ public final void close() throws IOException {
if (enterState(ServiceState.CLOSED)) {
innerClose();
}
@@ -313,7 +313,7 @@
* @param oldState the old state of the service
* @param newState the new state
*/
- protected boolean isValidStateTransition(ServiceState oldState,
+ protected final boolean isValidStateTransition(ServiceState oldState,
ServiceState newState) {
switch(oldState) {
case CREATED:
@@ -382,7 +382,7 @@
* When did the service last change state
* @return the last state change of this service
*/
- public Date getLastStateChange() {
+ public final Date getLastStateChange() {
return lastStateChange;
}
Modified: hadoop/common/branches/HADOOP-6194/src/test/core/org/apache/hadoop/util/TestServiceLifecycle.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/HADOOP-6194/src/test/core/org/apache/hadoop/util/TestServiceLifecycle.java?rev=809044&r1=809043&r2=809044&view=diff
==============================================================================
--- hadoop/common/branches/HADOOP-6194/src/test/core/org/apache/hadoop/util/TestServiceLifecycle.java (original)
+++ hadoop/common/branches/HADOOP-6194/src/test/core/org/apache/hadoop/util/TestServiceLifecycle.java Fri Aug 28 22:13:23 2009
@@ -20,7 +20,6 @@
import junit.framework.TestCase;
import java.io.IOException;
-import java.util.List;
/**
@@ -29,6 +28,7 @@
public class TestServiceLifecycle extends TestCase {
private MockService service;
+ private MockService.LifecycleEventCount counter;
public TestServiceLifecycle(String name) {
super(name);
@@ -38,6 +38,8 @@
protected void setUp() throws Exception {
super.setUp();
service = new MockService();
+ counter = new MockService.LifecycleEventCount();
+ service.addStateChangeListener(counter);
}
@Override
@@ -52,7 +54,7 @@
private void close() throws IOException {
service.close();
- assertInTerminatedState();
+ assertInClosedState();
}
protected void assertInState(Service.ServiceState state)
@@ -72,7 +74,7 @@
assertInState(Service.ServiceState.FAILED);
}
- private void assertInTerminatedState() throws Service.ServiceStateException {
+ private void assertInClosedState() throws Service.ServiceStateException {
assertInState(Service.ServiceState.CLOSED);
}
@@ -95,7 +97,7 @@
enterState(Service.ServiceState.FAILED);
}
- private void enterTerminatedState() throws Service.ServiceStateException {
+ private void enterClosedState() throws Service.ServiceStateException {
enterState(Service.ServiceState.CLOSED);
}
@@ -105,6 +107,14 @@
service.getStateChangeCount());
}
+ private void assertStateListenerCount(int expected) {
+ assertEquals("Wrong listener state change count for " + service,
+ expected,
+ counter.getCount());
+ }
+
+
+
private void assertNoStartFromState(Service.ServiceState serviceState)
throws Throwable {
enterState(serviceState);
@@ -123,6 +133,7 @@
/**
* Walk through the lifecycle and check it changes visible state
+ * @throws Throwable if something went wrong
*/
public void testBasicLifecycle() throws Throwable {
assertInCreatedState();
@@ -141,6 +152,9 @@
* @throws Throwable if something went wrong
*/
public void testStartIdempotent() throws Throwable {
+ //remove the counter, see it works. twice
+ service.removeStateChangeListener(counter);
+ service.removeStateChangeListener(counter);
start();
int count = service.getStateChangeCount();
//declare that we want to fail in our start operation
@@ -170,17 +184,21 @@
public void testStaticCloseOperation() throws Throwable {
Service.close(service);
- assertInTerminatedState();
+ assertInClosedState();
Service.close(service);
}
public void testFailInStart() throws Throwable {
service.setFailOnStart(true);
+ int count = service.getStateChangeCount();
try {
start();
failShouldNotGetHere();
} catch (MockService.MockServiceException e) {
assertInFailedState();
+ //we should have entered two more states, STARED and FAILED
+ assertStateChangeCount(count+2);
+ assertStateListenerCount(count+2);
}
}
@@ -202,7 +220,7 @@
try {
Service.startService(service);
} catch (MockService.MockServiceException e) {
- assertInTerminatedState();
+ assertInClosedState();
}
}
@@ -227,7 +245,7 @@
service.close();
fail("Should have thrown an exception");
} catch (IOException e) {
- assertInTerminatedState();
+ assertInClosedState();
assertTrue(service.isClosed());
}
//the second call should be a no-op; no exceptions get thrown
@@ -246,10 +264,10 @@
public void testFailFromTerminatedDoesNotChangeState() throws Throwable {
Service.startService(service);
service.close();
- assertInTerminatedState();
+ assertInClosedState();
Exception cause = new Exception("test");
service.enterFailedState(cause);
- assertInTerminatedState();
+ assertInClosedState();
assertEquals(cause,service.getFailureCause());
}