KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > distributed > BuildAgentTest


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 JavaDoc;
11
12 /**
13  * Created by IntelliJ IDEA.
14  * User: drollo
15  * Date: Jul 6, 2005
16  * Time: 4:12:20 PM
17  * To change this template use File | Settings | File Templates.
18  */

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 JavaDoc jiniProcess;
24
25     protected void setUp() throws Exception JavaDoc {
26         jiniProcess = DistributedMasterBuilderTest.startJini();
27     }
28
29     protected void tearDown() throws Exception JavaDoc {
30         BuildAgent.kill();
31         DistributedMasterBuilderTest.killJini(jiniProcess);
32     }
33
34     private static Object JavaDoc findAgent(final ServiceRegistrar reg,
35                                     final int retries, final boolean expectedFoundResult)
36             throws RemoteException JavaDoc, InterruptedException JavaDoc {
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 JavaDoc findAgent(final ServiceRegistrar reg,
46                                     final int retries, final boolean expectedFoundResult, final Entry[] entries)
47             throws RemoteException JavaDoc, InterruptedException JavaDoc {
48
49         int retryCount = 0;
50         Object JavaDoc result;
51         boolean isFound;
52         do {
53             if (retryCount > 0) {
54                 LOG.info("\tFind agent unit test retry " + retryCount + "...");
55                 Thread.sleep(1000); // wait a bit before retrying
56
}
57
58             result = reg.lookup(new ServiceTemplate(null,
59                 new Class JavaDoc[]{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     /**
78      * This test requires a bunch of manual steps:
79      * 1. Build the cc-agent.war (created via: ant war-agent).
80      * 2. Deploy cc-agent.war to a web server.
81      * 3. Manually launch agent via webstat (http://localhost:8080/cc-agent/agent.jnlp).
82      * 4. Manually run this test.
83      */

84     public void manual_testRestart() throws Exception JavaDoc {
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         // work around timestamp prefix in build.type entry
93
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         // allow time for the relaunched agent to spin up and register
102
Thread.sleep(60 * 1000);
103         // verify first agent is dead
104
try {
105             agentService.getMachineName();
106             fail("Agent should be dead");
107         } catch (Exception JavaDoc e) {
108             ; // good, this is what we want.
109
}
110         // find the newly relaunched agent
111
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 JavaDoc {
117         ServiceRegistrar reg = DistributedMasterBuilderTest.findTestLookupService(20);
118         assertNotNull("Couldn't find registrar.", reg);
119         findAgent(reg, 3, false);
120
121         final Thread JavaDoc t = new Thread JavaDoc() {
122             public void run() {
123                 BuildAgent.main(new String JavaDoc[] {
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         // allow BuildAgent main to load and register
132
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 JavaDoc mainThread = BuildAgent.getMainThread(); // hold onto main thread since kill nullifies it
142
assertNotNull("Main thread should not be null.", mainThread);
143
144         BuildAgent.kill();
145         assertFalse("Agent didn't die before timeout.", mainThread.isAlive()); // check held thread
146
findAgent(reg, 10, false);
147     }
148
149     // @todo Find way to skip this test if we are running in a "headless" environment
150
public void xxx_testKill() throws Exception JavaDoc {
151         ServiceRegistrar reg = DistributedMasterBuilderTest.findTestLookupService(20);
152         assertNotNull("Couldn't find registrar.", reg);
153         findAgent(reg, 3, false);
154
155         final Thread JavaDoc t = new Thread JavaDoc() {
156             public void run() {
157                 BuildAgent.main(new String JavaDoc[] {
158                     BuildAgentServiceImplTest.TEST_AGENT_PROPERTIES_FILE,
159                     BuildAgentServiceImplTest.TEST_USER_DEFINED_PROPERTIES_FILE
160                 });
161             }
162         };
163         t.start();
164         // allow BuildAgent main to load and register
165
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 JavaDoc mainThread = BuildAgent.getMainThread(); // hold onto main thread since kill nullifies it
175
assertNotNull("Main thread should not be null.", mainThread);
176
177         BuildAgent.kill();
178         assertFalse("Agent didn't die before timeout.", mainThread.isAlive()); // check held thread
179
findAgent(reg, 10, false);
180     }
181 }
182
Popular Tags