View Javadoc

1   // Copyright (C) 2004 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.engine.agent;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.ObjectInputStream;
34  import java.io.OutputStream;
35  import java.util.List;
36  import java.util.Properties;
37  
38  import net.grinder.common.GrinderProperties;
39  import net.grinder.common.processidentity.WorkerIdentity;
40  import net.grinder.communication.FanOutStreamSender;
41  import net.grinder.engine.agent.AgentIdentityImplementation.WorkerIdentityImplementation;
42  import net.grinder.engine.common.EngineException;
43  import net.grinder.engine.common.ScriptLocation;
44  import net.grinder.engine.messages.InitialiseGrinderMessage;
45  import net.grinder.engine.process.WorkerProcessEntryPoint;
46  import net.grinder.util.Directory;
47  
48  import org.junit.Test;
49  
50  
51  /**
52   *  Unit tests for {@code ProcessWorkerFactory}.
53   *
54   * @author Philip Aston
55   */
56  public class TestProcessWorkerFactory {
57  
58    private static final String s_testClasspath =
59      System.getProperty("java.class.path");
60  
61    @Test public void testCreate() throws Exception {
62      final GrinderProperties grinderProperties = new GrinderProperties();
63      grinderProperties.setProperty("grinder.jvm.classpath", s_testClasspath);
64  
65      final Properties systemProperties = new Properties();
66  
67      final FanOutStreamSender fanOutStreamSender = new FanOutStreamSender(1);
68  
69      final WorkerProcessCommandLine commandLine =
70        new WorkerProcessCommandLine(
71          grinderProperties, systemProperties, "", new Directory());
72  
73      final List<String> commandList = commandLine.getCommandList();
74  
75      commandList.set(
76        commandList.indexOf(WorkerProcessEntryPoint.class.getName()),
77        ReadMessageEchoClass.class.getName());
78  
79      final ScriptLocation script =
80        new ScriptLocation(new Directory(new File(".")), new File("a"));
81      final boolean reportToConsole = false;
82  
83      final AgentIdentityImplementation agentIdentityImplementation =
84        new AgentIdentityImplementation(getClass().getName());
85  
86      final ProcessWorkerFactory processWorkerFactory =
87        new ProcessWorkerFactory(commandLine,
88                                 agentIdentityImplementation,
89                                 fanOutStreamSender,
90                                 reportToConsole,
91                                 script,
92                                 grinderProperties);
93  
94      final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
95      final ByteArrayOutputStream errorStream = new ByteArrayOutputStream();
96  
97      final Worker worker =
98        processWorkerFactory.create(outputStream, errorStream);
99  
100     assertTrue(worker.getIdentity().getName().endsWith("-0"));
101 
102     worker.waitFor();
103 
104     assertEquals("", new String(errorStream.toByteArray()));
105 
106     final InputStream output =
107       new ByteArrayInputStream(outputStream.toByteArray());
108     final ObjectInputStream objectInputStream = new ObjectInputStream(output);
109 
110     final InitialiseGrinderMessage echoedInitialiseGrinderMessage =
111       (InitialiseGrinderMessage)objectInputStream.readObject();
112 
113     assertEquals(reportToConsole,
114                  echoedInitialiseGrinderMessage.getReportToConsole());
115     assertEquals(script, echoedInitialiseGrinderMessage.getScript());
116     assertEquals(
117       agentIdentityImplementation,
118       echoedInitialiseGrinderMessage.getWorkerIdentity().getAgentIdentity());
119 
120     final byte[] remainingBytes = new byte[500];
121     final int n = output.read(remainingBytes);
122 
123     assertEquals(-1, n); // No arguments.
124 
125     fanOutStreamSender.shutdown();
126   }
127 
128   @Test public void testBadWorker() throws Exception {
129     // Test a dusty code path through AbstractWorkerFactory where
130     // the Worker communication stream doesn't work.
131     final AgentIdentityImplementation agentIdentityImplementation =
132       new AgentIdentityImplementation(getClass().getName());
133 
134     final AbstractWorkerFactory myWorkerFactory =
135       new AbstractWorkerFactory(agentIdentityImplementation,
136                                 null,
137                                 false,
138                                 new ScriptLocation(new File(".")),
139                                 null) {
140 
141         protected Worker
142           createWorker(WorkerIdentityImplementation workerIdentity,
143                        OutputStream outputStream,
144                        OutputStream errorStream)
145         throws EngineException {
146           return new Worker() {
147 
148             public WorkerIdentity getIdentity() {
149               return null;
150             }
151 
152             public OutputStream getCommunicationStream() {
153               return new OutputStream() {
154                 public void write(int b) throws IOException {
155                   throw new IOException("Broken");
156                 }
157               };
158             }
159 
160             public int waitFor() {
161               return 0;
162             }
163 
164             public void destroy() {
165             }
166           };
167         }
168     };
169 
170     try {
171       myWorkerFactory.create(null, null);
172       fail("Expected EngineException");
173     }
174     catch (EngineException e) {
175     }
176   }
177 }