Re: [Pyunit-interest] PyUnit hangs when I do the following absurdly complicated thing
Brought to you by:
purcell
From: Steve P. <ste...@ya...> - 2004-06-30 11:05:38
|
Pat, Without trying it out for myself, I'm pretty sure your problem lies in the= =20 'displayOuts ' static method, and the fact that you are trying to read the= =20 child process' stdout fully before moving on to reading its stderr. When the child process produces output, it goes into a buffer that, when=20 full, must be read (emptied) by the parent process before the child can=20 continue to execute. The result of read()-ing from stdout is only '-1' if the child process will= =20 certainly produce no more output on that stream, ie. the child process has= =20 exited. What is happening, I think, is that the stderr buffer is filling up with a= =20 large stack-trace, and the child process will not continue and exit until=20 that has been read. So, reading from stdout blocks the parent process until= =20 stderr has been read. Note that when a test passes, the volume of stderr=20 output is much less, so you do not see the blocking behaviour in that case. The correct way to read both output streams of the child process is to copy= =20 them to System.out in parallel threads, ie. change the first loop to: InputStream in =3D child.getInputStream(); Thread t =3D new Thread() { public void run() { try { int c; while ((c =3D in.read()) !=3D -1) { System.out.print((char)c); } in.close(); } catch ( IOException e ) { throw new RuntimeException(e); } } }; t.start(); then, after reading stderr as before, do t.join(); Best wishes, =2DSteve On Tuesday 29 June 2004 21:51, Pat McGee wrote: > When I call a PyUnit program from a Java program on Windows XP, and the > PyUnit program trips an assertion, the Python program hangs. I don't > know what to try next. Anyone have any suggestions for how I could > learn more? > > I wrote a short Java program (attachment 1) that uses Runtime to run a > Python program. The specific call is: > Process child =3D Runtime.getRuntime().exec ("python TripAssert.py", > null, workDir); > > The Python program (attachment 2) imports unittest and then creates a > test case. The test case has a single line, which is an assert. > > When I run the Python program from the command line, it always works, > whether the assert is true or false. In either case, I get what I > expect. > > When I run the Java program that runs the Python program, it will run > correctly if the assert is true (sample output 1). But, when the assert > is false, the program hangs (sample output 2). Sample output 3 shows > the output when the assert is false and the Python program is run > directly. > > I thought it might be running out of memory. I=92ve got 256 Mb. But, the > Task Manager says there=92s over 100 Mb available. > > I=92m pretty new to Windows programming (I=92m an old Unix programmer), a= nd > I don=92t know how to figure out what=92s happening. What tools might I u= se > to try to find this? How can I find out what a process is waiting on? > Anyone seen anything like this? > Versions: > =95Python 2.3 > =95PyUnit 1.4.1 > =95Java j2sdk1.4.2_04 > =95Windows XP Pro Version 5.1, Service Pack 1 > --- Attachment 1: Java program ------- > import java.io.File; > import java.io.InputStream; > > public class TestCallPython { > > public static void main(String[] args) throws Exception { > System.out.println ("TestCallPython\n"); > File workDir =3D new File ("\\Documents and settings\\a0868072\\my > documents\\work\\workspace\\TestCallPython"); > Process child =3D Runtime.getRuntime().exec ("python TripAssert.py", > null, workDir); > > displayOuts (child); > } > > public static void displayOuts (Process child) throws Exception { > System.out.println ("stdout"); > InputStream in =3D child.getInputStream(); > int c; > while ((c =3D in.read()) !=3D -1) { > System.out.print((char)c); > } > in.close(); > System.out.println ("\nstderr"); > > InputStream er =3D child.getErrorStream(); > while ((c =3D er.read()) !=3D -1) { > System.out.print((char)c); > } > er.close(); > System.out.println ("end"); > } > } > --- Attachment 2: Python program ------- > import unittest > > class LoggingTestCase (unittest.TestCase): > def test1Assert1 (self): > #self.assertEqual (1, 2) > self.assertEqual (1, 1) > > > suite =3D unittest.makeSuite(LoggingTestCase,'test') > > if __name__ =3D=3D "__main__": > unittest.main() > > --- Sample output 1: when assertEqual (1, 1) compiled in ------- > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython>java TestCallPython > TestCallPython > > stdout > > stderr > . > ---------------------------------------------------------------------- > Ran 1 test in 0.000s > > OK > end > > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython> > > --- Sample output 2: when assertEqual (1, 2) compiled in, from Java > ------- > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython>java TestCallPython > TestCallPython > > stdout > > {Note: I waited about 10 seconds, then hit ^C here.} > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython> > > --- Sample output 3: when assertEqual (1, 2) compiled in, from Python > ------- > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython>python TripAssert.py > F > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > FAIL: test1Assert1 (__main__.LoggingTestCase) > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "TripAssert.py", line 5, in test1Assert1 > self.assertEqual (1, 2) > File "c:\python23\lib\unittest.py", line 302, in failUnlessEqual > raise self.failureException, \ > AssertionError: 1 !=3D 2 > > ---------------------------------------------------------------------- > Ran 1 test in 0.000s > > FAILED (failures=3D1) > > C:\Documents and Settings\a0868072\My > Documents\work\workspace\TestCallPython> > > --- > Pat McGee > Ph.D. Student, Computer Sciences Department, Florida Institute of > Technology > Treasurer, Association for Software Testing, Inc. > (http://AssociationForSoftwareTesting.org) > Editor, CS Department Tech Report Library (http://cs.fit.edu/~tr) > > jpm...@cs... > http://blackbox.cs.fit.edu/blog/pat |