public inbox for frysk-cvs@sourceware.org
help / color / mirror / Atom feed
* [SCM]  master: Replace Expect.expect(TIMEOUT, ...) with Expect.timeout(TIMEOUT).expect(...).
@ 2008-01-25 19:06 cagney
  0 siblings, 0 replies; only message in thread
From: cagney @ 2008-01-25 19:06 UTC (permalink / raw)
  To: frysk-cvs

The branch, master has been updated
       via  e0ae00fafe3d0b780d7f0aa82e2ff64246921cb4 (commit)
      from  66747abdd1148466ac14af8e1c2b2920f65978ca (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email.

- Log -----------------------------------------------------------------
commit e0ae00fafe3d0b780d7f0aa82e2ff64246921cb4
Author: Andrew Cagney <cagney@redhat.com>
Date:   Fri Jan 25 14:05:21 2008 -0500

    Replace Expect.expect(TIMEOUT, ...) with Expect.timeout(TIMEOUT).expect(...).
    
    frysk-core/frysk/hpd/ChangeLog
    2008-01-25  Andrew Cagney  <cagney@redhat.com>
    
    	* TestDisassemblerCommand.java: Do not use explicit timeouts.
    	* TestHpdTestHarness.java: Ditto.
    	* TestKillCommand.java: Ditto.
    	* TestLoadCommand.java: Ditto.
    
    frysk-sys/frysk/expunit/ChangeLog
    2008-01-25  Andrew Cagney  <cagney@redhat.com>
    
    	* Expect.java: Split into Child.java.
    	(setTimeoutSeconds(int)): Delete.
    	(timeout(int)): New.
    	(getTimeout()): Replace getTimeoutSeconds().
    	(expectMilliseconds(long,Match[])): Delete.
    	(expect(long,Match[])): Delete.
    	(expect(long,Match)): Delete.
    	(expect(long,String)): Delete.
    	(expect(long)): Delete.
    	(expect()): Delete.
    	* Child.java: New.
    	* TestExpect.java: Update.
    
    frysk-sys/frysk/testbed/ChangeLog
    2008-01-25  Andrew Cagney  <cagney@redhat.com>
    
    	* TestTearDownExpect.java: Update to match Expect rewrite.

-----------------------------------------------------------------------

Summary of changes:
 frysk-core/frysk/hpd/ChangeLog                    |    7 +
 frysk-core/frysk/hpd/TestDisassemblerCommand.java |   16 +-
 frysk-core/frysk/hpd/TestHpdTestHarness.java      |   20 +-
 frysk-core/frysk/hpd/TestKillCommand.java         |   28 ++--
 frysk-core/frysk/hpd/TestLoadCommand.java         |    4 +-
 frysk-sys/frysk/expunit/ChangeLog                 |   15 ++
 frysk-sys/frysk/expunit/Child.java                |  192 ++++++++++++++++
 frysk-sys/frysk/expunit/Expect.java               |  241 ++++-----------------
 frysk-sys/frysk/expunit/TestExpect.java           |   20 +-
 frysk-sys/frysk/testbed/ChangeLog                 |    4 +
 frysk-sys/frysk/testbed/TestTearDownExpect.java   |    3 +-
 11 files changed, 304 insertions(+), 246 deletions(-)
 create mode 100644 frysk-sys/frysk/expunit/Child.java

First 500 lines of diff:
diff --git a/frysk-core/frysk/hpd/ChangeLog b/frysk-core/frysk/hpd/ChangeLog
index d521c46..8d16747 100644
--- a/frysk-core/frysk/hpd/ChangeLog
+++ b/frysk-core/frysk/hpd/ChangeLog
@@ -1,3 +1,10 @@
+2008-01-25  Andrew Cagney  <cagney@redhat.com>
+
+	* TestDisassemblerCommand.java: Do not use explicit timeouts.
+	* TestHpdTestHarness.java: Ditto.
+	* TestKillCommand.java: Ditto.
+	* TestLoadCommand.java: Ditto.
+
 2008-01-24  Nurdin Premji  <npremji@redhat.com>
 
 	* TaskData.java (toPrint): Renamed to ...
diff --git a/frysk-core/frysk/hpd/TestDisassemblerCommand.java b/frysk-core/frysk/hpd/TestDisassemblerCommand.java
index 33f2f36..f3a9e4d 100644
--- a/frysk-core/frysk/hpd/TestDisassemblerCommand.java
+++ b/frysk-core/frysk/hpd/TestDisassemblerCommand.java
@@ -41,18 +41,13 @@ package frysk.hpd;
 
 import lib.opcodes.Disassembler;
 
-public class TestDisassemblerCommand
-    extends TestLib
-{
+public class TestDisassemblerCommand extends TestLib {
     public void testHpdDisassemble() {
 	if (unsupported("disassembler", !Disassembler.available()))
 	    return;
 	e = HpdTestbed.attachXXX("hpd-c");
 	e.send("disassemble\n");
-	e.expect(5, "\\*.*test.*\n(.*\n)*" + prompt);
-	e.send("quit\n");
-	e.expect("Quitting...");
-	e.close();
+	e.expect("\\*.*test.*\n(.*\n)*" + prompt);
     }
     
     public void testDisassembleRange() {
@@ -60,10 +55,7 @@ public class TestDisassemblerCommand
 	    return;
 	e = HpdTestbed.attachXXX("hpd-c");
 	e.send("disassemble 0x804860f  0x80487ea\n");
-	e.expect(5, " 0x804860f.*\n");
-	e.expect(5, " 0x80487ea.*\n" + prompt);
-	e.send("quit\n");
-	e.expect("Quitting...");
-	e.close();
+	e.expect(" 0x804860f.*\n");
+	e.expect(" 0x80487ea.*\n" + prompt);
     }
 }
diff --git a/frysk-core/frysk/hpd/TestHpdTestHarness.java b/frysk-core/frysk/hpd/TestHpdTestHarness.java
index 8c4eea5..41c04a5 100644
--- a/frysk-core/frysk/hpd/TestHpdTestHarness.java
+++ b/frysk-core/frysk/hpd/TestHpdTestHarness.java
@@ -49,22 +49,22 @@ public class TestHpdTestHarness extends TestLib {
 	e = new HpdTestbed();
 	e.send("load " + Config.getPkgLibFile("funit-threads-looper").getPath()
 		+ "\n");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Loaded executable file*");
 	e.send("run\n");
-	e.expect(5, "Attached to process*");
+	e.expect("Attached to process*");
 	e.send("go\n");
-	e.expect(5, "Running process*");
+	e.expect("Running process*");
 	e.send("kill\n");
-	e.expect(5, "Killing process*");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Killing process*");
+	e.expect("Loaded executable file*");
 	e.send("run\n");
-	e.expect(5, "Attached to process*");
+	e.expect("Attached to process*");
 	e.send("go\n");
-	e.expect(5, "Running process*");
+	e.expect("Running process*");
 	e.send("kill\n");
-	e.expect(5, "Killing process*");
-	e.expect(5, "Loaded executable file*");
-	e.expect(5, "quit\n");
+	e.expect("Killing process*");
+	e.expect("Loaded executable file*");
+	e.expect("quit\n");
 	e.expect("Quitting...");
     }
 }
diff --git a/frysk-core/frysk/hpd/TestKillCommand.java b/frysk-core/frysk/hpd/TestKillCommand.java
index fd5571d..0d1decd 100644
--- a/frysk-core/frysk/hpd/TestKillCommand.java
+++ b/frysk-core/frysk/hpd/TestKillCommand.java
@@ -62,14 +62,14 @@ public class TestKillCommand extends TestLib {
 	e = new HpdTestbed();
 	e.send("load " + Config.getPkgLibFile("funit-threads-looper").getPath()
 		+ "\n");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Loaded executable file*");
 	e.send("run\n");
-	e.expect(5, "Attached to process*");
+	e.expect("Attached to process*");
 	e.send("go\n");
-	e.expect(5, "Running process*");
+	e.expect("Running process*");
 	e.send("kill\n");
-	e.expect(5, "Killing process*");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Killing process*");
+	e.expect("Loaded executable file*");
 	/* Make sure you run again to make sure all has been cleaned up properly
 	 * from the last run.
 	 */
@@ -81,17 +81,17 @@ public class TestKillCommand extends TestLib {
          *  test to fail for no good reason.  A bug will be filed on this and the
          *  lines can be uncommented when fixed. */
 	/*
-	e.send(5, "run", "Attached to process*");
-	e.expect(5, "Attached to process*");
+	e.send("run", "Attached to process*");
+	e.expect("Attached to process*");
 	e.send("go\n");
-	e.expect(5, "Running process*");
+	e.expect("Running process*");
 	e.send("kill\n");
-	e.expect(5, "Killing process*");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Killing process*");
+	e.expect("Loaded executable file*");
 	/* Make sure we can quit gracefully  */
 	/*
 	e.send("quit\n");
-	e.expect(5, "Quitting*"); */
+	e.expect("Quitting*"); */
 	e.close();
     }
     
@@ -103,11 +103,11 @@ public class TestKillCommand extends TestLib {
 	e = new HpdTestbed();
 	e.send("load " + Config.getPkgLibFile("funit-threads-looper").getPath()
 		+ "\n");
-	e.expect(5, "Loaded executable file*");
+	e.expect("Loaded executable file*");
 	e.send("run\n");
-	e.expect(5, "Attached to process*");
+	e.expect("Attached to process*");
 	e.send("kill\n");
-	e.expect(5, "Killing process*");
+	e.expect("Killing process*");
 	//e.send("quit\n");
 	//e.expect("Quitting*");
 	e.close();
diff --git a/frysk-core/frysk/hpd/TestLoadCommand.java b/frysk-core/frysk/hpd/TestLoadCommand.java
index 0cb70ff..b2abe24 100644
--- a/frysk-core/frysk/hpd/TestLoadCommand.java
+++ b/frysk-core/frysk/hpd/TestLoadCommand.java
@@ -51,7 +51,7 @@ public class TestLoadCommand extends TestLib {
 	e = new HpdTestbed();
 	e.send("load " + Config.getPkgDataFile("test-exe-x86").getPath()
 		+ "\n");
-	e.expect(5, "Loaded executable file.*");
+	e.expect("Loaded executable file.*");
 	e.send("quit\n");
 	e.expect("Quitting...");
 	e.close();
@@ -61,7 +61,7 @@ public class TestLoadCommand extends TestLib {
 	e = new HpdTestbed();
 	e.send("load " + Config.getPkgDataFile("test-exe-x86").getPath()
 		+ "foo\n");
-	e.expect(5, "File does not exist or is not readable*");
+	e.expect("File does not exist or is not readable*");
 	e.send("quit\n");
 	e.expect("Quitting...");
 	e.close();
diff --git a/frysk-sys/frysk/expunit/ChangeLog b/frysk-sys/frysk/expunit/ChangeLog
index 0ff37e8..6012b5b 100644
--- a/frysk-sys/frysk/expunit/ChangeLog
+++ b/frysk-sys/frysk/expunit/ChangeLog
@@ -1,3 +1,18 @@
+2008-01-25  Andrew Cagney  <cagney@redhat.com>
+
+	* Expect.java: Split into Child.java.
+	(setTimeoutSeconds(int)): Delete.
+	(timeout(int)): New.
+	(getTimeout()): Replace getTimeoutSeconds().
+	(expectMilliseconds(long,Match[])): Delete.
+	(expect(long,Match[])): Delete.
+	(expect(long,Match)): Delete.
+	(expect(long,String)): Delete.
+	(expect(long)): Delete.
+	(expect()): Delete.
+	* Child.java: New.
+	* TestExpect.java: Update.
+
 2008-01-22  Andrew Cagney  <cagney@redhat.com>
 
 	* Expect.java (expectTermination(Signal)): New.
diff --git a/frysk-sys/frysk/expunit/Child.java b/frysk-sys/frysk/expunit/Child.java
new file mode 100644
index 0000000..aacdacf
--- /dev/null
+++ b/frysk-sys/frysk/expunit/Child.java
@@ -0,0 +1,192 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2007, 2008, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.expunit;
+
+import frysk.sys.Errno;
+import frysk.sys.ProcessIdentifier;
+import frysk.sys.PseudoTerminal;
+import frysk.sys.Signal;
+import frysk.rsl.Log;
+import frysk.rsl.Level;
+
+/**
+ * The child being waited on.
+ */
+
+class Child {
+    static protected Log fine = Log.get(Child.class, Level.FINE);
+    static private Log finest = Log.get(Child.class, Level.FINEST);
+
+    private final PseudoTerminal child;
+    private final ProcessIdentifier pid;
+
+    /**
+     * Create an expect instance running the specified program args[0]
+     * and args.
+     */
+    Child(String[] args) {
+	child = new PseudoTerminal();
+	pid = child.addChild(args);
+	fine.log(this, "new", child, "pid", pid, "args", args);
+    }
+
+    public String toString() {
+	return child.toString();
+    }
+
+    /**
+     * Clean up.
+     *
+     * XXX: This drains all outstanding WAITPID events, and SIGCHLD
+     * events.
+     */
+    public void close() {
+	if (child != null) {
+	    fine.log(this, "close child");
+	    child.close();
+	}
+	if (pid != null) {
+	    fine.log(this, "close pid");
+	    try {
+		pid.kill();
+	    } catch (Errno e) {
+		// Toss it, as cleanup.
+	    }
+	    pid.blockingDrain();
+	}
+	Signal.CHLD.drain();
+    }
+
+    /**
+     * Finalizer, also tries to clean up.
+     */
+    public void finalize() {
+	close ();
+    }
+
+    /**
+     * Return the Process ID of the child.
+     */
+    ProcessIdentifier getPid() {
+	return pid;
+    }
+
+    /**
+     * Buffer containing all the unmatched output from the child.
+     * It's a String and not a StringBuffer as after every change the
+     * buffer gets re-converted to a String anyway.
+     */
+    private String output = new String ();
+    /**
+     * Detected end-of-file, or hang-up.
+     */
+    private boolean eof = false;
+    
+    /**
+     * Send "string" to the child process.
+     */
+    void send(String string) {
+	fine.log(this, "send", (Object)string);
+	byte[] bytes = string.getBytes();
+	child.write(bytes, 0, bytes.length);
+    }
+
+    /**
+     * Expect one of the specified patterns; throw TimeoutException if
+     * timeoutSecond expires; throw EofException if end-of-file is
+     * encountered.
+     * 
+     * This is package visible so that Expect can use it, but no one
+     * else.
+     */
+    void expectMilliseconds(long timeoutMilliseconds, Match[] matches) {
+	final long endTime = (System.currentTimeMillis()
+			      + timeoutMilliseconds);
+	fine.log(this, "expect", matches,
+		 "timeout [milliseconds]", (int)timeoutMilliseconds);
+	while (true) {
+	    for (int i = 0; i < matches.length; i++) {
+		Match p = matches[i];
+		finest.log(this, "find", p, "in", (Object) output);
+		if (p.find(output)) {
+		    fine.log(this, "match", (Object) p.group(),
+			     "with", p);
+		    p.execute();
+		    // Remove everything up to and including what
+		    // matched.
+		    if (p.end() >= 0)
+			output = output.substring(p.end());
+		    return;
+		}
+	    }
+	    if (eof) {
+		fine.log(this, "match EOF");
+		throw new EndOfFileException(matches, output);
+	    }
+	    long timeRemaining = endTime - System.currentTimeMillis();
+	    if (timeRemaining <= 0) {
+		fine.log(this, "match TIMEOUT");
+		throw new TimeoutException(timeoutMilliseconds / 1000, matches,
+			output);
+	    }
+
+	    finest.log(this, "poll for [milliseconds]", timeRemaining);
+	    if (child.ready(timeRemaining)) {
+		byte[] bytes = new byte[100];
+		int nr = child.read(bytes, 0, bytes.length);
+		switch (nr) {
+		case -1:
+		    finest.log(this, "poll -> EOF");
+		    eof = true;
+		    break;
+		case 0:
+		    finest.log(this, "poll -> no data!");
+		    break;
+		default:
+		    String read = new String(bytes, 0, nr);
+		    output = output + read;
+		    finest.log(this, "poll -> ", (Object) read, "giving",
+			    (Object) output);
+		    break;
+		}
+	    }
+	}
+    }
+}
diff --git a/frysk-sys/frysk/expunit/Expect.java b/frysk-sys/frysk/expunit/Expect.java
index 1e4809c..fd3072a 100644
--- a/frysk-sys/frysk/expunit/Expect.java
+++ b/frysk-sys/frysk/expunit/Expect.java
@@ -39,9 +39,7 @@
 
 package frysk.expunit;
 
-import frysk.sys.Errno;
 import frysk.sys.ProcessIdentifier;
-import frysk.sys.PseudoTerminal;
 import frysk.sys.Signal;
 import frysk.rsl.Log;
 import frysk.rsl.Level;
@@ -56,23 +54,24 @@ import java.io.File;
  * an exit status.
  */
 
-public class Expect
-{
-    static protected Log fine = Log.get(Expect.class, Level.FINE);
-    static private Log finest = Log.get(Expect.class, Level.FINEST);
+public class Expect {
+    private static final Log fine = Log.get(Expect.class, Level.FINE);
 
-    private final PseudoTerminal child = new PseudoTerminal ();
-    private ProcessIdentifier pid = null;
+    private final Child child;
+    private final long timeoutMilliseconds;
+    private Expect(Child child, long timeoutMilliseconds) {
+	this.child = child;
+	this.timeoutMilliseconds = timeoutMilliseconds;
+    }
 
     /**
      * Create an expect instance running the specified program args[0]
      * and args.
      */
     public Expect(String[] args) {
-	pid = child.addChild(args);
-	fine.log(this, "new", child, "pid", pid, "args", args);
+	this(new Child(args), defaultTimeoutSeconds * 1000);
+	fine.log(this, "new", child, "args", args);
     }
-
     /**
      * Create an expect instance running PROGRAM with no arguments.
      * 
@@ -82,16 +81,14 @@ public class Expect
     public Expect(File program) {
 	this(new String[] { program.getAbsolutePath() });
     }
-
     /**
      * Using <tt>bash</tt, create an expect instance running the
      * specified command.  Since the command is invoked using
      * <tt>bash</tt> it will be expanded using that shells variable
      * and globbing rules.
      */
-    public Expect (String command)
-    {
-	this (new String[] { "/bin/bash", "-c", command });
+    public Expect(String command) {
+	this(new String[] { "/bin/bash", "-c", command });
     }
 
     /**
@@ -101,18 +98,15 @@ public class Expect
      * events.
      */
     public void close() {
-	if (pid != null) {
-	    fine.log(this, "close");
+	if (child != null) {
 	    child.close();
-	    try {
-		pid.kill();
-	    } catch (Errno e) {
-		// Toss it, as cleanup.
-	    }
-	    pid.blockingDrain();
 	}
-	pid = null;
-	Signal.CHLD.drain();
+    }
+    /**
+     * Finalizer, also tries to clean up.
+     */
+    public void finalize() {
+	close();
     }
 
     /**
@@ -123,135 +117,37 @@ public class Expect
      * Set the global default timeout (in seconds).  Any expect
      * classes inherit this value.
      */
-    static public void setDefaultTimeoutSeconds (int defaultTimeoutSeconds)
-    {
+    static public void setDefaultTimeoutSeconds(int defaultTimeoutSeconds) {
 	Expect.defaultTimeoutSeconds = defaultTimeoutSeconds;
     }
 
     /**


hooks/post-receive
--
frysk system monitor/debugger


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2008-01-25 19:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-25 19:06 [SCM] master: Replace Expect.expect(TIMEOUT, ...) with Expect.timeout(TIMEOUT).expect(...) cagney

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).