1 4 package com.tctest.restart.unit; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 import EDU.oswego.cs.dl.util.concurrent.FutureResult; 8 9 import com.tc.exception.TCRuntimeException; 10 import com.tc.objectserver.control.ServerControl; 11 import com.tc.test.TCTestCase; 12 import com.tc.test.restart.RestartTestEnvironment; 13 import com.tc.util.PortChooser; 14 import com.tc.util.concurrent.ThreadUtil; 15 import com.tctest.restart.AppStateSnapshot; 16 17 import java.io.File ; 18 import java.util.Iterator ; 19 20 public class DSOServerRestartTester extends TCTestCase { 21 22 private RestartTestEnvironment env; 23 private long startTimeout = 30 * 1000; 24 25 public void setUp() throws Exception { 26 super.setUp(); 27 env = new RestartTestEnvironment(this.getTempDirectory(), new PortChooser(), RestartTestEnvironment.PROD_MODE); 28 env.setUp(); 29 } 30 31 public void tearDown() throws Exception { 32 assertNoTestThreadExceptions(); 33 env.shutdownServer(); 34 } 35 36 public void testLockState() throws Exception { 37 RestartUnitTestApp[] apps = new RestartUnitTestApp[10]; 38 for (int i = 0; i < apps.length; i++) { 39 apps[i] = new DSOServerRestartTestApp(env.getThreadGroup()); 40 } 41 42 basicTestLockState(apps); 43 44 waitForTestThreads(); 45 } 46 47 public void testDSOLockState() throws Exception { 48 env.newExtraProcessServer(); 49 env.startServer(startTimeout); 50 RestartUnitTestApp[] apps = newTestApps(10); 51 52 basicTestLockState(apps); 53 54 waitForTestThreads(); 55 } 56 57 private void basicTestLockState(RestartUnitTestApp[] apps) throws Exception { 58 Object lck = new Object (); 59 AppStateSnapshot snapshot = new AppStateSnapshot(apps); 60 CyclicBarrier startBarrier = new CyclicBarrier(apps.length + 1); 61 for (int i = 0; i < apps.length; i++) { 62 final RestartUnitTestApp app = apps[i]; 63 app.setID(i); 64 app.reset(); 65 app.setSharedLock(lck); 66 app.setStartBarrier(startBarrier); 67 env.startNewClientThread(new Runnable () { 68 public void run() { 69 app.attemptLock(); 70 } 71 }); 72 } 73 74 startBarrier.barrier(); 76 77 while (true) { 78 stopServerAndReleaseLock(snapshot); 79 System.out.println(snapshot); 80 if (!snapshot.allEnded() && !env.hasThreadGroupErrors()) env.startServer(startTimeout); 81 else break; 82 } 83 } 84 85 private void stopServerAndReleaseLock(AppStateSnapshot snapshot) throws Exception { 86 RestartUnitTestApp holder = (RestartUnitTestApp) snapshot.getHolder(); 87 if (holder == null) { 89 if (snapshot.allEnded()) return; 90 else fail("No holder, but not all ended: " + snapshot); 91 } 92 93 if (env.getServer().isRunning()) env.getServer().attemptShutdown(); 95 Thread.sleep(2000); 97 holder.fallThrough(); 99 while (env.getServer().isRunning()) { 101 Thread.sleep(100); 102 } 103 } 104 105 public void testDSOWaitNotify() throws Exception { 106 env.newExtraProcessServer(); 107 env.startServer(startTimeout); 108 RestartUnitTestApp[] apps = newTestApps(10); 109 basicTestWaitNotify(apps); 110 waitForTestThreads(); 111 } 112 113 private void basicTestWaitNotify(RestartUnitTestApp[] apps) throws Exception { 114 Object lck = new Object (); 115 AppStateSnapshot snapshot = new AppStateSnapshot(apps); 116 CyclicBarrier startBarrier = new CyclicBarrier(1); 117 apps[0].setDistributedSharedLock(lck); 118 ServerControl server = env.getServer(); 119 for (int i = 0; i < apps.length; i++) { 120 final RestartUnitTestApp app = apps[i]; 121 app.setID(i); 122 app.reset(); 123 app.setStartBarrier(startBarrier); 124 env.startNewClientThread(new Runnable () { 125 public void run() { 126 app.doWait(0); 127 } 128 }); 129 } 130 while (!snapshot.allWaiters()) { 131 snapshot.takeSnapshot(); 134 } 135 136 for (int i = 0; i < apps.length; i++) { 137 apps[0].doNotify(); 139 server.shutdown(); 140 snapshot.takeSnapshot(); 141 System.out.println(snapshot); 142 assertEquals(apps.length - i - 1, snapshot.getWaiters().length); 143 if (i + 1 < apps.length) { 144 server.start(startTimeout); 145 } 146 } 147 snapshot.takeSnapshot(); 148 assertTrue(snapshot.allEnded()); 149 } 150 151 public void testDSOWaitNotifyAll() throws Exception { 152 env.newExtraProcessServer().start(startTimeout); 153 RestartUnitTestApp[] apps = newTestApps(10); 154 basicTestWaitNotifyAll(apps); 155 waitForTestThreads(); 156 } 157 158 public void testDSOTimedWait() throws Exception { 159 env.newExtraProcessServer().start(startTimeout); 160 RestartUnitTestApp[] apps = newTestApps(10); 161 basicTestTimedWait(apps); 162 waitForTestThreads(); 163 } 164 165 private void basicTestTimedWait(RestartUnitTestApp[] apps) throws Exception { 166 ServerControl server = env.getServer(); 167 Object lck = new Object (); 168 AppStateSnapshot snapshot = new AppStateSnapshot(apps); 169 RestartUnitTestApp shutdownBlocker = newTestApps(1)[0]; 170 shutdownBlocker.setStartBarrier(new CyclicBarrier(1)); 171 final CyclicBarrier startBarrier = new CyclicBarrier(apps.length + 1); 172 apps[0].setDistributedSharedLock(lck); 173 final int waitTime = 10 * 1000; 174 for (int i = 0; i < apps.length; i++) { 175 final RestartUnitTestApp app = apps[i]; 176 app.setID(i); 177 app.reset(); 178 app.setStartBarrier(startBarrier); 179 env.startNewClientThread(new Runnable () { 180 public void run() { 181 app.doWait(waitTime); 182 } 183 }); 184 } 185 186 startBarrier.barrier(); 187 while (!snapshot.takeSnapshot().allWaiters()) { 188 System.out.println("Waiting for all apps to be waiters: " + snapshot); 189 ThreadUtil.reallySleep(100); 190 } 191 System.out.println("obtaining a lock to block shutdown..."); 192 FutureResult shutdownBlockerBarrier = new FutureResult(); 193 shutdownBlocker.blockShutdown(shutdownBlockerBarrier); 194 System.out.println("starting server shutdown sequence..."); 195 server.attemptShutdown(); 196 System.out.println("waiting for timers to go off in server before allowing shutdown to complete..."); 197 ThreadUtil.reallySleep(waitTime + 1000); 198 System.out.println("letting the server finish shutting down..."); 199 shutdownBlockerBarrier.set(new Object ()); 200 System.out.println("waiting until server is actually shut down..."); 201 server.waitUntilShutdown(); 202 203 assertTrue(snapshot.takeSnapshot().allWaiters()); 204 ThreadUtil.reallySleep(waitTime); 205 assertTrue(snapshot.takeSnapshot().allWaiters()); 206 server.start(startTimeout); 207 while (!snapshot.takeSnapshot().allEnded()) { 208 System.out.println(snapshot); 209 ThreadUtil.reallySleep(500); 210 } 211 } 212 213 private void basicTestWaitNotifyAll(RestartUnitTestApp[] apps) throws Exception { 214 ServerControl server = env.getServer(); 215 Object lck = new Object (); 216 AppStateSnapshot snapshot = new AppStateSnapshot(apps); 217 CyclicBarrier startBarrier = new CyclicBarrier(1); 218 apps[0].setDistributedSharedLock(lck); 219 for (int i = 0; i < apps.length; i++) { 220 final RestartUnitTestApp app = apps[i]; 221 app.setID(i); 222 app.reset(); 223 app.setStartBarrier(startBarrier); 224 env.startNewClientThread(new Runnable () { 225 public void run() { 226 app.doWait(0); 227 } 228 }); 229 } 230 while (!snapshot.allWaiters()) { 232 snapshot.takeSnapshot(); 233 } 234 235 server.shutdown(); 236 final CyclicBarrier serverStartBarrier = new CyclicBarrier(2); 237 final RestartUnitTestApp app = apps[0]; 238 new Thread (new Runnable () { 239 public void run() { 240 try { 241 serverStartBarrier.barrier(); 242 app.doNotifyAll(); 243 } catch (Exception e) { 244 throw new TCRuntimeException(e); 245 } 246 } 247 }).start(); 248 249 serverStartBarrier.barrier(); 250 server.start(startTimeout); 251 do { 252 snapshot.takeSnapshot(); 253 System.out.println(snapshot); 254 Thread.sleep(500); 255 } while (!snapshot.allEnded()); 256 } 257 258 private RestartUnitTestApp newRestartTestApp(ThreadGroup tg) throws Exception { 259 return null; 265 } 266 267 private RestartUnitTestApp[] newTestApps(int instanceCount) throws Exception { 268 RestartUnitTestApp[] apps = new RestartUnitTestApp[instanceCount]; 271 for (int i = 0; i < apps.length; i++) { 272 apps[i] = newRestartTestApp(env.getThreadGroup()); 273 } 274 return apps; 275 } 276 277 public void testServerStartAndStop() throws Exception { 278 env.newExtraProcessServer(); 279 assertTrue(isEmptyDirectory(env.getDBHome())); 280 env.startServer(startTimeout); 281 env.shutdownServer(); 282 assertFalse(isEmptyDirectory(env.getDBHome())); 283 } 284 285 private boolean isEmptyDirectory(File dir) { 286 if (dir.exists()) { 287 if (!dir.isDirectory()) return false; 288 if (dir.listFiles().length != 0) return false; 289 } 290 return true; 291 } 292 293 private void waitForTestThreads() { 294 while (!env.hasThreadGroupErrors() && env.hasActiveClients()) { 295 ThreadUtil.reallySleep(500); 296 } 297 } 298 299 private void assertNoTestThreadExceptions() { 300 boolean failed = false; 301 for (Iterator i = env.getThreadGroupErrors().iterator(); i.hasNext();) { 302 ((Throwable ) i.next()).printStackTrace(); 303 failed = true; 304 } 305 assertFalse("There were test thread exceptions", failed); 306 } 307 } 308 | Popular Tags |