Permalink
Browse files

Hub shutdown action + upgraded Selenium Remote Control.

git-svn-id: https://svn.openqa.org/svn/selenium-grid/trunk@361 22590ab4-ef3b-0410-9bef-a9f5a1089f47
  • Loading branch information...
1 parent 37667f7 commit e28b4ab25e861894e22ecefc15e1f88b7b728265 ph7 committed Aug 21, 2008
View
Oops, something went wrong.
View
@@ -6,8 +6,7 @@
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/ruby" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/integration" isTestSource="true" />
- <sourceFolder url="file://$MODULE_DIR$/src/test/unit" isTestSource="true" />
- <sourceFolder url="file://$MODULE_DIR$/src/test/unit/java" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/test/unit/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/src/test/integration" />
</content>
<orderEntry type="inheritedJdk" />
@@ -5,6 +5,7 @@
import com.thoughtworks.selenium.grid.configuration.ResourceLocator;
import com.thoughtworks.selenium.grid.hub.remotecontrol.DynamicRemoteControlPool;
import com.thoughtworks.selenium.grid.hub.remotecontrol.GlobalRemoteControlPool;
+import com.thoughtworks.selenium.grid.hub.management.LifecycleManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -20,6 +21,7 @@
private DynamicRemoteControlPool pool;
private EnvironmentManager environmentManager;
private GridConfiguration gridConfiguration;
+ private LifecycleManager lifecycleManager;
public static synchronized HubRegistry registry() {
if (null == singleton) {
@@ -55,4 +57,12 @@ public synchronized GridConfiguration gridConfiguration() {
}
return gridConfiguration;
}
+
+ public synchronized LifecycleManager lifecycleManager() {
+ if (null == lifecycleManager) {
+ lifecycleManager = new LifecycleManager();
+ }
+ return lifecycleManager;
+ }
+
}
@@ -3,6 +3,7 @@
import com.thoughtworks.selenium.grid.configuration.HubConfiguration;
import com.thoughtworks.selenium.grid.hub.management.RegistrationServlet;
import com.thoughtworks.selenium.grid.hub.management.UnregistrationServlet;
+import com.thoughtworks.selenium.grid.hub.management.LifecycleManagerServlet;
import com.thoughtworks.selenium.grid.hub.management.console.ConsoleServlet;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.ContextHandlerCollection;
@@ -33,6 +34,7 @@ public static void main(String[] args) throws Exception {
root.addServlet(new ServletHolder(new ConsoleServlet()), "/console");
root.addServlet(new ServletHolder(new RegistrationServlet()), "/registration-manager/register");
root.addServlet(new ServletHolder(new UnregistrationServlet()), "/registration-manager/unregister");
+ root.addServlet(new ServletHolder(new LifecycleManagerServlet()), "/lifecycle-manager");
server.start();
server.join();
@@ -0,0 +1,25 @@
+package com.thoughtworks.selenium.grid.hub.management;
+
+import com.thoughtworks.selenium.grid.hub.HubRegistry;
+
+import javax.servlet.http.HttpServlet;
+
+/**
+ * Base class for all servlets exposed by Selenium Grid Hub.
+ *
+ *
+ * Provides access to the global application registry via a method
+ * that can easily be overriden in unit tests.
+ */
+public class HubServlet extends HttpServlet {
+
+ /**
+ * Application Registry (singleton)
+ *
+ * @return A valid application registry. Never null.
+ */
+ protected HubRegistry registry() {
+ return HubRegistry.registry();
+ }
+
+}
@@ -0,0 +1,18 @@
+package com.thoughtworks.selenium.grid.hub.management;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Manages lifecyle of Hub components
+ */
+public class LifecycleManager {
+
+ private static final Log LOGGER = LogFactory.getLog(LifecycleManager.class);
+
+ public void shutdown() {
+ LOGGER.info("Shutting down Hub...");
+ System.exit(0);
+ }
+
+}
@@ -0,0 +1,28 @@
+package com.thoughtworks.selenium.grid.hub.management;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.ServletException;
+import java.io.IOException;
+
+/**
+ * Servlet in charge of shutting down the Hub.
+ */
+public class LifecycleManagerServlet extends HubServlet {
+
+ private static final Log LOGGER = LogFactory.getLog(LifecycleManagerServlet.class);
+
+ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ final String action;
+
+ action = request.getParameter("action");
+ LOGGER.info("Requesting life cycle manager action : '" + action + "'");
+ if ("shutdown".equals(action)) {
+ registry().lifecycleManager().shutdown();
+ }
+ }
+
+}
@@ -1,15 +1,12 @@
package com.thoughtworks.selenium.grid.hub.management;
-import com.thoughtworks.selenium.grid.hub.HubRegistry;
-
-import javax.servlet.http.HttpServlet;
+import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import javax.servlet.ServletException;
import java.io.IOException;
-public abstract class RegistrationManagementServlet extends HttpServlet {
+public abstract class RegistrationManagementServlet extends HubServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
@@ -23,15 +20,11 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
protected abstract void process(HttpServletRequest request, HttpServletResponse response) throws IOException;
- protected void witeSuccessfulResponse(HttpServletResponse response) throws IOException {
+ protected void writeSuccessfulResponse(HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().print("OK");
}
- protected HubRegistry registry() {
- return HubRegistry.registry();
- }
-
}
@@ -16,19 +16,19 @@
*/
public class RegistrationServlet extends RegistrationManagementServlet {
- private static final Log logger = LogFactory.getLog(RegistrationServlet.class);
+ private static final Log LOGGER = LogFactory.getLog(RegistrationServlet.class);
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
final RemoteControlProxy newRemoteControl;
final DynamicRemoteControlPool pool;
- logger.info("Registering new remote control...");
+ LOGGER.info("Registering new remote control...");
newRemoteControl = RemoteControlParser.parse(request);
pool = registry().remoteControlPool();
pool.register(newRemoteControl);
- logger.info("Registered " + newRemoteControl);
- witeSuccessfulResponse(response);
+ LOGGER.info("Registered " + newRemoteControl);
+ writeSuccessfulResponse(response);
}
@@ -25,7 +25,7 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
pool = registry().remoteControlPool();
pool.unregister(newRemoteControl);
logger.info("Unregistered " + newRemoteControl);
- witeSuccessfulResponse(response);
+ writeSuccessfulResponse(response);
}
}
@@ -33,4 +33,15 @@ public void gridConfigurationReturnsAUniqueInstance() {
HubRegistry.registry().gridConfiguration());
}
+ @Test
+ public void lifecyleManagerReturnsAValidManager() {
+ assertNotNull(HubRegistry.registry().lifecycleManager());
+ }
+
+ @Test
+ public void lifecyleManagerReturnsAUniqueInstance() {
+ assertSame(HubRegistry.registry().lifecycleManager(),
+ HubRegistry.registry().lifecycleManager());
+ }
+
}
@@ -0,0 +1,71 @@
+package com.thoughtworks.selenium.grid.hub.management;
+
+import com.thoughtworks.selenium.grid.hub.HubRegistry;
+import org.jbehave.classmock.UsingClassMock;
+import org.jbehave.core.mock.Mock;
+import org.junit.Test;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+public class LifecyleManagerServletTest extends UsingClassMock {
+
+ @Test
+ public void shutdownHubWhenPostingAShutdownAction() throws IOException, ServletException {
+ final Mock lifecyleManager;
+ final LifecycleManagerServlet servlet;
+ final Mock registry;
+ final Mock request;
+ final Mock expectedResponse;
+
+ request = mock(HttpServletRequest.class);
+ expectedResponse = mock(HttpServletResponse.class);
+ registry = mock(HubRegistry.class);
+ lifecyleManager = mock(LifecycleManager.class);
+ servlet = new LifecycleManagerServlet() {
+
+ protected HubRegistry registry() {
+ return (HubRegistry) registry;
+ }
+
+ };
+
+ request.stubs("getParameter").with("action").will(returnValue("shutdown"));
+ registry.stubs("lifecycleManager").will(returnValue(lifecyleManager));
+ lifecyleManager.expects("shutdown");
+
+ servlet.doPost((HttpServletRequest) request, (HttpServletResponse) expectedResponse);
+ verifyMocks();
+ }
+
+ @Test
+ public void doNotShutdownHubWhenPostedActionIsNotShutdown() throws IOException, ServletException {
+ final Mock lifecyleManager;
+ final LifecycleManagerServlet servlet;
+ final Mock registry;
+ final Mock request;
+ final Mock expectedResponse;
+
+ request = mock(HttpServletRequest.class);
+ expectedResponse = mock(HttpServletResponse.class);
+ registry = mock(HubRegistry.class);
+ lifecyleManager = mock(LifecycleManager.class);
+ servlet = new LifecycleManagerServlet() {
+
+ protected HubRegistry registry() {
+ return (HubRegistry) registry;
+ }
+
+ };
+
+ request.stubs("getParameter").with("action").will(returnValue("random action"));
+ registry.stubs("lifecycleManager").will(returnValue(lifecyleManager));
+ lifecyleManager.expects("shutdown").never();
+
+ servlet.doPost((HttpServletRequest) request, (HttpServletResponse) expectedResponse);
+ verifyMocks();
+ }
+
+}
@@ -34,7 +34,7 @@ protected HubRegistry registry() {
return (HubRegistry) registry;
}
- protected void witeSuccessfulResponse(HttpServletResponse response) throws IOException {
+ protected void writeSuccessfulResponse(HttpServletResponse response) throws IOException {
assertSame(expectedResponse, response);
}
};
@@ -34,7 +34,7 @@ protected HubRegistry registry() {
return (HubRegistry) registry;
}
- protected void witeSuccessfulResponse(HttpServletResponse response) throws IOException {
+ protected void writeSuccessfulResponse(HttpServletResponse response) throws IOException {
assertSame(expectedResponse, response);
}
};
Binary file not shown.

0 comments on commit e28b4ab

Please sign in to comment.