1 33 34 package edu.rice.cs.util.newjvm; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 import edu.rice.cs.drjava.config.FileOption; 38 39 import edu.rice.cs.util.Log; 40 import edu.rice.cs.util.swing.Utilities; 41 42 import java.rmi.RemoteException ; 43 44 47 public class IntegratedMasterSlaveTest extends DrJavaTestCase { 48 49 private static Log _log = new Log("MasterSlave.txt", false); 50 51 final TestMasterJVM _testMaster = new TestMasterJVM(); 53 public void tearDown() throws Exception { 54 _testMaster.dispose(); 55 super.tearDown(); 56 } 57 58 public void testItAll() throws Exception { 59 for (int i = 0; i < 2; i++) _testMaster.runTestSequence(); 63 _log.log("testItAll completed"); 64 } 65 66 public void testImmediateQuit() throws Exception { 67 for (int i = 0; i < 5; i++) _testMaster.runImmediateQuitTest(); 68 _log.log("testImmediateQuit completed"); 69 } 70 71 72 private class TestMasterJVM extends AbstractMasterJVM implements TestMasterRemote { 73 74 75 private volatile boolean _justQuit; private final Object _quitLock = new Object (); 77 78 79 private volatile boolean _connected; private final Object _connectedLock = new Object (); 81 82 83 private volatile char _letter; 84 private final Object _letterLock = new Object (); 85 86 private volatile String _currentTest = ""; 87 88 public TestMasterJVM() { super(CounterSlave.class.getName()); } 89 90 94 public void runImmediateQuitTest() throws Exception { 95 96 98 _currentTest = "runImmediateQuitTest"; 99 _justQuit = false; 100 _connected = false; 101 _letter = 'a'; 103 invokeSlave(new String [0], FileOption.NULL_FILE); 104 105 107 quitSlave(); 109 110 112 synchronized(_quitLock) { while (! _justQuit) _quitLock.wait(); } 114 115 _currentTest = ""; 117 118 _log.log("Ran immediateQuitTest"); 120 121 } 123 124 public void runTestSequence() throws Exception { 125 126 _currentTest = "runTestSequence"; 127 _justQuit = false; 128 _connected = false; 129 _letter = 'a'; 130 131 invokeSlave(new String [0], FileOption.NULL_FILE); 132 133 synchronized(_connectedLock) { while (! _connected) _connectedLock.wait(); } 134 135 ((TestSlaveRemote)getSlave()).startLetterTest(); 136 137 _log.log("letter test started"); 138 139 synchronized(_letterLock) { while (_letter != 'f') { _letterLock.wait(); } } 141 142 _log.log("letter test finished"); 143 144 for (int i = 0; i < 7; i++) { 145 int value = ((TestSlaveRemote) getSlave()).getNumber(); 146 assertEquals("value returned by slave", i, value); 147 } 148 149 _log.log("number test finished"); 150 151 quitSlave(); 152 synchronized(_quitLock) { while (! _justQuit) _quitLock.wait(); } _currentTest = ""; 154 _log.log("Ran runTestSequence"); 155 } 156 157 public char getLetter() { 158 synchronized(_letterLock) { 159 char ret = _letter; 160 _letter++; 161 _letterLock.notify(); 162 return ret; 163 } 164 } 165 166 protected void handleSlaveConnected() { 167 TestSlaveRemote slave = (TestSlaveRemote) getSlave(); 168 assertTrue("slave is set", slave != null); 169 assertTrue("startUp not in progress", ! isStartupInProgress()); 170 assertEquals("letter value", 'a', _letter); 172 synchronized(_connectedLock) { 173 _connected = true; 174 _connectedLock.notify(); 175 } 176 _log.log("_handleSlaveConnected() finished"); 177 } 178 179 protected void handleSlaveQuit(int status) { 180 assertEquals("slave result code", 0, status); 181 if (_currentTest.equals("runTestSequence")) { 182 assertEquals("last letter returned", 'f', _letter); 184 } 185 assertTrue("slave is not set", getSlave() == null); 186 assertTrue("startUp not in progress", ! isStartupInProgress()); 187 188 synchronized(_quitLock) { 190 _justQuit = true; 191 _quitLock.notify(); 192 } 193 } 194 195 198 public void errorStartingSlave(Throwable cause) throws RemoteException { 199 fail("There was an error starting the slave JVM: " + cause); 200 } 201 } 202 203 212 public static class CounterSlave extends AbstractSlaveJVM implements TestSlaveRemote { 213 214 public static final CounterSlave ONLY = new CounterSlave(); 215 216 private volatile int _counter = 0; 217 private volatile TestMasterRemote _master = null; 218 219 private CounterSlave() { } 220 221 public synchronized int getNumber() { return _counter++; } 222 223 protected void handleStart(MasterRemote m) { _master = (TestMasterRemote) m; } 224 225 public void startLetterTest() throws RemoteException { 226 Thread thread = new Thread () { 228 public void run() { 229 try { 230 for (char c = 'a'; c <= 'e'; c++) { 231 char got = _master.getLetter(); 232 if (c != got) System.exit(2); 233 } 234 235 Thread.sleep(15000); 237 System.exit(4); 238 } 239 catch (InterruptedException e) { System.exit(5); } 240 catch (RemoteException re) { 241 javax.swing.JOptionPane.showMessageDialog(null, re.toString()); 242 System.exit(3); 243 } 244 catch (ClassCastException cce) { System.exit(1); } 245 } 246 }; 247 thread.start(); 248 } 249 } 250 251 public interface TestSlaveRemote extends SlaveRemote { 252 public int getNumber() throws RemoteException ; 253 public void startLetterTest() throws RemoteException ; 254 } 255 256 public interface TestMasterRemote extends MasterRemote { 257 public char getLetter() throws RemoteException ; 258 } 259 } 260 | Popular Tags |