aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjestin2006-08-02 16:23:18 +0000
committerjestin2006-08-02 16:23:18 +0000
commit1c5d4da0b5be2f8532c96e9bc477c34e5687ae19 (patch)
tree069cc6edd570f95bbefd7b1260a2beb9d4cda29b
parentf7d01e0337800bcb54fea096604457f900cbf976 (diff)
downloadivy-java-1c5d4da0b5be2f8532c96e9bc477c34e5687ae19.zip
ivy-java-1c5d4da0b5be2f8532c96e9bc477c34e5687ae19.tar.gz
ivy-java-1c5d4da0b5be2f8532c96e9bc477c34e5687ae19.tar.bz2
ivy-java-1c5d4da0b5be2f8532c96e9bc477c34e5687ae19.tar.xz
ibid
-rw-r--r--tests/AsyncAPI.java150
-rw-r--r--tests/StopStart.java30
2 files changed, 180 insertions, 0 deletions
diff --git a/tests/AsyncAPI.java b/tests/AsyncAPI.java
new file mode 100644
index 0000000..eca9ddc
--- /dev/null
+++ b/tests/AsyncAPI.java
@@ -0,0 +1,150 @@
+/**
+ * Ivy java Async API tester: bindAsyncMsg, sendAsyncMsg.
+ *
+ * @author Yannick Jestin <mailto:jestin@cena.fr>
+ *
+ * (c) CENA
+ *
+ * usage: java AsyncAPI -h
+ *
+ * this program tests the Asynchronous reception of messages ( each callback
+ * is performed in a separate threads ). It also exhibits the behaviour of the
+ * library with regards to concurrent connections ! To stress the test, try it
+ * with different JVM ( kaffe is especially hard to pass ), and on SMP
+ * machines ...
+ *
+ * changelog
+ *
+ * 1.2.6 : async sending seems utterly buggy ...
+ *
+ */
+import fr.dgac.ivy.*;
+import gnu.getopt.*;
+
+class AsyncAPI {
+
+ public static final int MSGSIZE = 10;
+ public static final int NBITER = 100;
+ public static final int DELAYMS = 1000;
+ public static final String HEADER = "ASYNCPACKET";
+ public static final String TOSUBSCRIBE = "^"+HEADER+"([0-9]*) (.*)";
+ public static final String RECEIVENAME = "MSreceive";
+ public static final String SENDNAME = "MSsend";
+ private static long epoch = System.currentTimeMillis();
+
+ private Ivy bus;
+ DelayAnswer delay;
+ int re;
+ private String name;
+ boolean verbose;
+ private int nbpacket;
+ private int count=0,total=0;
+ protected Integer truc = new Integer(0);
+
+ public AsyncAPI(int nb,String domain,int d, boolean v,boolean async) throws IvyException {
+ verbose=v;
+ nbpacket=nb;
+ name = "MSreceive";
+ bus = new Ivy(name,name+" Ready", null);
+ delay=new DelayAnswer(d);
+ if (async)
+ re = bus.bindAsyncMsg(TOSUBSCRIBE,delay);
+ else
+ re = bus.bindMsg(TOSUBSCRIBE,delay);
+ bus.start(domain);
+ }
+
+ private static java.text.DateFormat df = java.text.DateFormat.getTimeInstance();
+ private static String date() {
+ return "["+df.format(new java.util.Date())+"] ";
+ }
+
+ class DelayAnswer implements IvyMessageListener {
+ int delay;
+ public DelayAnswer(int delay) {
+ this.delay=delay;
+ }
+ public void receive(IvyClient ic, String[] args) {
+ synchronized(truc) {
+ count++;
+ if (verbose) {
+ System.out.println(date()+count+"/"+nbpacket+" packets received ("+args[0]+")");
+ total+=Integer.parseInt(args[0]);
+ }
+ if (count<nbpacket) return;
+ if (total==(((nbpacket+1)*nbpacket)/2)) {
+ System.out.println("receiver quitting the bus normally");
+ ic.sendDie("ok, bye");
+ bus.stop();
+ } else {
+ System.out.println("wrong count and total, hit ^C to exit");
+ //System.exit(-1);
+ }
+ }
+ try { Thread.sleep(delay); } catch (InterruptedException ie) { }
+ }
+ }
+
+ public static final String helpmsg = "usage: java "+SENDNAME+" [options]\n\t-b domain\n\t-r\t to enable async reception\n\t-x to enable async sending\n\t-l loop count (default "+NBITER+")\n\t-s msgsize\t int value (default "+MSGSIZE+")\n\t-d delay in milliseconds (default "+DELAYMS+")\n\t-q \tquiet\n\t-e\tsend bus stop at the end\n\t-h\thelp\n\n";
+ public static void main(String[] args) throws IvyException {
+ Getopt opt = new Getopt(SENDNAME,args,"b:l:d:s:xrqeh");
+ String domain=Ivy.getDomain(null); // default bus
+ int nb = NBITER, delay = DELAYMS, c, size = MSGSIZE;
+ boolean doasyncSend=false, doasyncBind=false, verbose=true, exit=false;
+ while ((c = opt.getopt()) != -1) switch (c) {
+ case 'b':
+ domain=opt.getOptarg();
+ break;
+ case 's':
+ size=Integer.parseInt(opt.getOptarg());
+ break;
+ case 'l':
+ nb=Integer.parseInt(opt.getOptarg());
+ break;
+ case 'd':
+ delay=Integer.parseInt(opt.getOptarg());
+ break;
+ case 'e':
+ exit=true;
+ break;
+ case 'q':
+ verbose=false;
+ break;
+ case 'x':
+ System.out.println("async sending is not robust enough. end of test.");
+ System.exit(-1);
+ doasyncSend=true;
+ break;
+ case 'r':
+ doasyncBind=true;
+ break;
+ case 'h':
+ default:
+ System.out.println(helpmsg);
+ System.exit(0);
+ }
+ System.out.println("bus:"+domain+" loop:"+nb+" delay:"+delay+" verbose:"+verbose+" asyncBind:"+
+ doasyncBind+" asyncSend:"+doasyncSend+" msgsize:"+size);
+
+ AsyncAPI receiver = new AsyncAPI(nb,domain,delay,verbose,doasyncBind);
+ Ivy mainbus = new Ivy(SENDNAME,SENDNAME+" Ready", null);
+ mainbus.start(domain);
+ if ((mainbus.waitForClient(RECEIVENAME,5000))==null) {
+ System.out.println(RECEIVENAME+" did not join the bus. Quitting");
+ System.exit(0);
+ }
+ System.out.println(RECEIVENAME+" is here, sending packets");
+ StringBuffer tosend = new StringBuffer(size);
+ for (int i=0;i<size;i++) tosend.append("a");
+ for (int i=1;i<=nb;i++) {
+ if (verbose) System.out.println(date()+"sending packet "+i);
+ mainbus.sendMsg(HEADER+i+" "+tosend.toString());
+ // mainbus.sendMsg(HEADER+i+" "+tosend.toString(),doasyncSend);
+ }
+ System.out.println(date()+"sender has sent all its packets, waiting for a die message");
+ // i won't stop the sender's bus here, otherwise the all the packet
+ // can still be unprocessed
+ // TODO regession test for Ivy.stop()
+ }
+
+}
diff --git a/tests/StopStart.java b/tests/StopStart.java
new file mode 100644
index 0000000..5962d27
--- /dev/null
+++ b/tests/StopStart.java
@@ -0,0 +1,30 @@
+import fr.dgac.ivy.* ;
+
+class StopStart {
+
+ Ivy bus;
+
+ public static void main(String[] args) throws IvyException {
+ String domain=Ivy.getDomainArgs("StopStartTest",args);
+ new StopStart(domain);
+ }
+
+ public StopStart(String domain) throws IvyException {
+ int n=1;
+ bus = new Ivy("StopStart","StopStart ready", null);
+ System.out.println("--------------- starting bus");
+ bus.start(domain);
+ System.out.println("--------------- sleeping "+n+" seconds");
+ try { Thread.sleep(n*1000); } catch (InterruptedException ie) { }
+ System.out.println("--------------- stopping bus");
+ bus.stop();
+ System.out.println("--------------- restarting bus");
+ bus.start(domain);
+ System.out.println("sleeping "+n+" seconds");
+ try { Thread.sleep(n*1000); } catch (InterruptedException ie) { }
+ System.out.println("--------------- restopping bus");
+ bus.stop();
+ System.out.println("--------------- good bye, program should exit now");
+ }
+
+}