Skip to content

Instantly share code, notes, and snippets.

@freynaud
Created February 28, 2012 11:09
Show Gist options
  • Save freynaud/1931940 to your computer and use it in GitHub Desktop.
Save freynaud/1931940 to your computer and use it in GitHub Desktop.
### Eclipse Workspace Patch 1.0
#P selenium
Index: java/server/test/org/openqa/grid/internal/mock/GridHelper.java
===================================================================
--- java/server/test/org/openqa/grid/internal/mock/GridHelper.java (revision 0)
+++ java/server/test/org/openqa/grid/internal/mock/GridHelper.java (revision 0)
@@ -0,0 +1,74 @@
+package org.openqa.grid.internal.mock;
+
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.jmock.Mockery;
+import org.openqa.grid.common.SeleniumProtocol;
+import org.openqa.grid.internal.ExternalSessionKey;
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.TestSession;
+import org.openqa.grid.web.servlet.handler.RequestType;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
+
+/**
+ * create mocked object for testing grid internal.
+ * Objects will have all the normal object characteristics,
+ * and will reserve/release the resources on the hub, but will
+ * not forward the requests to the nodes.
+ */
+public class GridHelper {
+
+ public static MockedRequestHandler createNewSessionHandler(Registry registry,
+ Map<String, Object> desiredCapability) {
+ SeleniumBasedRequest request =
+ createNewSessionRequest(registry, SeleniumProtocol.WebDriver, desiredCapability);
+ MockedRequestHandler handler = new MockedRequestHandler(request, null, registry);
+ return handler;
+ }
+
+
+ public static MockedRequestHandler createStopSessionHandler(Registry registry, TestSession session) {
+ SeleniumBasedRequest request =
+ createMockedRequest(registry, SeleniumProtocol.WebDriver, RequestType.STOP_SESSION, null);
+ MockedRequestHandler handler = new MockedRequestHandler(request, null, registry);
+ handler.setSession(session);
+ return handler;
+ }
+
+ public static SeleniumBasedRequest createMockedRequest(Registry registry,
+ SeleniumProtocol protocol, RequestType type, Map<String, Object> desiredCapability) {
+ Mockery context = new Mockery();
+ HttpServletRequest request = context.mock(HttpServletRequest.class);
+ return new SeleniumBasedRequest(request, registry, type, desiredCapability) {
+
+ @Override
+ public String getNewSessionRequestedCapability(TestSession session) {
+ return null;
+ }
+
+ @Override
+ public ExternalSessionKey extractSession() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public RequestType extractRequestType() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ @Override
+ public Map<String, Object> extractDesiredCapability() {
+ return getDesiredCapabilities();
+ }
+ };
+ }
+
+ public static SeleniumBasedRequest createNewSessionRequest(Registry registry,
+ SeleniumProtocol protocol, Map<String, Object> desiredCapability) {
+ return createMockedRequest(registry, protocol, RequestType.START_SESSION, desiredCapability);
+ }
+}
Index: java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedRequest.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedRequest.java (revision 0)
+++ java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedRequest.java (revision 0)
@@ -0,0 +1,202 @@
+package org.openqa.grid.web.servlet.handler;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+import org.apache.commons.io.IOUtils;
+import org.openqa.grid.common.SeleniumProtocol;
+import org.openqa.grid.internal.ExternalSessionKey;
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.TestSession;
+
+/**
+ * wrapper around a selenium http request that helps accessing the internal
+ * details that are selenium related ( type of protocol, new session request
+ * etc ) Also allows to change the content of the request, or read it
+ * on the hub.
+ *
+ */
+public abstract class SeleniumBasedRequest extends HttpServletRequestWrapper {
+
+ private byte[] body;
+ private final Registry registry;
+ private final RequestType type;
+ private final String encoding = "UTF-8";
+ private final Map<String, Object> desiredCapability;
+ private final long timestamp = System.currentTimeMillis();
+
+
+ public static SeleniumBasedRequest createFromRequest(HttpServletRequest request, Registry registry) {
+ if (SeleniumBasedRequest.getRequestProtocol(request) == SeleniumProtocol.Selenium) {
+ return new LegacySeleniumRequest(request, registry);
+ } else {
+ return new WebDriverRequest(request, registry);
+ }
+ }
+
+
+ /**
+ * check the request and finds out if that's a selenium legacy protocol( RC ) or a webdriver one.
+ * @param request
+ * @return
+ */
+ public static SeleniumProtocol getRequestProtocol(HttpServletRequest request) {
+ if ("/selenium-server/driver".equals(request.getServletPath())) {
+ return SeleniumProtocol.Selenium;
+ } else {
+ return SeleniumProtocol.WebDriver;
+ }
+ }
+
+ // for unit tests.
+ public SeleniumBasedRequest(HttpServletRequest request, Registry registry, RequestType type,
+ Map<String, Object> desiredCapability) {
+ super(request);
+ this.registry = registry;
+ this.type = type;
+ this.desiredCapability = desiredCapability;
+ }
+
+ public SeleniumBasedRequest(HttpServletRequest httpServletRequest, Registry registry) {
+ super(httpServletRequest);
+ try {
+ InputStream is = super.getInputStream();
+ body = IOUtils.toByteArray(is);
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ this.registry = registry;
+ type = extractRequestType();
+
+ if (type == RequestType.START_SESSION) {
+ desiredCapability = extractDesiredCapability();
+ } else {
+ desiredCapability = null;
+ }
+ }
+
+ public Registry getRegistry() {
+ return registry;
+ }
+
+ /**
+ * @return the type of the request.
+ */
+ public abstract RequestType extractRequestType();
+
+ /**
+ * Extract the session from the request. This only works for a request that has a session already
+ * assigned. It shouldn't be called for a new session request.
+ *
+ * @return the external session id sent by the remote. Null is the session cannot be found.
+ */
+ public abstract ExternalSessionKey extractSession();
+
+ /**
+ * Parse the request to extract the desiredCapabilities. For non web driver protocol ( selenium1 )
+ * some mapping will be necessary
+ *
+ * @return the desired capabilities requested by the client.
+ */
+ public abstract Map<String, Object> extractDesiredCapability();
+
+
+ // TODO freynaud remove the TestSession parameter.The listener can modify the
+ // original request instead.
+ public abstract String getNewSessionRequestedCapability(TestSession session);
+
+
+
+ public RequestType getRequestType() {
+ return type;
+ }
+
+ @Override
+ public ServletInputStream getInputStream() throws IOException {
+ return new ServletInputStreamImpl(new ByteArrayInputStream(body));
+ }
+
+ @Override
+ public BufferedReader getReader() throws IOException {
+ return new BufferedReader(new InputStreamReader(getInputStream(), encoding));
+ }
+
+ public String getBody() {
+ try {
+ Charset charset = Charset.forName(encoding);
+ CharsetDecoder decoder = charset.newDecoder();
+ CharBuffer cbuf = decoder.decode(ByteBuffer.wrap(body));
+ return new String(cbuf.toString());
+ } catch (CharacterCodingException e) {
+ e.printStackTrace();
+ }
+ return null;
+ }
+
+ public void setBody(String content) {
+ body = content.getBytes();
+ }
+
+ public long getCreationTime(){
+ return timestamp;
+ }
+
+
+ public String toString() {
+ SimpleDateFormat format = new SimpleDateFormat("d MMM yyyy HH:mm:ss");
+ StringBuilder builder = new StringBuilder();
+ builder.append("["+format.format(new Date(timestamp))+"] ");
+ builder.append(getMethod().toUpperCase() +" "+getPathInfo()+" ");
+ if (getBody() != null && !getBody().isEmpty()) {
+ builder.append(getBody());
+ }
+ return builder.toString();
+ }
+
+ public Map<String, Object> getDesiredCapabilities() {
+ return desiredCapability;
+ }
+
+ private class ServletInputStreamImpl extends ServletInputStream {
+
+ private InputStream is;
+
+ public ServletInputStreamImpl(InputStream is) {
+ this.is = is;
+ }
+
+ public int read() throws IOException {
+ return is.read();
+ }
+
+ public boolean markSupported() {
+ return false;
+ }
+
+ public synchronized void mark(int i) {
+ throw new RuntimeException("not implemented");
+ }
+
+ public synchronized void reset() throws IOException {
+ throw new RuntimeException("not implemented");
+ }
+ }
+
+
+
+}
Index: java/server/test/org/openqa/grid/internal/GridShutdownTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/GridShutdownTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/GridShutdownTest.java (working copy)
@@ -17,6 +17,10 @@
package org.openqa.grid.internal;
+
+import static org.openqa.grid.common.RegistrationRequest.APP;
+import static org.openqa.grid.common.RegistrationRequest.MAX_INSTANCES;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -24,16 +28,18 @@
import java.util.concurrent.CountDownLatch;
import junit.framework.Assert;
+
import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import static org.openqa.grid.common.RegistrationRequest.APP;
-import static org.openqa.grid.common.RegistrationRequest.MAX_INSTANCES;
-
public class GridShutdownTest {
- @Test(timeout = 500000)
+ // TODO freynaud test failing. Not part of the suite. Check if
+ // it was ok before.
+ @Test(timeout = 5000)
public void shutdown() throws InterruptedException {
final Map<String, Object> ff = new HashMap<String, Object>();
@@ -47,7 +53,7 @@
registry.add(p1);
registry.setThrowOnCapabilityNotPresent(true);
- MockedNewSessionRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, ff);
+ MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
final int before = getCurrentThreadCount();
@@ -55,24 +61,24 @@
List<Thread> threads = new ArrayList<Thread>();
for (int i = 0; i < numRequests(); i++) {
final Thread thread = new Thread(new Runnable() { // Thread safety reviewed
- public void run() {
- latch.countDown();
- MockedNewSessionRequestHandler
- newSessionRequest =
- new MockedNewSessionRequestHandler(registry, ff);
- newSessionRequest.process();
- }
- }, "TestThread" + i);
- threads.add( thread);
+ public void run() {
+ latch.countDown();
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
+ newSessionRequest.process();
+ }
+ }, "TestThread" + i);
+ threads.add(thread);
thread.start();
}
+ Thread.sleep(500);
latch.await();
Assert.assertEquals(before + 5, getCurrentThreadCount());
registry.stop();
for (Thread thread : threads) {
- thread.join();
+ thread.join();
}
Assert.assertTrue(getCurrentThreadCount() <= before);
+
}
private int getCurrentThreadCount() {
Index: java/server/src/org/openqa/grid/web/servlet/handler/Selenium1RequestHandler.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/Selenium1RequestHandler.java (revision 15989)
+++ java/server/src/org/openqa/grid/web/servlet/handler/Selenium1RequestHandler.java (working copy)
@@ -1,173 +0,0 @@
-/*
-Copyright 2011 WebDriver committers
-Copyright 2011 Software Freedom Conservancy
-
-Licensed 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.openqa.grid.web.servlet.handler;
-
-import org.openqa.grid.common.RegistrationRequest;
-import org.openqa.grid.common.SeleniumProtocol;
-import org.openqa.grid.internal.ExternalSessionKey;
-import org.openqa.grid.internal.exception.NewSessionException;
-import org.openqa.grid.internal.utils.ForwardConfiguration;
-import org.openqa.grid.internal.Registry;
-import org.openqa.grid.internal.TestSession;
-import org.openqa.grid.web.utils.BrowserNameUtils;
-
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.logging.Logger;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-/**
- * Handler processing the selenium1 based requests. Each request body has to be read to get the
- * sessionId at least.
- */
-public class Selenium1RequestHandler extends RequestHandler {
-
- private static final Logger log = Logger.getLogger(Selenium1RequestHandler.class.getName());
-
- Selenium1RequestHandler(HttpServletRequest request, HttpServletResponse response,
- Registry registry) {
- super(request, response, registry);
- if (getRequestBody() == null) {
- throw new InstantiationError(
- "Cannot create a selenium1 request handler from a request without body");
- }
- }
-
- @Override
- public RequestType extractRequestType() {
- if (getRequestBody().contains("cmd=getNewBrowserSession")) {
- return RequestType.START_SESSION;
- } else if (getRequestBody().contains("cmd=testComplete")) {
- return RequestType.STOP_SESSION;
- } else {
- return RequestType.REGULAR;
- }
- }
-
- @Override
- public ExternalSessionKey extractSession() {
- if (getRequestType() == RequestType.START_SESSION) {
- throw new IllegalAccessError("Cannot call that method of a new session request.");
- }
- // for selenium 1, the url is ignored. The session has to be read from
- // the request body.
- String command = getRequestBody();
- String[] pieces = command.split("&");
- ExternalSessionKey externalSessionKey;
- for (String piece : pieces) {
- externalSessionKey = ExternalSessionKey.fromSe1Request( piece);
- if (externalSessionKey != null){
- return externalSessionKey;
- }
- }
- return null;
-
- }
-
- @Override
- public Map<String, Object> extractDesiredCapability() {
- if (getRequestType() != RequestType.START_SESSION) {
- throw new Error("the desired capability is only present in the new session requests.");
- }
- String[] pieces = getRequestBody().split("&");
- for (String piece : pieces) {
- try {
- piece = URLDecoder.decode(piece, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- }
- if (piece.startsWith("1=")) {
- String envt = piece.replace("1=", "");
- Map<String, Object> cap = new HashMap<String, Object>();
- // TODO freynaud : more splitting, like trying to guess the
- // plateform or version ?
-
- // We don't want to process Grid 1.0 environment names because
- // they use an explicit mapping
- // to a browser launcher string.
- if (getRegistry().getConfiguration().getGrid1Mapping().containsKey(envt)) {
- cap.put(RegistrationRequest.BROWSER, envt);
- }
-
- // Otherwise, process the environment string to extract the
- // target browser and platform.
- else {
- cap.putAll(BrowserNameUtils.parseGrid2Environment(envt));
- }
-
- return cap;
- }
- }
-
- throw new RuntimeException("Error");
- }
-
- // TODO freynaud do some real parsing here instead. BrowserString to
- // Capabilities service or so.
- @Override
- public ExternalSessionKey forwardNewSessionRequestAndUpdateRegistry(TestSession session) throws NewSessionException {
- String responseBody;
-
- try {
- String body = getRequestBody();
- String[] pieces = body.split("&");
- StringBuilder builder = new StringBuilder();
-
- for (String piece : pieces) {
- if (piece.startsWith("1=")) {
- piece = URLDecoder.decode(piece, "UTF-8");
- String parts[] = piece.split("1=");
-
- // We don't want to process Grid 1.0 environment names
- // because they use an explicit mapping
- // to a browser launcher string.
- if (getRegistry().getConfiguration().getGrid1Mapping().containsKey(parts[1])) {
- piece =
- String.format(
- "1=%s",
- URLEncoder.encode(
- BrowserNameUtils.lookupGrid1Environment(parts[1], getRegistry()), "UTF-8"));
- }
-
- // Otherwise, the requested environment includes the browser
- // name before the space.
- else {
- piece =
- (String) BrowserNameUtils.parseGrid2Environment(piece).get(
- RegistrationRequest.BROWSER);
- }
- }
- builder.append(piece).append("&");
- }
-
- ForwardConfiguration config = new ForwardConfiguration();
- config.setProtocol(SeleniumProtocol.Selenium);
- config.setNewSessionRequest(true);
- config.setContentOverWrite(builder.toString());
- responseBody = session.forward(getRequest(), getResponse(), config);
- } catch (IOException e) {
- throw new NewSessionException("Error forwarding the request " + e.getMessage(),e);
- }
- return ExternalSessionKey.fromResponseBody(responseBody);
- }
-}
Index: java/server/test/org/openqa/grid/internal/mock/MockedRequestHandler.java
===================================================================
--- java/server/test/org/openqa/grid/internal/mock/MockedRequestHandler.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/mock/MockedRequestHandler.java (working copy)
@@ -1,73 +1,36 @@
-/*
-Copyright 2011 WebDriver committers
-Copyright 2011 Software Freedom Conservancy
-
-Licensed 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.openqa.grid.internal.mock;
-
-import org.openqa.grid.internal.ExternalSessionKey;
-import org.openqa.grid.internal.Registry;
-import org.openqa.grid.internal.TestSession;
-import org.openqa.grid.web.servlet.handler.RequestHandler;
-import org.openqa.grid.web.servlet.handler.RequestType;
-import org.openqa.grid.web.servlet.handler.WebDriverRequestHandler;
-
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-public class MockedRequestHandler extends WebDriverRequestHandler {
-
- public MockedRequestHandler(Registry registry) {
- super(null, null, registry);
- }
-
- public MockedRequestHandler(HttpServletRequest request, HttpServletResponse response,
- Registry registry) {
- super(request, response, registry);
- }
-
- @Override
- public ExternalSessionKey forwardNewSessionRequestAndUpdateRegistry(TestSession session) {
- // System.out.println("forwarding to " + session.getInternalKey());
- return ExternalSessionKey.fromString("");
- }
-
- @Override
- protected void forwardRequest(TestSession session, RequestHandler handler)
- throws java.io.IOException {
- // System.out.println("forwarding request to "+session);
- }
-
- @Override
- public void setSession(TestSession session) {
- super.setSession(session);
- }
-
- public TestSession getTestSession() {
- return super.getSession();
- }
-
- @Override
- public void setDesiredCapabilities(Map<String, Object> desiredCapabilities) {
- super.setDesiredCapabilities(desiredCapabilities);
- }
-
- @Override
- public void setRequestType(RequestType requestType) {
- super.setRequestType(requestType);
- }
-}
+package org.openqa.grid.internal.mock;
+
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.TestSession;
+import org.openqa.grid.internal.exception.NewSessionException;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
+
+public class MockedRequestHandler extends RequestHandler {
+
+
+ public MockedRequestHandler(SeleniumBasedRequest request, HttpServletResponse response,
+ Registry registry) {
+ super(request, response, registry);
+ }
+
+ public void setSession(TestSession session){
+ super.setSession(session);
+ }
+
+ @Override
+ protected void forwardRequest(TestSession session, RequestHandler handler) throws IOException {
+ // do nothing
+ }
+
+ @Override
+ public void forwardNewSessionRequestAndUpdateRegistry(TestSession session)
+ throws NewSessionException {
+ // do nothing
+ }
+
+}
Index: java/server/test/org/openqa/grid/internal/ConcurrencyLockTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/ConcurrencyLockTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/ConcurrencyLockTest.java (working copy)
@@ -1,134 +1,133 @@
-/*
-Copyright 2011 WebDriver committers
-Copyright 2011 Software Freedom Conservancy
-
-Licensed 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.openqa.grid.internal;
-
-import static org.openqa.grid.common.RegistrationRequest.APP;
-
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
-import org.openqa.grid.web.servlet.handler.RequestType;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Check that 1 type of request doesn't block other requests.
- * <p/>
- * For a hub capable of handling 1 FF and 1 IE for instance, if the hub already built a queue of FF
- * requests and a IE request is received it should be processed right away and not blocked by the FF
- * queue.
- */
-
-public class ConcurrencyLockTest {
-
- private static Registry registry;
-
- private static Map<String, Object> ie = new HashMap<String, Object>();
- private static Map<String, Object> ff = new HashMap<String, Object>();
-
- /**
- * create a hub with 1 IE and 1 FF
- */
- @BeforeClass
- public static void setup() {
- registry = Registry.newInstance();
- ie.put(APP, "IE");
- ff.put(APP, "FF");
-
- RemoteProxy p1 = RemoteProxyFactory.getNewBasicRemoteProxy(ie, "http://machine1:4444", registry);
- RemoteProxy p2 = RemoteProxyFactory.getNewBasicRemoteProxy(ff, "http://machine2:4444", registry);
- registry.add(p1);
- registry.add(p2);
-
- }
-
-
- private List<String> results = Collections.synchronizedList(new ArrayList<String>());
-
- @Test(timeout = 10000)
- public void runTest() throws InterruptedException {
- List<Map<String, Object>> caps = new ArrayList<Map<String, Object>>();
- caps.add(ff);
- caps.add(ff);
- caps.add(ff);
- caps.add(ie);
-
- List<Thread> threads = new ArrayList<Thread>();
- for (final Map<String, Object> cap : caps) {
- Thread t = new Thread(new Runnable() { // Thread safety reviewed
- public void run() {
- try {
- runTests2(cap);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- });
- t.start();
- threads.add(t);
- }
-
- for (Thread t : threads) {
- t.join();
- }
- Assert.assertEquals(4, results.size());
- Assert.assertEquals("IE", results.get(0));
- Assert.assertEquals("FF", results.get(1));
- Assert.assertEquals("FF", results.get(2));
- Assert.assertEquals("FF", results.get(3));
- }
-
- private void runTests2(Map<String, Object> cap) throws InterruptedException {
-
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(cap);
-
- if (cap.get(APP).equals("FF")) {
- // start the FF right away
- newSessionRequest.process();
- TestSession s = newSessionRequest.getTestSession();
- Thread.sleep(2000);
- results.add("FF");
- registry.terminateSynchronousFOR_TEST_ONLY(s);
- } else {
- // wait for 1 sec before starting IE to make sure the FF proxy is
- // busy with the 3 FF requests.
- Thread.sleep(1000);
- newSessionRequest.process();
- results.add("IE");
- }
- // at that point, the hub has recieved first 3 FF requests that are
- // queued and 1 IE request 1sec later, after the FF are already blocked
- // in the queue.The blocked FF request shouldn't block IE from starting,
- // so IE should be done first.
- }
-
-
- @AfterClass
- public static void teardown() {
- registry.stop();
- }
-
-}
+/*
+Copyright 2011 WebDriver committers
+Copyright 2011 Software Freedom Conservancy
+
+Licensed 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.openqa.grid.internal;
+
+import static org.openqa.grid.common.RegistrationRequest.APP;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestType;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Check that 1 type of request doesn't block other requests.
+ * <p/>
+ * For a hub capable of handling 1 FF and 1 IE for instance, if the hub already built a queue of FF
+ * requests and a IE request is received it should be processed right away and not blocked by the FF
+ * queue.
+ */
+
+public class ConcurrencyLockTest {
+
+ private static Registry registry;
+
+ private static Map<String, Object> ie = new HashMap<String, Object>();
+ private static Map<String, Object> ff = new HashMap<String, Object>();
+
+ /**
+ * create a hub with 1 IE and 1 FF
+ */
+ @BeforeClass
+ public static void setup() {
+ registry = Registry.newInstance();
+ ie.put(APP, "IE");
+ ff.put(APP, "FF");
+
+ RemoteProxy p1 = RemoteProxyFactory.getNewBasicRemoteProxy(ie, "http://machine1:4444", registry);
+ RemoteProxy p2 = RemoteProxyFactory.getNewBasicRemoteProxy(ff, "http://machine2:4444", registry);
+ registry.add(p1);
+ registry.add(p2);
+
+ }
+
+
+ private List<String> results = Collections.synchronizedList(new ArrayList<String>());
+
+ @Test(timeout = 10000)
+ public void runTest() throws InterruptedException {
+ List<Map<String, Object>> caps = new ArrayList<Map<String, Object>>();
+ caps.add(ff);
+ caps.add(ff);
+ caps.add(ff);
+ caps.add(ie);
+
+ List<Thread> threads = new ArrayList<Thread>();
+ for (final Map<String, Object> cap : caps) {
+ Thread t = new Thread(new Runnable() { // Thread safety reviewed
+ public void run() {
+ try {
+ runTests2(cap);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ });
+ t.start();
+ threads.add(t);
+ }
+
+ for (Thread t : threads) {
+ t.join();
+ }
+ Assert.assertEquals(4, results.size());
+ Assert.assertEquals("IE", results.get(0));
+ Assert.assertEquals("FF", results.get(1));
+ Assert.assertEquals("FF", results.get(2));
+ Assert.assertEquals("FF", results.get(3));
+ }
+
+ private void runTests2(Map<String, Object> cap) throws InterruptedException {
+
+ MockedRequestHandler newSessionHandler =GridHelper.createNewSessionHandler(registry, cap);
+
+ if (cap.get(APP).equals("FF")) {
+ // start the FF right away
+ newSessionHandler.process();
+ TestSession s = newSessionHandler.getSession();
+ Thread.sleep(2000);
+ results.add("FF");
+ registry.terminateSynchronousFOR_TEST_ONLY(s);
+ } else {
+ // wait for 1 sec before starting IE to make sure the FF proxy is
+ // busy with the 3 FF requests.
+ Thread.sleep(1000);
+ newSessionHandler.process();
+ results.add("IE");
+ }
+ // at that point, the hub has recieved first 3 FF requests that are
+ // queued and 1 IE request 1sec later, after the FF are already blocked
+ // in the queue.The blocked FF request shouldn't block IE from starting,
+ // so IE should be done first.
+ }
+
+
+ @AfterClass
+ public static void teardown() {
+ registry.stop();
+ }
+
+}
Index: java/server/test/org/openqa/grid/internal/PriorityTestLoad.java
===================================================================
--- java/server/test/org/openqa/grid/internal/PriorityTestLoad.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/PriorityTestLoad.java (working copy)
@@ -19,21 +19,22 @@
import static org.openqa.grid.common.RegistrationRequest.APP;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.internal.listeners.Prioritizer;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.grid.web.servlet.handler.RequestType;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
/**
* Grid with only 1 node. Sending MAX thread in it to load the queue and keep it ordered.
*/
@@ -54,7 +55,7 @@
static Map<String, Object> ff = new HashMap<String, Object>();
static RemoteProxy p1;
- static List<MockedRequestHandler> requests = new ArrayList<MockedRequestHandler>();
+ static List<RequestHandler> requests = new ArrayList<RequestHandler>();
/**
* create a hub with 1 FF
@@ -73,20 +74,18 @@
Map<String, Object> cap = new HashMap<String, Object>();
cap.put(APP, "FF");
cap.put("_priority", i);
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, cap);
+ RequestHandler req = GridHelper.createNewSessionHandler(registry, cap);
requests.add(req);
}
// use all the proxies
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(ff);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
// and keep adding request in the queue.
- for (MockedRequestHandler h : requests) {
- final MockedRequestHandler req = h;
+ for (RequestHandler h : requests) {
+ final RequestHandler req = h;
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
req.process();
@@ -117,9 +116,9 @@
while (!reqDone) {
Thread.sleep(20);
}
- Assert.assertNotNull(requests.get(requests.size() - 1).getTestSession());
+ Assert.assertNotNull(requests.get(requests.size() - 1).getSession());
Assert.assertEquals(
- requests.get(requests.size() - 1).getDesiredCapabilities().get("_priority"), MAX);
+ requests.get(requests.size() - 1).getRequest().getDesiredCapabilities().get("_priority"), MAX);
}
@AfterClass
Index: java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequestHandler.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequestHandler.java (revision 15989)
+++ java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequestHandler.java (working copy)
@@ -1,139 +0,0 @@
-/*
-Copyright 2011 WebDriver committers
-Copyright 2011 Software Freedom Conservancy
-
-Licensed 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.openqa.grid.web.servlet.handler;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.logging.Logger;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.openqa.grid.common.SeleniumProtocol;
-import org.openqa.grid.common.exception.GridException;
-import org.openqa.grid.internal.ExternalSessionKey;
-import org.openqa.grid.internal.Registry;
-import org.openqa.grid.internal.TestSession;
-import org.openqa.grid.internal.exception.NewSessionException;
-import org.openqa.grid.internal.utils.ForwardConfiguration;
-import org.seleniumhq.jetty7.server.Response;
-
-/**
- * Handles an individual request, scope is a single request and hence a single thread.
- */
-public class WebDriverRequestHandler extends RequestHandler {
-
- private static final Logger log = Logger.getLogger(WebDriverRequestHandler.class.getName());
-
- protected WebDriverRequestHandler(HttpServletRequest request, HttpServletResponse response,
- Registry registry) {
- super(request, response, registry);
- }
-
-
- @Override
- public RequestType extractRequestType() {
-
- if ("/session".equals(getRequest().getPathInfo())) {
- return RequestType.START_SESSION;
- } else if (getRequest().getMethod().equalsIgnoreCase("DELETE")) {
- ExternalSessionKey
- externalKey = ExternalSessionKey.fromWebDriverRequest(getRequest().getPathInfo());
- if (getRequest().getPathInfo().endsWith("/session/" + externalKey.getKey())) {
- return RequestType.STOP_SESSION;
- }
- }
- return RequestType.REGULAR;
- }
-
- @Override
- public ExternalSessionKey extractSession() {
- if (getRequestType() == RequestType.START_SESSION) {
- throw new IllegalAccessError("Cannot call that method of a new session request.");
- }
- String path = getRequest().getPathInfo();
- return ExternalSessionKey.fromWebDriverRequest(path);
- }
-
- // TODO freynaud parsing is so so.
- @SuppressWarnings("unchecked")
- // JSON iterator.
- @Override
- public Map<String, Object> extractDesiredCapability() {
- String json = getRequestBody();
- Map<String, Object> desiredCapability = new HashMap<String, Object>();
- try {
- JSONObject map = new JSONObject(json);
- JSONObject dc = map.getJSONObject("desiredCapabilities");
- for (Iterator iterator = dc.keys(); iterator.hasNext();) {
- String key = (String) iterator.next();
- Object value = dc.get(key);
- if (value == JSONObject.NULL) {
- value = null;
- }
- desiredCapability.put(key, value);
- }
- } catch (JSONException e) {
- throw new GridException("Cannot extract a capabilities from the request " + json);
- }
- return desiredCapability;
- }
-
- @Override
- public ExternalSessionKey forwardNewSessionRequestAndUpdateRegistry(TestSession session) throws NewSessionException{
- try {
- // here, don't forward the requestBody directly, but read the
- // desiredCapabilities from the session instead.
- // That allow the TestSessionListener.before modification of the
- // capability map to be propagated.
- JSONObject c = new JSONObject();
- c.put("desiredCapabilities", session.getRequestedCapabilities());
- String content = c.toString();
- ForwardConfiguration config = new ForwardConfiguration();
- config.setProtocol(SeleniumProtocol.WebDriver);
- config.setNewSessionRequest(true);
- config.setContentOverWrite(content);
- session.forward(getRequest(), getResponse(), config);
- } catch (IOException e) {
- //log.warning("Error forwarding the request " + e.getMessage());
- throw new NewSessionException("Error forwarding the request " + e.getMessage(),e);
- } catch (JSONException e) {
- //log.warning("Error with the request " + e.getMessage());
- throw new NewSessionException("Error with the request " + e.getMessage(),e);
- }
-
- if (getResponse().containsHeader("Location")) {
- String location =
- ((Response) getResponse()).getHeader( "Location" );
- ExternalSessionKey res = ExternalSessionKey.fromWebDriverRequest( location);
- if ( res!=null){
- return res;
- }else {
- throw new NewSessionException("couldn't extract the new session from the response header.");
- }
- } else {
- //log.warning("Error, header should contain Location");
- throw new NewSessionException("Error, header should contain Location ");
- }
-
- }
-}
Index: java/server/src/org/openqa/grid/web/servlet/handler/LegacySeleniumRequest.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/LegacySeleniumRequest.java (revision 0)
+++ java/server/src/org/openqa/grid/web/servlet/handler/LegacySeleniumRequest.java (revision 0)
@@ -0,0 +1,134 @@
+package org.openqa.grid.web.servlet.handler;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.openqa.grid.common.RegistrationRequest;
+import org.openqa.grid.common.SeleniumProtocol;
+import org.openqa.grid.internal.ExternalSessionKey;
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.TestSession;
+import org.openqa.grid.internal.exception.NewSessionException;
+import org.openqa.grid.web.utils.BrowserNameUtils;
+
+public class LegacySeleniumRequest extends SeleniumBasedRequest {
+
+ public LegacySeleniumRequest(HttpServletRequest httpServletRequest, Registry registry) {
+ super(httpServletRequest, registry);
+
+ }
+
+ @Override
+ public RequestType extractRequestType() {
+ if (getBody().contains("cmd=getNewBrowserSession")) {
+ return RequestType.START_SESSION;
+ } else if (getBody().contains("cmd=testComplete")) {
+ return RequestType.STOP_SESSION;
+ } else {
+ return RequestType.REGULAR;
+ }
+
+ }
+
+ @Override
+ public ExternalSessionKey extractSession() {
+ if (getRequestType() == RequestType.START_SESSION) {
+ throw new IllegalAccessError("Cannot call that method of a new session request.");
+ }
+ // for selenium 1, the url is ignored. The session has to be read from
+ // the request body.
+ String command = getBody();
+ String[] pieces = command.split("&");
+ ExternalSessionKey externalSessionKey;
+ for (String piece : pieces) {
+ externalSessionKey = ExternalSessionKey.fromSe1Request(piece);
+ if (externalSessionKey != null) {
+ return externalSessionKey;
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Map<String, Object> extractDesiredCapability() {
+ if (getRequestType() != RequestType.START_SESSION) {
+ throw new Error("the desired capability is only present in the new session requests.");
+ }
+ String[] pieces = getBody().split("&");
+ for (String piece : pieces) {
+ try {
+ piece = URLDecoder.decode(piece, "UTF-8");
+ } catch (UnsupportedEncodingException e) {}
+ if (piece.startsWith("1=")) {
+ String envt = piece.replace("1=", "");
+ Map<String, Object> cap = new HashMap<String, Object>();
+ // TODO freynaud : more splitting, like trying to guess the
+ // plateform or version ?
+
+ // We don't want to process Grid 1.0 environment names because
+ // they use an explicit mapping
+ // to a browser launcher string.
+ if (getRegistry().getConfiguration().getGrid1Mapping().containsKey(envt)) {
+ cap.put(RegistrationRequest.BROWSER, envt);
+ }
+
+ // Otherwise, process the environment string to extract the
+ // target browser and platform.
+ else {
+ cap.putAll(BrowserNameUtils.parseGrid2Environment(envt));
+ }
+
+ return cap;
+ }
+ }
+
+ throw new RuntimeException("Error");
+ }
+
+ @Override
+ public String getNewSessionRequestedCapability(TestSession session) {
+ try {
+ String body = getBody();
+ String[] pieces = body.split("&");
+ StringBuilder builder = new StringBuilder();
+
+ for (String piece : pieces) {
+ if (piece.startsWith("1=")) {
+ piece = URLDecoder.decode(piece, "UTF-8");
+ String parts[] = piece.split("1=");
+
+ // We don't want to process Grid 1.0 environment names
+ // because they use an explicit mapping
+ // to a browser launcher string.
+ if (getRegistry().getConfiguration().getGrid1Mapping().containsKey(parts[1])) {
+ piece =
+ String.format(
+ "1=%s",
+ URLEncoder.encode(
+ BrowserNameUtils.lookupGrid1Environment(parts[1], getRegistry()), "UTF-8"));
+ }
+
+ // Otherwise, the requested environment includes the browser
+ // name before the space.
+ else {
+ piece =
+ (String) BrowserNameUtils.parseGrid2Environment(piece).get(
+ RegistrationRequest.BROWSER);
+ }
+ }
+ builder.append(piece).append("&");
+ }
+ return builder.toString();
+ } catch (UnsupportedEncodingException ignore) {
+
+ }
+ throw new NewSessionException("Error with the request ");
+
+ }
+
+}
Index: java/server/test/org/openqa/grid/internal/NewRequestCrashesDuringNewSessionTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/NewRequestCrashesDuringNewSessionTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/NewRequestCrashesDuringNewSessionTest.java (working copy)
@@ -17,18 +17,22 @@
package org.openqa.grid.internal;
+import static org.openqa.grid.common.RegistrationRequest.APP;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletResponse;
+
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.common.SeleniumProtocol;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.openqa.grid.common.RegistrationRequest.APP;
-
public class NewRequestCrashesDuringNewSessionTest {
private static Registry registry;
@@ -54,9 +58,9 @@
@Test(timeout = 1000)
public void basic() {
// should work
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, ff);
- newSessionRequest.process();
- TestSession s = newSessionRequest.getTestSession();
+ MockedRequestHandler newSessionRequest =GridHelper.createNewSessionHandler(registry, ff);
+ newSessionRequest.process();
+ TestSession s = newSessionRequest.getSession();
Assert.assertNotNull(s);
registry.terminate(s, SessionTerminationReason.CLIENT_STOPPED_SESSION);
Assert.assertEquals(0, registry.getNewSessionRequestCount());
@@ -70,8 +74,9 @@
public void requestIsremovedFromTheQeueAfterItcrashes() throws InterruptedException {
// should work
try {
+ SeleniumBasedRequest newSession = GridHelper.createNewSessionRequest(registry, SeleniumProtocol.WebDriver, ff);
MockedRequestHandler newSessionRequest =
- new MockedBuggyNewSessionRequestHandler(registry, ff);
+ new MockedBuggyNewSessionRequestHandler(newSession,null,registry);
newSessionRequest.process();
} catch (RuntimeException e) {
System.out.println(e.getMessage());
@@ -85,17 +90,19 @@
registry.stop();
}
- class MockedBuggyNewSessionRequestHandler extends MockedNewSessionRequestHandler {
+ class MockedBuggyNewSessionRequestHandler extends MockedRequestHandler {
- public MockedBuggyNewSessionRequestHandler(Registry registry,
- Map<String, Object> desiredCapabilities) {
- super(registry, desiredCapabilities);
+
+ public MockedBuggyNewSessionRequestHandler(SeleniumBasedRequest request,
+ HttpServletResponse response, Registry registry) {
+ super(request, response, registry);
}
+
@Override
- public ExternalSessionKey forwardNewSessionRequestAndUpdateRegistry(TestSession session) {
+ public void forwardNewSessionRequestAndUpdateRegistry(TestSession session) {
throw new RuntimeException("something horrible happened.");
}
-
+
}
}
Index: java/server/test/org/openqa/grid/internal/SessionTimesOutTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/SessionTimesOutTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/SessionTimesOutTest.java (working copy)
@@ -17,23 +17,24 @@
package org.openqa.grid.internal;
+import static org.openqa.grid.common.RegistrationRequest.APP;
+import static org.openqa.grid.common.RegistrationRequest.CLEAN_UP_CYCLE;
+import static org.openqa.grid.common.RegistrationRequest.ID;
+import static org.openqa.grid.common.RegistrationRequest.TIME_OUT;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.internal.listeners.TimeoutListener;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.openqa.grid.common.RegistrationRequest.APP;
-import static org.openqa.grid.common.RegistrationRequest.CLEAN_UP_CYCLE;
-import static org.openqa.grid.common.RegistrationRequest.ID;
-import static org.openqa.grid.common.RegistrationRequest.TIME_OUT;
-
public class SessionTimesOutTest {
private static RegistrationRequest req = new RegistrationRequest();
@@ -80,16 +81,16 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
// wait for a timeout
Thread.sleep(500);
- MockedRequestHandler newSessionRequest2 = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest2 = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest2.process();
- TestSession session2 = newSessionRequest2.getTestSession();
+ TestSession session2 = newSessionRequest2.getSession();
Assert.assertNotNull(session2);
Assert.assertNotSame(session, session2);
} finally {
@@ -125,9 +126,9 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
// timeout cleanup will start
Thread.sleep(500);
// but the session finishes before the timeout cleanup finishes
@@ -143,9 +144,9 @@
}
Assert.assertTrue(timeoutDone);
- MockedRequestHandler newSessionRequest2 = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest2 = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest2.process();
- TestSession session2 = newSessionRequest2.getTestSession();
+ TestSession session2 = newSessionRequest2.getSession();
Assert.assertNotNull(session2);
Assert.assertTrue(session.equals(session));
Assert.assertFalse(session2.equals(session));
@@ -167,7 +168,7 @@
}
// a proxy throwing an exception will end up not releasing the resources.
- @Test(timeout = 1000, expected = IllegalAccessError.class)
+ @Test(timeout = 1000)
public void testTimeoutBug() throws InterruptedException {
final Registry registry = Registry.newInstance();
RemoteProxy p1 = new MyBuggyRemoteProxyTimeout(req, registry);
@@ -176,11 +177,11 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- final MockedRequestHandler newSessionRequest2 =
- new MockedNewSessionRequestHandler(registry, app1);
+ final RequestHandler newSessionRequest2 =
+ GridHelper.createNewSessionHandler(registry, app1);
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
// the request should never be processed because the
@@ -191,9 +192,8 @@
// wait for a timeout
Thread.sleep(500);
- // should throw illegal access. getTestSession cannot be called
- // because the request has not been processed yet.
- newSessionRequest2.getTestSession();
+ // the request has not been processed yet.
+ Assert.assertNull(newSessionRequest2.getServerSession());
} finally {
registry.stop();
}
@@ -242,9 +242,9 @@
final MyStupidConfig proxy = new MyStupidConfig(req, registry);
proxy.setupTimeoutListener();
registry.add(proxy);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
// wait -> timed out and released.
Thread.sleep(500);
boolean shouldTimeout = timeout > 0 && cycle > 0;
Index: java/server/src/org/openqa/grid/web/servlet/handler/RequestHandler.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/RequestHandler.java (revision 15989)
+++ java/server/src/org/openqa/grid/web/servlet/handler/RequestHandler.java (working copy)
@@ -17,23 +17,7 @@
package org.openqa.grid.web.servlet.handler;
-import org.openqa.grid.common.exception.ClientGoneException;
-import org.openqa.grid.common.exception.GridException;
-import org.openqa.grid.internal.ExternalSessionKey;
-import org.openqa.grid.internal.Registry;
-import org.openqa.grid.internal.RemoteProxy;
-import org.openqa.grid.internal.SessionTerminationReason;
-import org.openqa.grid.internal.TestSession;
-import org.openqa.grid.internal.exception.NewSessionException;
-import org.openqa.grid.internal.listeners.Prioritizer;
-import org.openqa.grid.internal.listeners.TestSessionListener;
-import org.openqa.grid.internal.utils.ForwardConfiguration;
-
-import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -44,25 +28,31 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.openqa.grid.common.exception.ClientGoneException;
+import org.openqa.grid.common.exception.GridException;
+import org.openqa.grid.internal.ExternalSessionKey;
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.RemoteProxy;
+import org.openqa.grid.internal.SessionTerminationReason;
+import org.openqa.grid.internal.TestSession;
+import org.openqa.grid.internal.exception.NewSessionException;
+import org.openqa.grid.internal.listeners.Prioritizer;
+import org.openqa.grid.internal.listeners.TestSessionListener;
+
/**
- * Base stuff to handle the request coming from a remote. Ideally, there should be only 1 concrete
- * class, but to support both legacy selenium1 and web driver, 2 classes are needed. <p/> {@link
- * Selenium1RequestHandler} for the part specific to selenium1 protocol {@link
- * WebDriverRequestHandler} for the part specific to webdriver protocol
+ * Base stuff to handle the request coming from a remote.
*
* Threading notes; RequestHandlers are instantiated per-request, run on the servlet container
* thread. The instance is also accessed by the matcher thread.
*/
@SuppressWarnings("JavaDoc")
-public abstract class RequestHandler implements Comparable<RequestHandler> {
+public class RequestHandler implements Comparable<RequestHandler> {
private final Registry registry;
- private final HttpServletRequest request;
+ private final SeleniumBasedRequest request;
private final HttpServletResponse response;
- private String body = null;
- private boolean bodyHasBeenRead = false;
private volatile Map<String, Object> desiredCapabilities = null;
private RequestType requestType = null;
private volatile TestSession session = null;
@@ -73,79 +63,52 @@
private static final Logger log = Logger.getLogger(RequestHandler.class.getName());
private final Thread waitingThread;
- /**
- * Detect what kind of protocol ( selenium1 vs webdriver ) is used by the request and create the
- * associated handler.
- */
- public static RequestHandler createHandler(HttpServletRequest request,
- HttpServletResponse response, Registry registry) {
- if (isSeleniumProtocol(request)) {
- return new Selenium1RequestHandler(request, response, registry);
- } else {
- return new WebDriverRequestHandler(request, response, registry);
- }
- }
+
+
- protected RequestHandler(HttpServletRequest request, HttpServletResponse response,
- Registry registry) {
+ public RequestHandler(SeleniumBasedRequest request, HttpServletResponse response,
+ Registry registry) {
this.request = request;
this.response = response;
this.registry = registry;
this.waitingThread = Thread.currentThread();
}
- /**
- * @return the type of the request.
- */
- public abstract RequestType extractRequestType();
- /**
- * Extract the session from the request. This only works for a request that has a session already
- * assigned. It shouldn't be called for a new session request.
- *
- * @return the external session id sent by the remote. Null is the session cannot be found.
- */
- public abstract ExternalSessionKey extractSession();
/**
- * Parse the request to extract the desiredCapabilities. For non web driver protocol ( selenium1 )
- * some mapping will be necessary
- *
- * @return the desired capabilities requested by the client.
- */
- public abstract Map<String, Object> extractDesiredCapability();
-
- /**
* Forward the new session request to the TestSession that has been assigned, and parse the
* response to extract and return the external key assigned by the remote.
*
- * @return the external key sent by the remote.
* @throws NewSessionException in case anything wrong happens during the new session process.
*/
- public abstract ExternalSessionKey forwardNewSessionRequestAndUpdateRegistry(TestSession session)
- throws NewSessionException;
+ public void forwardNewSessionRequestAndUpdateRegistry(TestSession session)
+ throws NewSessionException {
+ try {
+ String content = request.getNewSessionRequestedCapability(session);
+ getRequest().setBody(content);
+ session.forward(getRequest(), getResponse());
+ } catch (IOException e) {
+ //log.warning("Error forwarding the request " + e.getMessage());
+ throw new NewSessionException("Error forwarding the request " + e.getMessage(), e);
+ }
+ }
protected void forwardRequest(TestSession session, RequestHandler handler) throws IOException {
- if (bodyHasBeenRead) {
- ForwardConfiguration config = new ForwardConfiguration();
- config.setContentOverWrite(getRequestBody());
- session.forward(request, response, config);
- } else {
- session.forward(request, response);
- }
+ session.forward(request, response);
}
/**
* forwards the request to the remote, allocating / releasing the resources if necessary.
*/
public void process() {
- switch (getRequestType()) {
+ switch (request.getRequestType()) {
case START_SESSION:
try {
registry.addNewSessionRequest(this);
waitForSessionBound();
beforeSessionEvent();
- forwardNewSessionRequestAndUpdateRegistry(session);
+ forwardNewSessionRequestAndUpdateRegistry(session);
} catch (Exception e) {
cleanup();
throw new GridException("Error forwarding the new session " + e.getMessage(), e);
@@ -157,11 +120,10 @@
if (session == null) {
ExternalSessionKey sessionKey = null;
try {
- sessionKey = extractSession();
- } catch (RuntimeException ignore) {
- }
+ sessionKey = request.extractSession();
+ } catch (RuntimeException ignore) {}
throw new GridException("Session [" + sessionKey + "] not available - "
- + registry.getActiveSessions());
+ + registry.getActiveSessions());
}
try {
forwardRequest(session, this);
@@ -174,13 +136,12 @@
throw new GridException("cannot forward the request " + t.getMessage(), t);
}
- if (getRequestType() == RequestType.STOP_SESSION) {
+ if (request.getRequestType() == RequestType.STOP_SESSION) {
registry.terminate(session, SessionTerminationReason.CLIENT_STOPPED_SESSION);
}
break;
default:
throw new RuntimeException("NI");
-
}
}
@@ -192,7 +153,7 @@
}
}
-
+
/**
* calls the TestSessionListener is the proxy for that node has one specified.
*
@@ -232,47 +193,9 @@
}
/**
- * return true is the request is using the selenium1 protocol, false if that's a web driver
- * protocol.
+ * the SeleniumBasedRequest this hanlder is processing.
*/
- private static boolean isSeleniumProtocol(HttpServletRequest request) {
- return "/selenium-server/driver".equals(request.getServletPath());
- }
-
- /**
- * reads the input stream of the request and returns its content.
- */
- protected String getRequestBody() {
- if (!bodyHasBeenRead) {
- bodyHasBeenRead = true;
- StringBuilder sb = new StringBuilder();
- String line;
- try {
- InputStream is = request.getInputStream();
- if (is == null) {
- return null;
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
- while ((line = reader.readLine()) != null) {
- // TODO freynaud bug ?
- sb.append(line);/* .append("\n"); */
-
- }
- is.close();
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- body = sb.toString();
- }
- return body;
- }
-
- /**
- * the HttpServletRequest this hanlder is processing.
- */
- public HttpServletRequest getRequest() {
+ public SeleniumBasedRequest getRequest() {
return request;
}
@@ -283,36 +206,19 @@
return response;
}
- public Map<String, Object> getDesiredCapabilities() {
- if (desiredCapabilities == null) {
- desiredCapabilities = extractDesiredCapability();
- }
- return desiredCapabilities;
- }
- protected void setDesiredCapabilities(Map<String, Object> desiredCapabilities) {
- this.desiredCapabilities = desiredCapabilities;
- }
public int compareTo(RequestHandler o) {
Prioritizer prioritizer = registry.getPrioritizer();
if (prioritizer != null) {
- return prioritizer.compareTo(this.getDesiredCapabilities(), o.getDesiredCapabilities());
+ return prioritizer.compareTo(this.getRequest().getDesiredCapabilities(), o.getRequest()
+ .getDesiredCapabilities());
} else {
return 0;
}
}
- protected RequestType getRequestType() {
- if (requestType == null) {
- requestType = extractRequestType();
- }
- return requestType;
- }
- protected void setRequestType(RequestType requestType) {
- this.requestType = requestType;
- }
protected void setSession(TestSession session) {
this.session = session;
@@ -323,9 +229,9 @@
sessionAssigned.countDown();
}
- protected TestSession getSession() {
+ public TestSession getSession() {
if (session == null) {
- ExternalSessionKey externalKey = extractSession();
+ ExternalSessionKey externalKey = request.extractSession();
session = registry.getExistingSession(externalKey);
}
return session;
@@ -352,7 +258,6 @@
public String toString() {
StringBuilder b = new StringBuilder();
b.append("session :").append(session).append(" , ");
- b.append("cap : ").append(getDesiredCapabilities());
b.append("\n");
return b.toString();
}
@@ -362,7 +267,7 @@
b.append("\nmethod: ").append(request.getMethod());
b.append("\npathInfo: ").append(request.getPathInfo());
b.append("\nuri: ").append(request.getRequestURI());
- b.append("\ncontent :").append(getRequestBody());
+ b.append("\ncontent :").append(request.getBody());
return b.toString();
}
Index: java/server/test/org/openqa/grid/internal/mock/MockedNewSessionRequestHandler.java
===================================================================
--- java/server/test/org/openqa/grid/internal/mock/MockedNewSessionRequestHandler.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/mock/MockedNewSessionRequestHandler.java (working copy)
@@ -1,16 +0,0 @@
-package org.openqa.grid.internal.mock;
-
-import org.openqa.grid.internal.Registry;
-import org.openqa.grid.web.servlet.handler.RequestType;
-
-import java.util.Map;
-
-public class MockedNewSessionRequestHandler extends MockedRequestHandler {
-
- public MockedNewSessionRequestHandler(Registry registry, Map<String, Object> desiredCapabilities) {
- super(registry);
- setRequestType(RequestType.START_SESSION);
- setDesiredCapabilities(desiredCapabilities);
- }
-
-}
Index: java/server/test/org/openqa/grid/internal/StatusServletTests.java
===================================================================
--- java/server/test/org/openqa/grid/internal/StatusServletTests.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/StatusServletTests.java (working copy)
@@ -17,6 +17,14 @@
package org.openqa.grid.internal;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+
import junit.framework.Assert;
import org.apache.http.HttpHost;
@@ -31,22 +39,14 @@
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.common.RegistrationRequest;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.utils.GridHubConfiguration;
import org.openqa.grid.web.Hub;
-import org.openqa.grid.web.servlet.handler.RequestType;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.selenium.net.PortProber;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.internal.HttpClientFactory;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.URL;
-import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
-
public class StatusServletTests {
private static Hub hub;
@@ -100,11 +100,9 @@
Map<String, Object> cap = new HashMap<String, Object>();
cap.put(CapabilityType.BROWSER_NAME, "app1");
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(cap);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, cap);
newSessionRequest.process();
- session = newSessionRequest.getTestSession();
+ session = newSessionRequest.getSession();
session.setExternalKey(ExternalSessionKey.fromString("ext. key"));
}
Index: java/server/test/org/openqa/grid/internal/AddingProxyAgainFreesResources.java
===================================================================
--- java/server/test/org/openqa/grid/internal/AddingProxyAgainFreesResources.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/AddingProxyAgainFreesResources.java (working copy)
@@ -19,17 +19,16 @@
import static org.openqa.grid.common.RegistrationRequest.APP;
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
-import org.openqa.grid.web.servlet.handler.RequestType;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-
/**
* registering an already existing node assumes the node has been restarted, and all the resources
* are free again
@@ -42,7 +41,8 @@
private static Map<String, Object> ff = new HashMap<String, Object>();
private static RemoteProxy p1;
- private static MockedRequestHandler request;
+ private static RequestHandler handler;
+ private static RequestHandler handler2;
private static TestSession session = null;
/**
@@ -57,29 +57,28 @@
p1 = RemoteProxyFactory.getNewBasicRemoteProxy(ff, "http://machine1:4444", registry);
registry.add(p1);
- Map<String, Object> cap = new HashMap<String, Object>();
- cap.put(APP, "FF");
- request = new MockedNewSessionRequestHandler(registry, cap);
+
+
+ handler = GridHelper.createNewSessionHandler(registry, ff);
// use all the spots ( so 1 ) of the grid so that a queue builds up
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(ff);
- newSessionRequest.process();
- session = newSessionRequest.getTestSession();
+ handler.process();
+ session = handler.getSession();
+
// the test has been assigned.
Assert.assertNotNull(session);
// add the request to the queue
- final MockedRequestHandler req = request;
+
+ handler2 = GridHelper.createNewSessionHandler(registry, ff);
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
- req.process();
+ handler2.process();
}
}).start();
// the 1 slot of the node is used.
Assert.assertEquals(1, p1.getTotalUsed());
-
+
// registering the node again should discard the existing test. The node
// will be fresh as far as the grid is concerned so the 2nd test that
// was waiting in the queue should be processed.
@@ -90,7 +89,7 @@
@Test(timeout = 1000)
public void validateRequest2isNowRunningOnTheNode() throws InterruptedException {
Thread.sleep(250);
- Assert.assertNotNull(request.getTestSession());
+ Assert.assertNotNull(handler2.getSession());
}
@AfterClass
Index: java/server/test/org/openqa/grid/internal/RegistryStateTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/RegistryStateTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/RegistryStateTest.java (working copy)
@@ -22,17 +22,18 @@
import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.common.RegistrationRequest;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-
public class RegistryStateTest {
@@ -74,9 +75,9 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
registry.terminateSynchronousFOR_TEST_ONLY(session);
Assert.assertEquals(0, registry.getActiveSessions().size());
@@ -97,9 +98,9 @@
Assert.assertEquals(1, registry.getAllProxies().size());
Assert.assertEquals(0, registry.getUsedProxies().size());
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
Assert.assertEquals(1, registry.getActiveSessions().size());
Assert.assertEquals(1, registry.getAllProxies().size());
@@ -122,9 +123,9 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
registry.terminateSynchronousFOR_TEST_ONLY(session);
Assert.assertEquals(0, registry.getActiveSessions().size());
@@ -141,9 +142,9 @@
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
final ExternalSessionKey externalKey = ExternalSessionKey.fromString("1234");
session.setExternalKey(externalKey);
Index: java/server/src/org/openqa/grid/internal/Registry.java
===================================================================
--- java/server/src/org/openqa/grid/internal/Registry.java (revision 15989)
+++ java/server/src/org/openqa/grid/internal/Registry.java (working copy)
@@ -238,12 +238,12 @@
this.hub = hub;
}
- public void addNewSessionRequest(RequestHandler request) {
+ public void addNewSessionRequest(RequestHandler handler) {
try {
lock.lock();
- proxies.verifyAbilityToHandleDesiredCapabilities(request.getDesiredCapabilities());
- newSessionQueue.add(request);
+ proxies.verifyAbilityToHandleDesiredCapabilities(handler.getRequest().getDesiredCapabilities());
+ newSessionQueue.add(handler);
fireMatcherStateChanged();
} finally {
lock.unlock();
@@ -278,12 +278,12 @@
}
- private boolean takeRequestHandler(RequestHandler request) {
- final TestSession session = proxies.getNewSession(request.getDesiredCapabilities());
+ private boolean takeRequestHandler(RequestHandler handler) {
+ final TestSession session = proxies.getNewSession(handler.getRequest().getDesiredCapabilities());
final boolean sessionCreated = session != null;
if (sessionCreated) {
activeTestSessions.add(session);
- request.bindSession(session);
+ handler.bindSession(session);
}
return sessionCreated;
}
Index: java/server/test/org/openqa/grid/internal/SmokeTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/SmokeTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/SmokeTest.java (working copy)
@@ -19,20 +19,19 @@
import static org.openqa.grid.common.RegistrationRequest.APP;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
-import org.openqa.grid.web.servlet.handler.RequestType;
-
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
+
public class SmokeTest {
private static Registry registry;
@@ -75,9 +74,9 @@
for (int i = 0; i < MAX; i++) {
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, ie);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ie);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
inc();
sessions.add(session);
}
@@ -87,9 +86,9 @@
for (int i = 0; i < MAX; i++) {
new Thread(new Runnable() { // Thread safety reviewed
public void run() {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, ff);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
inc();
sessions.add(session);
}
@@ -110,9 +109,7 @@
// all the tests ran via the registry.
while (stopped < 2 * MAX) {
for (TestSession session : sessions) {
- MockedRequestHandler stopSessionRequest = new MockedRequestHandler(registry);
- stopSessionRequest.setSession(session);
- stopSessionRequest.setRequestType(RequestType.STOP_SESSION);
+ RequestHandler stopSessionRequest = GridHelper.createStopSessionHandler(registry, session);
stopSessionRequest.process();
stopped++;
sessions.remove(session);
Index: java/server/test/org/openqa/grid/internal/RegistryTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/RegistryTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/RegistryTest.java (working copy)
@@ -17,26 +17,26 @@
package org.openqa.grid.internal;
+import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
+import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Random;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.atomic.AtomicInteger;
+
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.common.RegistrationRequest;
import org.openqa.grid.common.exception.CapabilityNotPresentOnTheGridException;
+import org.openqa.grid.common.exception.GridException;
import org.openqa.grid.internal.listeners.RegistrationListener;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.selenium.remote.CapabilityType;
-import org.openqa.grid.common.exception.GridException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Random;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.atomic.AtomicInteger;
-
-import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
-import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
-
public class RegistryTest {
private static final int TOTAL_THREADS = 100;
@@ -108,7 +108,7 @@
public void emptyRegistry() throws Throwable {
Registry registry = Registry.newInstance();
try {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
} catch (Exception e) {
Assert.assertEquals(GridException.class, e.getCause().getClass());
@@ -123,7 +123,7 @@
registry.setThrowOnCapabilityNotPresent(false);
try {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
} finally {
registry.stop();
@@ -136,10 +136,10 @@
Registry registry = Registry.newInstance();
try {
registry.add(new RemoteProxy(req, registry));
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
- System.out.println(newSessionRequest.getDesiredCapabilities());
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
+ System.out.println(newSessionRequest.getRequest().getDesiredCapabilities());
newSessionRequest.process();
- System.out.println("new " + newSessionRequest.getTestSession());
+ System.out.println("new " + newSessionRequest.getSession());
} catch (Exception e) {
Assert.assertEquals(CapabilityNotPresentOnTheGridException.class, e.getCause().getClass());
} finally {
@@ -154,7 +154,7 @@
try {
registry.add(new RemoteProxy(req, registry));
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
} finally {
@@ -191,7 +191,7 @@
* try to simulate a real proxy. The proxy registration takes up to 1 sec to register, and crashes
* in 10% of the case.
*
- * @author Fran�ois Reynaud
+ * @author Francois Reynaud
*/
class MyRemoteProxy extends RemoteProxy implements RegistrationListener {
Index: java/server/test/org/openqa/grid/internal/listener/RegistrationListenerTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/listener/RegistrationListenerTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/listener/RegistrationListenerTest.java (working copy)
@@ -17,6 +17,12 @@
package org.openqa.grid.internal.listener;
+import static org.openqa.grid.common.RegistrationRequest.APP;
+import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
+
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -25,16 +31,10 @@
import org.openqa.grid.internal.RemoteProxy;
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.listeners.RegistrationListener;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-import static org.openqa.grid.common.RegistrationRequest.APP;
-import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
-
-
public class RegistrationListenerTest {
private static boolean serverUp = false;
@@ -73,10 +73,10 @@
Registry registry = Registry.newInstance();
registry.add(new MyRemoteProxy(req, registry));
- MockedRequestHandler request = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler request = GridHelper.createNewSessionHandler(registry, app1);
request.process();
- Assert.assertNotNull(request.getTestSession());
+ Assert.assertNotNull(request.getSession());
Assert.assertTrue(serverUp);
}
@@ -156,9 +156,9 @@
// check onRegistration has not run yet.
Assert.assertEquals(slowRemoteUp, false);
// should return right away, as RemoteProxy is fast.
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req =GridHelper.createNewSessionHandler(registry, app1);
req.process();
- TestSession s1 = req.getTestSession();
+ TestSession s1 = req.getSession();
Assert.assertNotNull(s1);
// slow proxy hasn't finished to start slow remote, isn't accessible via
@@ -168,9 +168,9 @@
Assert.assertEquals(false, slowRemoteUp);
// will block until MySlowRemoteProxy is fully registered.
- MockedNewSessionRequestHandler req2 = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req2 = GridHelper.createNewSessionHandler(registry, app1);
req2.process();
- TestSession s2 = req2.getTestSession();
+ TestSession s2 = req2.getSession();
Assert.assertNotNull(s2);
// return when the proxy is visible = fully registered. So registry has
// 2 proxies at that point.
Index: java/server/test/org/openqa/grid/build.desc
===================================================================
--- java/server/test/org/openqa/grid/build.desc (revision 15989)
+++ java/server/test/org/openqa/grid/build.desc (working copy)
@@ -10,4 +10,5 @@
deps = [
"//java/server/src/org/openqa/grid",
"//third_party/java/junit",
+ "//third_party/java/jmock"
])
\ No newline at end of file
Index: java/server/src/org/openqa/grid/internal/utils/ForwardConfiguration.java
===================================================================
--- java/server/src/org/openqa/grid/internal/utils/ForwardConfiguration.java (revision 15989)
+++ java/server/src/org/openqa/grid/internal/utils/ForwardConfiguration.java (working copy)
@@ -1,57 +0,0 @@
-package org.openqa.grid.internal.utils;
-
-import org.openqa.grid.common.SeleniumProtocol;
-
-/**
- * some requests have to be more than just forwarded.
- * For instance for selenium1, the new session request contains
- * the sessionId in the body of the response, so the response has
- * to be read.
- * ForwardConfig gather all those special cases.
- *
- */
-public class ForwardConfiguration {
-
- private SeleniumProtocol protocol = null;
- private boolean bodyHasToBeRead = false;
- private boolean isNewSessionRequest = false;
- private String contentOverWrite = null;
- public SeleniumProtocol getProtocol() {
- return protocol;
- }
- public void setProtocol(SeleniumProtocol protocol) {
- this.protocol = protocol;
- }
- public boolean isBodyHasToBeRead() {
- return bodyHasToBeRead;
- }
-
- /**
- * true = the body of the request will be read before being forwarded.
- * @param bodyHasToBeRead
- */
- public void setBodyHasToBeRead(boolean bodyHasToBeRead) {
- this.bodyHasToBeRead = bodyHasToBeRead;
- }
- public boolean isNewSessionRequest() {
- return isNewSessionRequest;
- }
- public void setNewSessionRequest(boolean isNewSessionRequest) {
- this.isNewSessionRequest = isNewSessionRequest;
- }
- public String getContentOverWrite() {
- return contentOverWrite;
- }
-
- /**
- * if !=null : the body of the request will be replaced by contentOverWrite.
- * @param contentOverWrite
- */
- public void setContentOverWrite(String contentOverWrite) {
- this.contentOverWrite = contentOverWrite;
- }
-
-
-
-}
-
Index: java/server/src/org/openqa/grid/internal/NewSessionRequestQueue.java
===================================================================
--- java/server/src/org/openqa/grid/internal/NewSessionRequestQueue.java (revision 15989)
+++ java/server/src/org/openqa/grid/internal/NewSessionRequestQueue.java (working copy)
@@ -108,7 +108,7 @@
public synchronized Iterable<DesiredCapabilities> getDesiredCapabilities() {
List<DesiredCapabilities> result = new ArrayList<DesiredCapabilities>();
for (RequestHandler req : newSessionRequests) {
- result.add(new DesiredCapabilities(req.getDesiredCapabilities()));
+ result.add(new DesiredCapabilities(req.getRequest().getDesiredCapabilities()));
}
return result;
}
Index: java/server/test/org/openqa/grid/internal/NewSessionRequestTimeout.java
===================================================================
--- java/server/test/org/openqa/grid/internal/NewSessionRequestTimeout.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/NewSessionRequestTimeout.java (working copy)
@@ -19,15 +19,15 @@
import static org.openqa.grid.common.RegistrationRequest.APP;
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-
public class NewSessionRequestTimeout {
private static Registry registry;
@@ -52,12 +52,12 @@
public void method() {
// should work
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, ff);
+ MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
// should throw after 1sec being stuck in the queue
try {
- MockedRequestHandler newSessionRequest2 = new MockedNewSessionRequestHandler(registry, ff);
+ MockedRequestHandler newSessionRequest2 = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest2.process();
} catch (RuntimeException ignore) {
}
Index: java/server/test/org/openqa/grid/internal/listener/SessionListenerTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/listener/SessionListenerTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/listener/SessionListenerTest.java (working copy)
@@ -17,6 +17,17 @@
package org.openqa.grid.internal.listener;
+import static org.openqa.grid.common.RegistrationRequest.APP;
+import static org.openqa.grid.common.RegistrationRequest.CLEAN_UP_CYCLE;
+import static org.openqa.grid.common.RegistrationRequest.ID;
+import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
+import static org.openqa.grid.common.RegistrationRequest.TIME_OUT;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -27,19 +38,9 @@
import org.openqa.grid.internal.TestSession;
import org.openqa.grid.internal.listeners.TestSessionListener;
import org.openqa.grid.internal.listeners.TimeoutListener;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
-
-import static org.openqa.grid.common.RegistrationRequest.APP;
-import static org.openqa.grid.common.RegistrationRequest.CLEAN_UP_CYCLE;
-import static org.openqa.grid.common.RegistrationRequest.ID;
-import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
-import static org.openqa.grid.common.RegistrationRequest.TIME_OUT;
-
public class SessionListenerTest {
static class MyRemoteProxy extends RemoteProxy implements TestSessionListener {
@@ -77,9 +78,10 @@
Registry registry = Registry.newInstance();
registry.add(new MyRemoteProxy(req, registry));
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req = GridHelper.createNewSessionHandler(registry, app1);
+
req.process();
- TestSession session = req.getTestSession();
+ TestSession session = req.getSession();
Assert.assertEquals(true, session.get("FLAG"));
registry.terminate(session, SessionTerminationReason.CLIENT_STOPPED_SESSION);
try {
@@ -93,7 +95,7 @@
/**
* buggy proxy that will throw an exception the first time beforeSession is called.
*
- * @author Fran�ois Reynaud
+ * @author Francois Reynaud
*/
static class MyBuggyBeforeRemoteProxy extends RemoteProxy implements TestSessionListener {
@@ -103,8 +105,7 @@
super(request, registry);
}
- public void afterSession(TestSession session) {
- }
+ public void afterSession(TestSession session) {}
public void beforeSession(TestSession session) {
if (firstCall) {
@@ -122,7 +123,7 @@
Registry registry = Registry.newInstance();
registry.add(new MyBuggyBeforeRemoteProxy(req, registry));
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req = GridHelper.createNewSessionHandler(registry, app1);
try {
req.process();
} catch (Exception ignore) {
@@ -138,10 +139,10 @@
Assert.assertEquals(registry.getActiveSessions().size(), 0);
- MockedNewSessionRequestHandler req2 = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req2 = GridHelper.createNewSessionHandler(registry, app1);
req2.process();
- TestSession session = req2.getTestSession();
+ TestSession session = req2.getSession();
Assert.assertNotNull(session);
Assert.assertEquals(registry.getActiveSessions().size(), 1);
@@ -150,7 +151,7 @@
/**
* buggy proxy that will throw an exception the first time beforeSession is called.
*
- * @author Fran�ois Reynaud
+ * @author Francois Reynaud
*/
static class MyBuggyAfterRemoteProxy extends RemoteProxy implements TestSessionListener {
@@ -162,8 +163,7 @@
throw new NullPointerException();
}
- public void beforeSession(TestSession session) {
- }
+ public void beforeSession(TestSession session) {}
}
static volatile boolean processed = false;
@@ -177,9 +177,9 @@
try {
registry.add(new MyBuggyAfterRemoteProxy(req, registry));
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler req = GridHelper.createNewSessionHandler(registry, app1);
req.process();
- TestSession session = req.getTestSession();
+ TestSession session = req.getSession();
Assert.assertEquals(registry.getActiveSessions().size(), 1);
Assert.assertNotNull(session);
registry.terminate(session, SessionTerminationReason.CLIENT_STOPPED_SESSION);
@@ -189,18 +189,15 @@
e.printStackTrace();
}
- final
- MockedNewSessionRequestHandler
- req2 =
- new MockedNewSessionRequestHandler(registry, app1);
+ final RequestHandler req2 = GridHelper.createNewSessionHandler(registry, app1);
- new Thread(new Runnable() { // Thread safety reviewed
+ new Thread(new Runnable() { // Thread safety reviewed
- public void run() {
- req2.process();
- processed = true;
- }
- }).start();
+ public void run() {
+ req2.process();
+ processed = true;
+ }
+ }).start();
Thread.sleep(100);
Assert.assertFalse(processed);
@@ -239,8 +236,7 @@
}
}
- public void beforeSession(TestSession session) {
- }
+ public void beforeSession(TestSession session) {}
public void beforeRelease(TestSession session) {
getRegistry().terminate(session, SessionTerminationReason.CLIENT_STOPPED_SESSION);
@@ -273,9 +269,9 @@
proxy.setupTimeoutListener();
registry.add(proxy);
- MockedNewSessionRequestHandler r = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler r = GridHelper.createNewSessionHandler(registry, app1);
r.process();
- TestSession session = r.getTestSession();
+ TestSession session = r.getSession();
Thread.sleep(150);
// the session has timed out -> doing the long after method.
Index: java/server/test/org/openqa/grid/internal/ParallelTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/ParallelTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/ParallelTest.java (working copy)
@@ -22,19 +22,20 @@
import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.openqa.grid.common.RegistrationRequest;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
-
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openqa.grid.common.RegistrationRequest;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
+
public class ParallelTest {
static RegistrationRequest req = null;
@@ -70,7 +71,7 @@
RemoteProxy p1 = new RemoteProxy(req, registry);
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
} finally {
registry.stop();
@@ -91,19 +92,20 @@
RemoteProxy p1 = new RemoteProxy(req, registry);
try {
registry.add(p1);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
TestThreadCounter testThreadCounter = new TestThreadCounter();
testThreadCounter.start(new Runnable() {
public void run() {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ MockedRequestHandler newSessionRequest =
+ GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
processed = true;
}
});
testThreadCounter.waitUntilStarted(1);
- Assert.assertFalse(processed); // Can race, but should *never* fail
+ Assert.assertFalse(processed); // Can race, but should *never* fail
} finally {
registry.stop();
}
@@ -119,7 +121,7 @@
try {
registry.add(p1);
for (int i = 0; i < 5; i++) {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ MockedRequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
}
} finally {
@@ -144,8 +146,7 @@
for (int i = 0; i < 6; i++) {
testThreadCounter.start(new Runnable() {
public void run() {
- MockedRequestHandler newSessionRequest =
- new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
count.incrementAndGet();
}
@@ -183,8 +184,7 @@
for (int i = 0; i < 5; i++) {
testThreadCounter.start(new Runnable() {
public void run() {
- MockedRequestHandler newSessionRequest =
- new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
}
});
@@ -193,8 +193,7 @@
testThreadCounter.start(new Runnable() {
public void run() {
- MockedRequestHandler newSessionRequest =
- new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
app6Done = true;
}
@@ -237,9 +236,9 @@
// reserve 5 app1
List<TestSession> used = new ArrayList<TestSession>();
for (int i = 0; i < 5; i++) {
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- used.add(newSessionRequest.getTestSession());
+ used.add(newSessionRequest.getSession());
}
Assert.assertEquals(registry.getActiveSessions().size(), 5);
@@ -255,9 +254,9 @@
for (int i = 0; i < 5; i++) {
int original = registry.getActiveSessions().size();
Assert.assertEquals(original, i);
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app1);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app1);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
used.add(session);
}
@@ -265,9 +264,9 @@
registry.terminateSynchronousFOR_TEST_ONLY(used.get(0));
- MockedRequestHandler newSessionRequest = new MockedNewSessionRequestHandler(registry, app2);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, app2);
newSessionRequest.process();
- newSessionRequest.getTestSession();
+ newSessionRequest.getSession();
Assert.assertEquals(registry.getActiveSessions().size(), 5);
System.out.println(registry.getAllProxies());
} finally {
Index: java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedResponse.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedResponse.java (revision 0)
+++ java/server/src/org/openqa/grid/web/servlet/handler/SeleniumBasedResponse.java (revision 0)
@@ -0,0 +1,40 @@
+package org.openqa.grid.web.servlet.handler;
+
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpServletResponseWrapper;
+
+public class SeleniumBasedResponse extends HttpServletResponseWrapper {
+
+ private byte[] forwardedContent = null;
+ private final String encoding = "UTF-8";
+
+ public SeleniumBasedResponse(HttpServletResponse response) {
+ super(response);
+ }
+
+ public String getForwardedContent() {
+ if (forwardedContent == null){
+ return null;
+ }
+ Charset charset = Charset.forName(encoding);
+ CharsetDecoder decoder = charset.newDecoder();
+ CharBuffer cbuf = null;
+ try {
+ cbuf = decoder.decode(ByteBuffer.wrap(forwardedContent));
+ } catch (CharacterCodingException e) {
+ // ignore
+ }
+ return cbuf.toString();
+ }
+
+ public void setForwardedContent(byte[] forwardedContent) {
+ this.forwardedContent = forwardedContent;
+ }
+
+}
Index: java/server/test/org/openqa/grid/internal/DefaultToFIFOPriorityTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/DefaultToFIFOPriorityTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/DefaultToFIFOPriorityTest.java (working copy)
@@ -17,24 +17,23 @@
package org.openqa.grid.internal;
-import junit.framework.Assert;
+import static org.openqa.grid.common.RegistrationRequest.APP;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.openqa.grid.internal.listeners.Prioritizer;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
-import org.openqa.grid.internal.mock.MockedRequestHandler;
-import org.openqa.grid.web.servlet.handler.RequestType;
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import static org.openqa.grid.common.RegistrationRequest.APP;
+import junit.framework.Assert;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.openqa.grid.internal.listeners.Prioritizer;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.internal.mock.MockedRequestHandler;
+
// TODO freynaud copy paste from PriorityTestLoad ....
public class DefaultToFIFOPriorityTest {
@@ -69,17 +68,15 @@
Map<String, Object> cap = new HashMap<String, Object>();
cap.put(APP, "FF");
cap.put("_priority", i);
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, cap);
+ MockedRequestHandler req =GridHelper.createNewSessionHandler(registry, cap);
requests.add(req);
}
// use all the spots ( so 1 ) of the grid so that a queue buils up
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(ff);
+ MockedRequestHandler newSessionRequest =GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
- session = newSessionRequest.getTestSession();
+ session = newSessionRequest.getSession();
// fill the queue with MAX requests.
for (MockedRequestHandler h : requests) {
@@ -106,15 +103,15 @@
int cpt = 0;
while (cpt < 8) {
try {
- requests.get(0).getTestSession();
+ requests.get(0).getSession();
break;
} catch (IllegalAccessError e) {
// ignore.
}
Thread.sleep(250);
}
- Assert.assertNotNull(requests.get(0).getTestSession());
- Assert.assertEquals(1, requests.get(0).getDesiredCapabilities().get("_priority"));
+ Assert.assertNotNull(requests.get(0).getSession());
+ Assert.assertEquals(1, requests.get(0).getRequest().getDesiredCapabilities().get("_priority"));
}
@AfterClass
Index: java/server/test/org/openqa/grid/internal/LoadBalancedTests.java
===================================================================
--- java/server/test/org/openqa/grid/internal/LoadBalancedTests.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/LoadBalancedTests.java (working copy)
@@ -22,17 +22,18 @@
import static org.openqa.grid.common.RegistrationRequest.MAX_SESSION;
import static org.openqa.grid.common.RegistrationRequest.REMOTE_HOST;
+import java.util.HashMap;
+import java.util.Map;
+
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.common.RegistrationRequest;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
+import org.openqa.grid.internal.mock.MockedRequestHandler;
-import java.util.HashMap;
-import java.util.Map;
-
public class LoadBalancedTests {
private static Registry registry;
@@ -64,9 +65,9 @@
// request 5 slots : it should spread the load to 1 FF per proxy.
for (int i = 0; i < 5; i++) {
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, ff);
- req.process();
- TestSession session = req.getTestSession();
+ MockedRequestHandler req =GridHelper.createNewSessionHandler(registry, ff);
+ req.process();
+ TestSession session = req.getSession();
Assert.assertNotNull(session);
Assert.assertEquals(session.getSlot().getProxy().getTotalUsed(), 1);
@@ -74,9 +75,9 @@
// 2 ff per proxy.
for (int i = 0; i < 5; i++) {
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, ff);
+ MockedRequestHandler req =GridHelper.createNewSessionHandler(registry, ff);
req.process();
- TestSession session = req.getTestSession();
+ TestSession session = req.getSession();
Assert.assertNotNull(session);
Assert.assertEquals(2, session.getSlot().getProxy().getTotalUsed());
// and release
@@ -85,9 +86,8 @@
// at that point, 1 FF per proxy
for (int i = 0; i < 5; i++) {
- MockedNewSessionRequestHandler req = new MockedNewSessionRequestHandler(registry, ff);
- req.process();
- TestSession session = req.getTestSession();
+ MockedRequestHandler req =GridHelper.createNewSessionHandler(registry, ff);req.process();
+ TestSession session = req.getSession();
Assert.assertNotNull(session);
Assert.assertEquals(session.getSlot().getProxy().getTotalUsed(), 2);
Index: java/server/test/org/openqa/grid/internal/PriorityTest.java
===================================================================
--- java/server/test/org/openqa/grid/internal/PriorityTest.java (revision 15989)
+++ java/server/test/org/openqa/grid/internal/PriorityTest.java (working copy)
@@ -19,21 +19,22 @@
import static org.openqa.grid.common.RegistrationRequest.APP;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.grid.internal.listeners.Prioritizer;
-import org.openqa.grid.internal.mock.MockedNewSessionRequestHandler;
+import org.openqa.grid.internal.mock.GridHelper;
import org.openqa.grid.internal.mock.MockedRequestHandler;
+import org.openqa.grid.web.servlet.handler.RequestHandler;
import org.openqa.grid.web.servlet.handler.RequestType;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
public class PriorityTest {
private static Registry registry;
@@ -50,13 +51,13 @@
static Map<String, Object> ff = new HashMap<String, Object>();
static RemoteProxy p1;
- static MockedNewSessionRequestHandler newSessionRequest1;
- static MockedNewSessionRequestHandler newSessionRequest2;
- static MockedNewSessionRequestHandler newSessionRequest3;
- static MockedNewSessionRequestHandler newSessionRequest4;
- static MockedNewSessionRequestHandler newSessionRequest5;
+ static RequestHandler newSessionRequest1;
+ static RequestHandler newSessionRequest2;
+ static RequestHandler newSessionRequest3;
+ static RequestHandler newSessionRequest4;
+ static RequestHandler newSessionRequest5;
- static List<MockedRequestHandler> requests = new ArrayList<MockedRequestHandler>();
+ static List<RequestHandler> requests = new ArrayList<RequestHandler>();
/**
* create a hub with 1 FF
@@ -92,11 +93,11 @@
ff5.put(APP, "FF");
ff5.put("_priority", 5);
- newSessionRequest1 = new MockedNewSessionRequestHandler(registry, ff1);
- newSessionRequest2 = new MockedNewSessionRequestHandler(registry, ff2);
- newSessionRequest3 = new MockedNewSessionRequestHandler(registry, ff3);
- newSessionRequest4 = new MockedNewSessionRequestHandler(registry, ff4);
- newSessionRequest5 = new MockedNewSessionRequestHandler(registry, ff5);
+ newSessionRequest1 = GridHelper.createNewSessionHandler(registry, ff1);
+ newSessionRequest2 = GridHelper.createNewSessionHandler(registry, ff2);
+ newSessionRequest3 = GridHelper.createNewSessionHandler(registry, ff3);
+ newSessionRequest4 = GridHelper.createNewSessionHandler(registry, ff4);
+ newSessionRequest5 = GridHelper.createNewSessionHandler(registry, ff5);
requests.add(newSessionRequest1);
requests.add(newSessionRequest2);
@@ -104,20 +105,18 @@
requests.add(newSessionRequest3);
requests.add(newSessionRequest4);
- MockedRequestHandler newSessionRequest = new MockedRequestHandler(registry);
- newSessionRequest.setRequestType(RequestType.START_SESSION);
- newSessionRequest.setDesiredCapabilities(ff);
+ RequestHandler newSessionRequest = GridHelper.createNewSessionHandler(registry, ff);
newSessionRequest.process();
- TestSession session = newSessionRequest.getTestSession();
+ TestSession session = newSessionRequest.getSession();
// fill the queue with 5 requests.
- for (MockedRequestHandler h : requests) {
- final MockedRequestHandler req = h;
- new Thread(new Runnable() { // Thread safety reviewed
- public void run() {
- req.process();
- }
- }).start();
+ for (RequestHandler h : requests) {
+ final RequestHandler req = h;
+ new Thread(new Runnable() { // Thread safety reviewed
+ public void run() {
+ req.process();
+ }
+ }).start();
}
while (registry.getNewSessionRequestCount() != 5) {
@@ -135,7 +134,7 @@
@Test
public void validate() throws InterruptedException {
Thread.sleep(250);
- Assert.assertNotNull(newSessionRequest5.getTestSession());
+ Assert.assertNotNull(newSessionRequest5.getSession());
}
Index: java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequest.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequest.java (revision 0)
+++ java/server/src/org/openqa/grid/web/servlet/handler/WebDriverRequest.java (revision 0)
@@ -0,0 +1,77 @@
+package org.openqa.grid.web.servlet.handler;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.openqa.grid.common.exception.GridException;
+import org.openqa.grid.internal.ExternalSessionKey;
+import org.openqa.grid.internal.Registry;
+import org.openqa.grid.internal.TestSession;
+import org.openqa.grid.internal.exception.NewSessionException;
+
+public class WebDriverRequest extends SeleniumBasedRequest {
+
+ public WebDriverRequest(HttpServletRequest httpServletRequest, Registry registry) {
+ super(httpServletRequest, registry);
+ }
+
+ @Override
+ public RequestType extractRequestType() {
+ if ("/session".equals(getPathInfo())) {
+ return RequestType.START_SESSION;
+ } else if (getMethod().equalsIgnoreCase("DELETE")) {
+ ExternalSessionKey externalKey = ExternalSessionKey.fromWebDriverRequest(getPathInfo());
+ if (getPathInfo().endsWith("/session/" + externalKey.getKey())) {
+ return RequestType.STOP_SESSION;
+ }
+ }
+ return RequestType.REGULAR;
+ }
+
+ @Override
+ public ExternalSessionKey extractSession() {
+ if (getRequestType() == RequestType.START_SESSION) {
+ throw new IllegalAccessError("Cannot call that method of a new session request.");
+ }
+ String path = getPathInfo();
+ return ExternalSessionKey.fromWebDriverRequest(path);
+ }
+
+ @Override
+ public Map<String, Object> extractDesiredCapability() {
+ String json = getBody();
+ Map<String, Object> desiredCapability = new HashMap<String, Object>();
+ try {
+ JSONObject map = new JSONObject(json);
+ JSONObject dc = map.getJSONObject("desiredCapabilities");
+ for (Iterator iterator = dc.keys(); iterator.hasNext();) {
+ String key = (String) iterator.next();
+ Object value = dc.get(key);
+ if (value == JSONObject.NULL) {
+ value = null;
+ }
+ desiredCapability.put(key, value);
+ }
+ } catch (JSONException e) {
+ throw new GridException("Cannot extract a capabilities from the request " + json);
+ }
+ return desiredCapability;
+ }
+
+ @Override
+ public String getNewSessionRequestedCapability(TestSession session) {
+ try {
+ JSONObject c = new JSONObject();
+ c.put("desiredCapabilities", session.getRequestedCapabilities());
+ String content = c.toString();
+ return content;
+ } catch (JSONException e) {
+ throw new NewSessionException("Error with the request " + e.getMessage(),e);
+ }
+ }
+}
Index: java/server/src/org/openqa/grid/internal/TestSession.java
===================================================================
--- java/server/src/org/openqa/grid/internal/TestSession.java (revision 15989)
+++ java/server/src/org/openqa/grid/internal/TestSession.java (working copy)
@@ -17,28 +17,6 @@
package org.openqa.grid.internal;
-import com.google.common.io.ByteStreams;
-import com.google.common.io.Closeables;
-
-import org.apache.http.Header;
-import org.apache.http.HttpEntity;
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.client.ClientProtocolException;
-import org.apache.http.client.HttpClient;
-import org.apache.http.entity.InputStreamEntity;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.message.BasicHttpEntityEnclosingRequest;
-import org.apache.http.message.BasicHttpRequest;
-import org.apache.http.util.EntityUtils;
-import org.openqa.grid.common.SeleniumProtocol;
-import org.openqa.grid.common.exception.ClientGoneException;
-import org.openqa.grid.common.exception.GridException;
-import org.openqa.grid.internal.listeners.CommandListener;
-import org.openqa.grid.internal.utils.ForwardConfiguration;
-import org.openqa.grid.web.Hub;
-
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -48,6 +26,11 @@
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
@@ -59,6 +42,30 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.http.Header;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.ClientProtocolException;
+import org.apache.http.client.HttpClient;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.message.BasicHttpEntityEnclosingRequest;
+import org.apache.http.message.BasicHttpRequest;
+import org.apache.http.util.EntityUtils;
+import org.openqa.grid.common.exception.ClientGoneException;
+import org.openqa.grid.common.exception.GridException;
+import org.openqa.grid.internal.listeners.CommandListener;
+import org.openqa.grid.web.Hub;
+import org.openqa.grid.web.servlet.handler.LegacySeleniumRequest;
+import org.openqa.grid.web.servlet.handler.RequestType;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedResponse;
+import org.openqa.grid.web.servlet.handler.WebDriverRequest;
+
+import com.google.common.io.ByteStreams;
+import com.google.common.io.Closeables;
+
/**
* Represent a running test for the hub/registry. A test session is created when a TestSlot becomes
* available for a test. <p/> The session is destroyed when the test ends ( ended by the client or
@@ -144,8 +151,8 @@
// seen any new
// commands during that time frame.
return slot.getProtocol().isSelenium()
- && elapsedSinceCreation > MAX_IDLE_TIME_BEFORE_CONSIDERED_ORPHANED
- && sessionCreatedAt == lastActivity;
+ && elapsedSinceCreation > MAX_IDLE_TIME_BEFORE_CONSIDERED_ORPHANED
+ && sessionCreatedAt == lastActivity;
}
/**
@@ -181,30 +188,23 @@
@Override
public String toString() {
return externalKey != null ? "ext. key " + externalKey : internalKey
- + " (int. key, remote not contacted yet.)";
+ + " (int. key, remote not contacted yet.)";
}
- /**
- * Forward the request to the remote, execute the TestSessionListeners if applicable.
- */
- public void forward(HttpServletRequest request, HttpServletResponse response) throws IOException {
- forward(request, response, new ForwardConfiguration());
- }
-
private HttpClient getClient() {
return slot.getProxy().getHttpClientFactory().getGridHttpClient();
}
/**
- * @param config for special cases of forwards. Typically is the content has to be intercepted, or
- * if the hub has to be updated before the new session request is forwarded.
+ * forwards the request to the node.
*/
- public String forward(HttpServletRequest request, HttpServletResponse response,
- ForwardConfiguration config) throws IOException {
+ public String forward(SeleniumBasedRequest request, HttpServletResponse response)
+ throws IOException {
String res = null;
forwardingRequest = true;
+
try {
if (slot.getProxy() instanceof CommandListener) {
((CommandListener) slot.getProxy()).beforeCommand(this, request, response);
@@ -212,7 +212,7 @@
lastActivity = timeSource.currentTimeInMillis();
- HttpRequest proxyRequest = prepareProxyRequest(request, config);
+ HttpRequest proxyRequest = prepareProxyRequest(request/*, config*/);
HttpResponse proxyResponse = sendRequestToNode(proxyRequest);
@@ -221,35 +221,36 @@
response.setStatus(proxyResponse.getStatusLine().getStatusCode());
processResponseHeaders(request, response, slot.getRemoteURL(), proxyResponse);
- updateHubIfNewWebDriverSession(config, proxyResponse);
+ updateHubIfNewWebDriverSession(request, proxyResponse);
HttpEntity responseBody = proxyResponse.getEntity();
+ byte[] contentBeingForwarded = null;
if (responseBody != null) {
try {
InputStream in = responseBody.getContent();
-
- boolean isSeleniumNewSessionRequest =
- config.isNewSessionRequest() && config.getProtocol() == SeleniumProtocol.Selenium;
- if (config.isBodyHasToBeRead() || isSeleniumNewSessionRequest) {
+
+ if (request.getRequestType() == RequestType.START_SESSION
+ && request instanceof LegacySeleniumRequest) {
res = getResponseUtf8Content(in);
+
+ updateHubNewSeleniumSession(res);
- if (isSeleniumNewSessionRequest) {
- updateHubNewSeleniumSession(res);
- }
in = new ByteArrayInputStream(res.getBytes("UTF-8"));
}
final byte[] bytes = drainInputStream(in);
+ contentBeingForwarded = bytes;
writeRawBody(response, bytes);
} finally {
EntityUtils.consume(responseBody);
}
-
}
if (slot.getProxy() instanceof CommandListener) {
- ((CommandListener) slot.getProxy()).afterCommand(this, request, response);
+ SeleniumBasedResponse wrappedResponse = new SeleniumBasedResponse(response);
+ wrappedResponse.setForwardedContent(contentBeingForwarded);
+ ((CommandListener) slot.getProxy()).afterCommand(this, request, wrappedResponse);
}
return res;
} finally {
@@ -257,15 +258,16 @@
}
}
-
+
private void updateHubNewSeleniumSession(String content) {
ExternalSessionKey key = ExternalSessionKey.fromResponseBody(content);
setExternalKey(key);
}
- private void updateHubIfNewWebDriverSession(ForwardConfiguration config,
- HttpResponse proxyResponse) {
- if (config.isNewSessionRequest() && config.getProtocol() == SeleniumProtocol.WebDriver) {
+ private void updateHubIfNewWebDriverSession(SeleniumBasedRequest request,
+ HttpResponse proxyResponse) {
+ if (request.getRequestType() == RequestType.START_SESSION
+ && request instanceof WebDriverRequest) {
Header h = proxyResponse.getFirstHeader("Location");
if (h == null) {
throw new GridException(
@@ -277,7 +279,7 @@
}
private HttpResponse sendRequestToNode(HttpRequest proxyRequest) throws ClientProtocolException,
- IOException {
+ IOException {
HttpClient client = getClient();
URL remoteURL = slot.getRemoteURL();
HttpHost host = new HttpHost(remoteURL.getHost(), remoteURL.getPort());
@@ -285,7 +287,7 @@
return client.execute(host, proxyRequest);
}
- private HttpRequest prepareProxyRequest(HttpServletRequest request, ForwardConfiguration config)
+ private HttpRequest prepareProxyRequest(HttpServletRequest request/*, ForwardConfiguration config*/)
throws IOException {
URL remoteURL = slot.getRemoteURL();
@@ -293,7 +295,7 @@
String path = request.getRequestURI();
if (!path.startsWith(pathSpec)) {
throw new IllegalStateException("Expected path " + path + " to start with pathSpec "
- + pathSpec);
+ + pathSpec);
}
String end = path.substring(pathSpec.length());
String ok = remoteURL + end;
@@ -305,12 +307,12 @@
}
HttpRequest proxyRequest;
- if (config.getContentOverWrite() != null) {
+ /*if (config.getContentOverWrite() != null) {
BasicHttpEntityEnclosingRequest r =
new BasicHttpEntityEnclosingRequest(request.getMethod(), uri);
r.setEntity(new StringEntity(config.getContentOverWrite()));
proxyRequest = r;
- } else if (body != null) {
+ } else */if (body != null) {
BasicHttpEntityEnclosingRequest r =
new BasicHttpEntityEnclosingRequest(request.getMethod(), uri);
r.setEntity(new InputStreamEntity(body, request.getContentLength()));
@@ -319,7 +321,7 @@
proxyRequest = new BasicHttpRequest(request.getMethod(), uri);
}
- for (Enumeration<?> e = request.getHeaderNames(); e.hasMoreElements(); ) {
+ for (Enumeration<?> e = request.getHeaderNames(); e.hasMoreElements();) {
String headerName = (String) e.nextElement();
if ("Content-Length".equalsIgnoreCase(headerName)) {
@@ -331,8 +333,7 @@
return proxyRequest;
}
- private void writeRawBody(HttpServletResponse response, byte[] rawBody)
- throws IOException {
+ private void writeRawBody(HttpServletResponse response, byte[] rawBody) throws IOException {
OutputStream out = response.getOutputStream();
try {
// We need to set the Content-Length header before we write to the output stream. Usually
@@ -389,8 +390,7 @@
}
private void processResponseHeaders(HttpServletRequest request, HttpServletResponse response,
- URL remoteURL, HttpResponse proxyResponse)
- throws MalformedURLException {
+ URL remoteURL, HttpResponse proxyResponse) throws MalformedURLException {
String pathSpec = request.getServletPath() + request.getContextPath();
for (Header header : proxyResponse.getAllHeaders()) {
String name = header.getName();
@@ -457,8 +457,7 @@
case Selenium:
request =
new BasicHttpRequest("POST", remoteURL.toExternalForm()
- + "/?cmd=testComplete&sessionId=" + getExternalKey()
- .getKey());
+ + "/?cmd=testComplete&sessionId=" + getExternalKey().getKey());
break;
case WebDriver:
String uri = remoteURL.toString() + "/session/" + externalKey;
Index: java/server/src/org/openqa/grid/web/servlet/DriverServlet.java
===================================================================
--- java/server/src/org/openqa/grid/web/servlet/DriverServlet.java (revision 15989)
+++ java/server/src/org/openqa/grid/web/servlet/DriverServlet.java (working copy)
@@ -27,7 +27,8 @@
import org.json.JSONObject;
import org.openqa.grid.internal.Registry;
import org.openqa.grid.web.servlet.handler.RequestHandler;
-import org.openqa.grid.web.servlet.handler.WebDriverRequestHandler;
+import org.openqa.grid.web.servlet.handler.SeleniumBasedRequest;
+import org.openqa.grid.web.servlet.handler.WebDriverRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -76,12 +77,14 @@
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException {
RequestHandler req = null;
+ SeleniumBasedRequest r = null;
try {
- req = RequestHandler.createHandler(request, response, getRegistry());
+ r = SeleniumBasedRequest.createFromRequest(request, getRegistry());
+ req = new RequestHandler(r, response, getRegistry());
req.process();
} catch (Throwable e) {
- if (req instanceof WebDriverRequestHandler) {
+ if (r instanceof WebDriverRequest) {
// http://code.google.com/p/selenium/wiki/JsonWireProtocol#Error_Handling
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment