1 package net.sourceforge.cruisecontrol.distributed; 2 3 import junit.framework.TestCase; 4 import org.apache.log4j.Logger; 5 import net.sourceforge.cruisecontrol.builders.DistributedMasterBuilderTest; 6 import net.jini.core.lookup.ServiceRegistrar; 7 import net.jini.core.lookup.ServiceTemplate; 8 import net.jini.core.entry.Entry; 9 10 import java.rmi.RemoteException ; 11 12 19 public class BuildAgentTest extends TestCase { 20 21 private static final Logger LOG = Logger.getLogger(net.sourceforge.cruisecontrol.distributed.BuildAgentTest.class); 22 23 private Process jiniProcess; 24 25 protected void setUp() throws Exception { 26 jiniProcess = DistributedMasterBuilderTest.startJini(); 27 } 28 29 protected void tearDown() throws Exception { 30 BuildAgent.kill(); 31 DistributedMasterBuilderTest.killJini(jiniProcess); 32 } 33 34 private static Object findAgent(final ServiceRegistrar reg, 35 final int retries, final boolean expectedFoundResult) 36 throws RemoteException , InterruptedException { 37 38 final Entry[] entries = SearchablePropertyEntries.getPropertiesAsEntryArray( 39 new SearchablePropertyEntries( 40 BuildAgentServiceImplTest.TEST_USER_DEFINED_PROPERTIES_FILE).getProperties() 41 ); 42 return findAgent(reg, retries, expectedFoundResult, entries); 43 } 44 45 private static Object findAgent(final ServiceRegistrar reg, 46 final int retries, final boolean expectedFoundResult, final Entry[] entries) 47 throws RemoteException , InterruptedException { 48 49 int retryCount = 0; 50 Object result; 51 boolean isFound; 52 do { 53 if (retryCount > 0) { 54 LOG.info("\tFind agent unit test retry " + retryCount + "..."); 55 Thread.sleep(1000); } 57 58 result = reg.lookup(new ServiceTemplate(null, 59 new Class []{BuildAgentService.class}, 60 entries)); 61 62 isFound = (result != null); 63 64 retryCount++; 65 66 } while ((expectedFoundResult != isFound) && (retryCount < retries)); 67 68 if (expectedFoundResult) { 69 assertNotNull("Should find agent", result); 70 } else { 71 assertNull("Should not find agent", result); 72 } 73 74 return result; 75 } 76 77 84 public void manual_testRestart() throws Exception { 85 final ServiceRegistrar reg = DistributedMasterBuilderTest.findTestLookupService(20); 86 assertNotNull("Couldn't find registrar.", reg); 87 88 final Entry[] entries = SearchablePropertyEntries.getPropertiesAsEntryArray( 89 new SearchablePropertyEntries( 90 BuildAgentServiceImplTest.TEST_USER_DEFINED_PROPERTIES_FILE).getProperties() 91 ); 92 final int idxBuildTypeEntry = 3; 94 assertEquals("Wrong entry in position where we expected to find 'build.type'.", 95 "build.type", ((PropertyEntry) entries[idxBuildTypeEntry]).name); 96 entries[idxBuildTypeEntry] = new PropertyEntry(((PropertyEntry) entries[idxBuildTypeEntry]).name, "test"); 97 98 final BuildAgentService agentService = (BuildAgentService) findAgent(reg, 3, true, entries); 99 assertNotNull(agentService.getMachineName()); 100 agentService.restart(false); 101 Thread.sleep(60 * 1000); 103 try { 105 agentService.getMachineName(); 106 fail("Agent should be dead"); 107 } catch (Exception e) { 108 ; } 110 final BuildAgentService agentService2 = (BuildAgentService) findAgent(reg, 3, true, entries); 112 assertNotNull(agentService2.getMachineName()); 113 agentService2.kill(false); 114 } 115 116 public void testKillNoUI() throws Exception { 117 ServiceRegistrar reg = DistributedMasterBuilderTest.findTestLookupService(20); 118 assertNotNull("Couldn't find registrar.", reg); 119 findAgent(reg, 3, false); 120 121 final Thread t = new Thread () { 122 public void run() { 123 BuildAgent.main(new String [] { 124 BuildAgentServiceImplTest.TEST_AGENT_PROPERTIES_FILE, 125 BuildAgentServiceImplTest.TEST_USER_DEFINED_PROPERTIES_FILE, 126 BuildAgent.MAIN_ARG_SKIP_UI 127 }); 128 } 129 }; 130 t.start(); 131 final int maxWaitSecsStartup = 30; 133 int count = 0; 134 while (count < maxWaitSecsStartup && BuildAgent.getMainThread() == null) { 135 Thread.sleep(1000); 136 count++; 137 } 138 assertNotNull("Agent didn't start before timeout.", BuildAgent.getMainThread()); 139 assertTrue("Agent didn't init before timeout.", BuildAgent.getMainThread().isAlive()); 140 findAgent(reg, 10, true); 141 final Thread mainThread = BuildAgent.getMainThread(); assertNotNull("Main thread should not be null.", mainThread); 143 144 BuildAgent.kill(); 145 assertFalse("Agent didn't die before timeout.", mainThread.isAlive()); findAgent(reg, 10, false); 147 } 148 149 public void xxx_testKill() throws Exception { 151 ServiceRegistrar reg = DistributedMasterBuilderTest.findTestLookupService(20); 152 assertNotNull("Couldn't find registrar.", reg); 153 findAgent(reg, 3, false); 154 155 final Thread t = new Thread () { 156 public void run() { 157 BuildAgent.main(new String [] { 158 BuildAgentServiceImplTest.TEST_AGENT_PROPERTIES_FILE, 159 BuildAgentServiceImplTest.TEST_USER_DEFINED_PROPERTIES_FILE 160 }); 161 } 162 }; 163 t.start(); 164 final int maxWaitSecsStartup = 30; 166 int count = 0; 167 while (count < maxWaitSecsStartup && BuildAgent.getMainThread() == null) { 168 Thread.sleep(1000); 169 count++; 170 } 171 assertNotNull("Agent didn't start before timeout.", BuildAgent.getMainThread()); 172 assertTrue("Agent didn't init before timeout.", BuildAgent.getMainThread().isAlive()); 173 findAgent(reg, 10, true); 174 final Thread mainThread = BuildAgent.getMainThread(); assertNotNull("Main thread should not be null.", mainThread); 176 177 BuildAgent.kill(); 178 assertFalse("Agent didn't die before timeout.", mainThread.isAlive()); findAgent(reg, 10, false); 180 } 181 } 182 | Popular Tags |