1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
53
54
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);
124
125 fanOutStreamSender.shutdown();
126 }
127
128 @Test public void testBadWorker() throws Exception {
129
130
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 }