You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by hb...@apache.org on 2002/10/19 21:58:18 UTC
cvs commit: jakarta-james/tests/src/java/org/apache/james/testing ProtocolSimulatorOptions.java ProtocolSimulator.java
hbedi 2002/10/19 12:58:17
Modified: tests/src/java/org/apache/james/testing
ProtocolSimulator.java
Added: tests/src/java/org/apache/james/testing
ProtocolSimulatorOptions.java
Log:
Extended protocol simulator to have configurable number of
- workers doing concurrent simulations and
- iterations per concurrent worker.
Added CLI options to run simulator. Makes usage etc. cleaner
Made the ProtocolSimulator code a bit cleaner.
Here are the command line options to run simulations.
java org.apache.james.testing.ProtocolSimulator
-s, --server
Remote Server Host To Test Against
-p, --port
Remote Server Port To Test Against
-t, --template
Protocol Session Template
-w, --workers
Number Of Concurrent Simulations. Default Is 1.
-i, --iterations
Number Of Protocol Simulations Iterations Per Worker. Defaul
t Is 1.
-h, --help
Usage Help
Revision Changes Path
1.4 +126 -59 jakarta-james/tests/src/java/org/apache/james/testing/ProtocolSimulator.java
Index: ProtocolSimulator.java
===================================================================
RCS file: /home/cvs/jakarta-james/tests/src/java/org/apache/james/testing/ProtocolSimulator.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ProtocolSimulator.java 18 Oct 2002 03:09:29 -0000 1.3
+++ ProtocolSimulator.java 19 Oct 2002 19:58:17 -0000 1.4
@@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.oro.text.perl.Perl5Util;
+import org.apache.james.util.CRLFPrintWriter;
/**
* <pre>
@@ -53,78 +54,124 @@
* S: \+OK.*
* C: PASS test
* S: \+OK.*
+ * C: QUIT
+ * S: \+OK.*
* </pre>
*/
public class ProtocolSimulator {
+ /** starts test run */
+ public static void main(String[] args) throws Exception {
+ ProtocolSimulatorOptions params = ProtocolSimulatorOptions.getOptions(args);
+ if ( params == null )
+ return;
+ new ProtocolSimulator(params).start();
+ }
+
+ // -------- constants shared by protocol simulators -----------
private static final String COMMENT_TAG = "#";
private static final String CLIENT_TAG = "C: ";
private static final String SERVER_TAG = "S: ";
private static final int CLIENT_TAG_LEN = CLIENT_TAG.length();
private static final int SERVER_TAG_LEN = SERVER_TAG.length();
- private static final Perl5Util perl = new Perl5Util();
- /**
- * Start the protocol simulator . <br>
- * Usage: ProtocolSimulator 'host' 'port' 'template'
- */
- public static void main(String[] args) throws Exception {
- System.out.println("Usage: ProtocolSimulator <host> <port> <template>");
- String host = args[0];
- int port = new Integer(args[1]).intValue();
- File template = new File(args[2]);
- System.out.println("Testing against "+host+":"+port+
- " using template: "+template.getAbsolutePath());
-
- // read socket. Ensure that lines written end with CRLF.
- Socket sock = new Socket(host,port);
- InputStream inp = sock.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(inp));
- PrintWriter writer = new PrintWriter(new BufferedOutputStream(sock.getOutputStream())) {
- public void println() {
- try {
- out.write("\r\n");
- out.flush();
- } catch(IOException ex) {
- throw new RuntimeException(ex.toString());
+
+ private final ProtocolSimulatorOptions params;
+ private final Perl5Util perl = new Perl5Util();
+
+ /** @params options Options to control Protocol Simulator run */
+ public ProtocolSimulator(ProtocolSimulatorOptions options) {
+ this.params = options;
+ }
+
+ /** start simulation run */
+ public void start() throws IOException, InterruptedException {
+ System.out.println("Testing Against Server "+params.host+":"+params.port);
+ System.out.println("Simulation Template: "+params.template);
+ System.out.println("Number Of Workers="+params.workers+
+ ", Iterations Per Worker="+params.iterations);
+
+ // create protocol simulation commands and fire simulation.
+ Command simulation = getSimulationCmd(params.template);
+ runSimulation(simulation);
+ }
+
+ /** represents a single protocol interaction */
+ private static interface Command {
+ /**
+ * do something. Either write to or read from and validate
+ * @param reader Input from protocol server
+ * @param writer Output to protocol Server
+ */
+ public void execute(BufferedReader reader,PrintWriter writer) throws Throwable;
+ }
+
+ // ---- methods to fire off simulation ---------
+
+ /** Starts simulation threads. Blocks till simulation threads are done. */
+ private void runSimulation(final Command simulation)
+ throws InterruptedException
+ {
+ Thread[] t = new Thread[params.workers];
+ for ( int i = 0 ; i < t.length ; i++ ) {
+ final int workerID = i;
+ t[i] = new Thread("protocol-simulator-"+workerID) {
+ public void run() {
+ try {
+ for ( int i = 0 ; i < params.iterations ; i++ )
+ runSingleSimulation(simulation);
+ } catch(Throwable t) {
+ System.out.println("Terminating "+workerID+" Reason="+t.toString());
+ t.printStackTrace();
+ }
}
- }
- };
-
-
+ };
+ }
+ for ( int i = 0 ; i < t.length ; i++ )
+ t[i].start();
+ for ( int i = 0 ; i < t.length ; i++ )
+ t[i].join();
+ }
+
+ /** run single simulation. called per thread per interation */
+ private void runSingleSimulation(Command simulation) throws Throwable {
+ Socket sock = null;
+ try {
+ sock = new Socket(params.host,params.port);
+ InputStream inp = sock.getInputStream();
+ OutputStream out = sock.getOutputStream();
+ BufferedReader reader = new BufferedReader(new InputStreamReader(inp));
+ PrintWriter writer = new CRLFPrintWriter(new BufferedOutputStream(out),true);
+ simulation.execute(reader,writer);
+ } finally {
+ if ( sock != null )
+ try { sock.close(); } catch(Throwable t) { }
+ }
+ }
+
+ // --------- helper methods --------------
+
+ /** read template and convert into single protocol interaction command
+ */
+ private Command getSimulationCmd(String template)
+ throws IOException
+ {
BufferedReader templateReader = new BufferedReader(new FileReader(template));
try {
- Line[] line = readTemplate(templateReader,reader,writer);
- for ( int i = 0 ; i < line.length ; i++ ) {
- try {
- line[i].process();
- } catch(Throwable t) {
- System.out.println("Failed on line: "+i);
- t.printStackTrace();
- break;
- }
- }
+ return getSimulationCmd(templateReader);
} finally {
- // try your be to cleanup.
+ // try your best to cleanup.
try { templateReader.close(); } catch(Throwable t) { }
- try { sock.close(); } catch(Throwable t) { }
}
}
- /** represents a single line of protocol interaction */
- private static interface Line {
- /** do something with the line. Either send or validate */
- public void process() throws IOException;
- }
-
-
- /** read template and convert into protocol interaction line
- * elements */
- private static Line[] readTemplate(BufferedReader templateReader,
- final BufferedReader reader,
- final PrintWriter writer) throws Exception
+ /** read template and convert into a single protocol interaction command.
+ */
+ private Command getSimulationCmd(BufferedReader templateReader)
+ throws IOException
{
- List list = new ArrayList();
+ // convert template lines into Protocol interaction commands.
+ final List list = new ArrayList();
while ( true ) {
final String templateLine = templateReader.readLine();
if ( templateLine == null )
@@ -135,16 +182,20 @@
if ( templateLine.startsWith(COMMENT_TAG) )
continue;
if ( templateLine.startsWith(CLIENT_TAG) ) {
- list.add(new Line() {
+ list.add(new Command() {
// just send the client data
- public void process() {
+ public void execute(BufferedReader reader,PrintWriter writer)
+ throws Throwable
+ {
writer.println(templateLine.substring(CLIENT_TAG_LEN));
}
});
} else if ( templateLine.startsWith(SERVER_TAG) ) {
- list.add(new Line() {
+ list.add(new Command() {
// read server line and validate
- public void process() throws IOException {
+ public void execute(BufferedReader reader,PrintWriter writer)
+ throws Throwable
+ {
String line = reader.readLine();
String pattern = templateLine.substring(SERVER_TAG_LEN);
System.out.println(pattern+":"+line);
@@ -155,8 +206,24 @@
}
});
} else
- throw new Exception("Invalid template line: "+templateLine);
+ throw new IOException("Invalid template line: "+templateLine);
}
- return (Line[])list.toArray(new Line[0]);
+
+ // aggregate all commands into a single simulation command
+ Command simulation = new Command() {
+ public void execute(BufferedReader reader,PrintWriter writer)
+ throws Throwable
+ {
+ for ( int i = 0 ; i < list.size() ; i++ ) {
+ try {
+ ((Command)list.get(i)).execute(reader,writer);
+ } catch(Throwable t) {
+ System.out.println("Failed on line: "+i);
+ throw t;
+ }
+ }
+ }
+ };
+ return simulation;
}
}
1.1 jakarta-james/tests/src/java/org/apache/james/testing/ProtocolSimulatorOptions.java
Index: ProtocolSimulatorOptions.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package org.apache.james.testing;
import org.apache.avalon.excalibur.cli.CLArgsParser;
import org.apache.avalon.excalibur.cli.CLOption;
import org.apache.avalon.excalibur.cli.CLOptionDescriptor;
import org.apache.avalon.excalibur.cli.CLUtil;
/**
* Process Protocol Simulator Options. This is required input to
* Protocol Simulator
*/
public class ProtocolSimulatorOptions {
// ------- options. See constructor ------
final String host;
final int port;
final String template;
final int iterations;
final int workers;
/**
* @param host server host to connect to
* @param port server port to connect to
* @param template Protocol simulation template file
* @param interations test iterations
* @param workers number of parallel threads
*/
public ProtocolSimulatorOptions(String host, int port,
String template,
int iterations,int workers)
{
this.host = host;
this.port = port;
this.template = template;
this.iterations = iterations;
this.workers = workers;
}
// --- process options ----
private static interface OptionTag {
char HELP = 'h';
char HOST = 's';
char PORT = 'p';
char TEMPLATE = 't';
char ITERATIONS = 'i';
char WORKERS = 'w';
} // interface OptionTag
private static final CLOptionDescriptor[] OPTIONS = {
new CLOptionDescriptor("server",
CLOptionDescriptor.ARGUMENT_REQUIRED,
OptionTag.HOST,
"Remote Server Host To Test Against"),
new CLOptionDescriptor("port",
CLOptionDescriptor.ARGUMENT_REQUIRED,
OptionTag.PORT,
"Remote Server Port To Test Against"),
new CLOptionDescriptor("template",
CLOptionDescriptor.ARGUMENT_REQUIRED,
OptionTag.TEMPLATE,
"Protocol Session Template"),
new CLOptionDescriptor("workers",
CLOptionDescriptor.ARGUMENT_REQUIRED,
OptionTag.WORKERS,
"Number Of Concurrent Simulations. Default Is 1."),
new CLOptionDescriptor("iterations",
CLOptionDescriptor.ARGUMENT_REQUIRED,
OptionTag.ITERATIONS,
"Number Of Protocol Simulations Iterations Per Worker. Default Is 1."),
new CLOptionDescriptor("help",
CLOptionDescriptor.ARGUMENT_OPTIONAL,
OptionTag.HELP,
"Usage Help"),
};
/**
* parse command line options
* @return Options or null if there has been parsing error
*/
static ProtocolSimulatorOptions getOptions(String[] args) {
// parse options.
CLArgsParser parser = new CLArgsParser( args, OPTIONS );
if( parser.getErrorString() != null ) {
System.out.println("Error: " + parser.getErrorString());
System.out.println("try --help option");
return null;
}
// set defaults
boolean help = false;
String host = null;
int port = -1;
String template = null;
int iterations = 1;
int workers = 1;
// collect options
CLOption[] option = (CLOption[])parser.getArguments()
.toArray(new CLOption[0]);
for( int i = 0; i < option.length; i++ ) {
CLOption opt = option[i];
switch(opt.getId()) {
case OptionTag.HELP:
help = true;
break;
case OptionTag.HOST:
host = opt.getArgument();
break;
case OptionTag.PORT:
port = Integer.parseInt(opt.getArgument());
break;
case OptionTag.TEMPLATE:
template = opt.getArgument();
break;
case OptionTag.ITERATIONS:
iterations = Integer.parseInt(opt.getArgument());
break;
case OptionTag.WORKERS:
workers = Integer.parseInt(opt.getArgument());
break;
default:
help = true;
break;
}
}
if ( host == null || port == -1 || template == null || help ) {
System.out.println(CLUtil.describeOptions(OPTIONS));
return null;
}
return new ProtocolSimulatorOptions(host,port,template,iterations,workers);
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>