001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.junit;
017:
018: import com.google.gwt.core.ext.TreeLogger;
019: import com.google.gwt.core.ext.UnableToCompleteException;
020: import com.google.gwt.junit.remote.BrowserManager;
021:
022: import java.net.InetAddress;
023: import java.net.UnknownHostException;
024:
025: /**
026: * Runs remotely in web mode. This feature is experimental and is not officially
027: * supported.
028: */
029: class RunStyleRemoteWeb extends RunStyle {
030:
031: private static final int INITIAL_KEEPALIVE_MS = 5000;
032: private static final int PING_KEEPALIVE_MS = 2000;
033:
034: // Larger values when debugging the unit test framework, so you
035: // don't get spurious timeouts.
036: // private static final int INITIAL_KEEPALIVE_MS = 500000;
037: // private static final int PING_KEEPALIVE_MS = 200000;
038:
039: /**
040: * Remote browser managers.
041: */
042: private final BrowserManager[] browserManagers;
043:
044: /**
045: * References to the remote browser processes.
046: */
047: private int[] remoteTokens;
048:
049: /**
050: * The containing shell.
051: */
052: private final JUnitShell shell;
053:
054: private boolean running = false;
055:
056: /**
057: * @param shell the containing shell
058: */
059: public RunStyleRemoteWeb(JUnitShell shell,
060: BrowserManager[] browserManagers) {
061: this .shell = shell;
062: this .browserManagers = browserManagers;
063: this .remoteTokens = new int[browserManagers.length];
064: }
065:
066: @Override
067: public void maybeLaunchModule(String moduleName, boolean forceLaunch)
068: throws UnableToCompleteException {
069:
070: if (forceLaunch || !running) {
071: shell.compileForWebMode(moduleName, null);
072: String localhost;
073:
074: try {
075: localhost = InetAddress.getLocalHost().getHostAddress();
076: } catch (UnknownHostException e) {
077: throw new RuntimeException(
078: "Unable to determine my ip address", e);
079: }
080: String url = "http://" + localhost + ":" + shell.getPort()
081: + "/" + moduleName;
082:
083: try {
084: for (int i = 0; i < remoteTokens.length; ++i) {
085: int remoteToken = remoteTokens[i];
086: BrowserManager mgr = browserManagers[i];
087: if (remoteToken != 0) {
088: mgr.killBrowser(remoteToken);
089: }
090: remoteTokens[i] = mgr.launchNewBrowser(url,
091: INITIAL_KEEPALIVE_MS);
092: }
093: } catch (Exception e) {
094: shell.getTopLogger().log(TreeLogger.ERROR,
095: "Error launching remote browser", e);
096: throw new UnableToCompleteException();
097: }
098:
099: running = true;
100: }
101: }
102:
103: @Override
104: public boolean wasInterrupted() {
105: for (int i = 0; i < remoteTokens.length; ++i) {
106: int remoteToken = remoteTokens[i];
107: BrowserManager mgr = browserManagers[i];
108: if (remoteToken > 0) {
109: try {
110: mgr.keepAlive(remoteToken, PING_KEEPALIVE_MS);
111: } catch (Exception e) {
112: // TODO(tobyr): We're failing on the first exception, rather than
113: // collecting them, but that's probably OK for now.
114: shell
115: .getTopLogger()
116: .log(
117: TreeLogger.WARN,
118: "Unexpected exception keeping remote browser alive",
119: e);
120: return true;
121: }
122: }
123: }
124: return false;
125: }
126:
127: }
|