|
| 1 | +/* |
| 2 | + * Copyright 2008 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.google.gwt.junit.remote; |
| 17 | + |
| 18 | +import junit.framework.TestCase; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.rmi.Naming; |
| 23 | +import java.rmi.NotBoundException; |
| 24 | +import java.rmi.RemoteException; |
| 25 | +import java.rmi.registry.LocateRegistry; |
| 26 | +import java.rmi.registry.Registry; |
| 27 | +import java.util.Properties; |
| 28 | + |
| 29 | +/** |
| 30 | + * Exercise the BrowserManagerServer with the serialized option turned on and |
| 31 | + * off. |
| 32 | + */ |
| 33 | +public class BrowserManagerServerTest extends TestCase { |
| 34 | + /** |
| 35 | + * Seconds for simulated browser to hang. |
| 36 | + */ |
| 37 | + static final int TIMEOUT_MS = 2000; |
| 38 | + /** |
| 39 | + * Time to wait between keepAlive calls to all browsers. |
| 40 | + */ |
| 41 | + static final int PING_INTERVAL_MS = 100; |
| 42 | + static final boolean LOG = false; |
| 43 | + |
| 44 | + private static final String REGISTRATION_KEY = "sleep-" + (TIMEOUT_MS / 1000) |
| 45 | + + "-seconds"; |
| 46 | + private static Registry rmiRegistry = null; |
| 47 | + |
| 48 | + private int portArg = Registry.REGISTRY_PORT; |
| 49 | + private BrowserManagerServer server = null; |
| 50 | + |
| 51 | + /** |
| 52 | + * Run a test with the 'serialized' flag on. |
| 53 | + */ |
| 54 | + public void testSerializedServer() throws Exception { |
| 55 | + BrowserManager browserManager = startBrowserManagerServer(true); |
| 56 | + |
| 57 | + // Launch some browsers all at once. |
| 58 | + final int NUM_BROWSERS = 4; |
| 59 | + int tokens[] = new int[NUM_BROWSERS]; |
| 60 | + |
| 61 | + for (int i = 0; i < NUM_BROWSERS; i++) { |
| 62 | + tokens[i] = launchBrowser(browserManager, i); |
| 63 | + } |
| 64 | + |
| 65 | + // Give them a chance to startup. |
| 66 | + Thread.sleep(TIMEOUT_MS / 4); |
| 67 | + |
| 68 | + int numQueued = server.numQueued(); |
| 69 | + int numRunning = server.numRunning(); |
| 70 | + assertEquals("Did not find the number of expected browsers queued up", |
| 71 | + NUM_BROWSERS - 1, numQueued); |
| 72 | + assertEquals("Expected only one running at a time for serialized.", 1, |
| 73 | + numRunning); |
| 74 | + |
| 75 | + outer : for (int runningBrowser = 0; runningBrowser < NUM_BROWSERS; ++runningBrowser) { |
| 76 | + // The current browser should be dead within twice the expected timeout. |
| 77 | + long shouldBeDeadBy = System.currentTimeMillis() + (TIMEOUT_MS * 2); |
| 78 | + while (System.currentTimeMillis() < shouldBeDeadBy) { |
| 79 | + assertTrue(server.numRunning() <= 1); |
| 80 | + |
| 81 | + // Ping every alive browser. |
| 82 | + for (int i = runningBrowser; i < NUM_BROWSERS; ++i) { |
| 83 | + // Keep the browser alive with a margin of safety until the next ping. |
| 84 | + try { |
| 85 | + browserManager.keepAlive(tokens[runningBrowser], TIMEOUT_MS); |
| 86 | + } catch (IllegalStateException ise) { |
| 87 | + // Expected. |
| 88 | + assertEquals("The wrong browser is dead", runningBrowser, i); |
| 89 | + if (LOG) { |
| 90 | + System.out.println("Browser token: " + tokens[i] |
| 91 | + + " exited sucessfully"); |
| 92 | + } |
| 93 | + // Ensure it's legal to kill the dead browser. |
| 94 | + browserManager.killBrowser(tokens[i]); |
| 95 | + // Continue with the next active browser. |
| 96 | + continue outer; |
| 97 | + } |
| 98 | + } |
| 99 | + Thread.sleep(PING_INTERVAL_MS); |
| 100 | + } |
| 101 | + // Error case |
| 102 | + fail("Browser " + runningBrowser + " failed to exit in a timely manner"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Run a test with the 'serialized' flag on. |
| 108 | + */ |
| 109 | + public void testUnserializedServer() throws Exception { |
| 110 | + BrowserManager browserManager = startBrowserManagerServer(false); |
| 111 | + |
| 112 | + // Launch some browsers all at once. |
| 113 | + final int NUM_BROWSERS = 6; |
| 114 | + int tokens[] = new int[NUM_BROWSERS]; |
| 115 | + |
| 116 | + for (int i = 0; i < NUM_BROWSERS; i++) { |
| 117 | + tokens[i] = launchBrowser(browserManager, i); |
| 118 | + } |
| 119 | + |
| 120 | + // Give them a chance to startup. |
| 121 | + Thread.sleep(TIMEOUT_MS / 4); |
| 122 | + |
| 123 | + int numQueued = server.numQueued(); |
| 124 | + int numRunning = server.numRunning(); |
| 125 | + assertEquals("No queuing should occur", 0, numQueued); |
| 126 | + assertEquals("All browers should be running", NUM_BROWSERS, numRunning); |
| 127 | + |
| 128 | + // The current browser should be dead within twice the expected timeout. |
| 129 | + long shouldBeDeadBy = System.currentTimeMillis() + (TIMEOUT_MS * 2); |
| 130 | + int liveBrowsers = NUM_BROWSERS; |
| 131 | + while (System.currentTimeMillis() < shouldBeDeadBy) { |
| 132 | + // Ping every alive browser. |
| 133 | + for (int i = 0; i < NUM_BROWSERS; ++i) { |
| 134 | + if (tokens[i] == 0) { |
| 135 | + // This one's already dead. |
| 136 | + continue; |
| 137 | + } |
| 138 | + // Keep the browser alive with a margin of safety until the next ping. |
| 139 | + try { |
| 140 | + browserManager.keepAlive(tokens[i], TIMEOUT_MS); |
| 141 | + } catch (IllegalStateException ise) { |
| 142 | + // Expected. |
| 143 | + if (LOG) { |
| 144 | + System.out.println("Browser token: " + tokens[i] |
| 145 | + + " exited sucessfully"); |
| 146 | + } |
| 147 | + // Ensure it's legal to kill the dead browser. |
| 148 | + browserManager.killBrowser(tokens[i]); |
| 149 | + |
| 150 | + tokens[i] = 0; |
| 151 | + --liveBrowsers; |
| 152 | + |
| 153 | + if (liveBrowsers == 0) { |
| 154 | + // All done; |
| 155 | + return; |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + Thread.sleep(PING_INTERVAL_MS); |
| 160 | + } |
| 161 | + // Error case |
| 162 | + fail(liveBrowsers + " browsers failed to exit in a timely manner"); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Start up the RMI registry and create a shell script that just sleeps for |
| 167 | + * 'timeout' seconds. |
| 168 | + */ |
| 169 | + @Override |
| 170 | + protected void setUp() throws Exception { |
| 171 | + if (rmiRegistry == null) { |
| 172 | + rmiRegistry = LocateRegistry.createRegistry(portArg); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Clean up temporary files. |
| 178 | + */ |
| 179 | + @Override |
| 180 | + protected void tearDown() throws Exception { |
| 181 | + // De-register the server. |
| 182 | + if (rmiRegistry != null) { |
| 183 | + rmiRegistry.unbind(REGISTRATION_KEY); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Start a browser task on the server. |
| 189 | + * |
| 190 | + * @param browserManager handle to the browser manager instance |
| 191 | + * @param token browser ident number |
| 192 | + */ |
| 193 | + private int launchBrowser(BrowserManager browserManager, int token) |
| 194 | + throws RemoteException { |
| 195 | + return browserManager.launchNewBrowser("# client" + token, TIMEOUT_MS); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Starts up an instance of BrowserManagerServer. |
| 200 | + * |
| 201 | + * @param isSerialized true to enable the serialized mode (run one browser |
| 202 | + * instance at a time, queue any others.) |
| 203 | + * @return the newly created instance of BrowserManagerServer on success |
| 204 | + */ |
| 205 | + private BrowserManager startBrowserManagerServer(boolean isSerialized) |
| 206 | + throws RemoteException, MalformedURLException, NotBoundException { |
| 207 | + // Construct a launch command for relaunching the JVM out of process, |
| 208 | + // running DummyProcess. |
| 209 | + StringBuilder sb = new StringBuilder(); |
| 210 | + Properties properties = System.getProperties(); |
| 211 | + sb.append(properties.getProperty("java.home")); |
| 212 | + sb.append(File.separatorChar); |
| 213 | + sb.append("bin"); |
| 214 | + sb.append(File.separatorChar); |
| 215 | + sb.append("java"); |
| 216 | + sb.append('\n'); |
| 217 | + |
| 218 | + sb.append("-classpath"); |
| 219 | + sb.append('\n'); |
| 220 | + |
| 221 | + sb.append(properties.getProperty("java.class.path")); |
| 222 | + sb.append('\n'); |
| 223 | + |
| 224 | + sb.append(DummyProcess.class.getName()); |
| 225 | + |
| 226 | + server = new BrowserManagerServer(sb.toString(), isSerialized); |
| 227 | + rmiRegistry.rebind(REGISTRATION_KEY, server); |
| 228 | + |
| 229 | + // Server started. Now, create a client and send some commands to it |
| 230 | + String url = "rmi://localhost/" + REGISTRATION_KEY; |
| 231 | + BrowserManager browserManager = (BrowserManager) Naming.lookup(url); |
| 232 | + return browserManager; |
| 233 | + } |
| 234 | +} |
0 commit comments