diff --git a/mininet/test/test_util.py b/mininet/test/test_util.py new file mode 100755 index 0000000..66a3128 --- /dev/null +++ b/mininet/test/test_util.py @@ -0,0 +1,39 @@ +#!/usr/bin/env python + +"""Package: mininet + Test functions defined in mininet.util.""" + +import unittest + +from mininet.util import quietRun + +class testQuietRun( unittest.TestCase ): + """Test quietRun that runs a command and returns its merged output from + STDOUT and STDIN""" + + @staticmethod + def getEchoCmd( n ): + "Return a command that will print n characters" + return "echo -n " + "x" * n + + def testEmpty( self ): + "Run a command that prints nothing" + output = quietRun(testQuietRun.getEchoCmd( 0 ) ) + self.assertEqual( 0, len( output ) ) + + def testOneRead( self ): + """Run a command whose output is entirely read on the first call if + each call reads at most 1024 characters + """ + for n in [ 42, 1024 ]: + output = quietRun( testQuietRun.getEchoCmd( n ) ) + self.assertEqual( n, len( output ) ) + + def testMultipleReads( self ): + "Run a command whose output is not entirely read on the first read" + for n in [ 1025, 4242 ]: + output = quietRun(testQuietRun.getEchoCmd( n ) ) + self.assertEqual( n, len( output ) ) + +if __name__ == "__main__": + unittest.main() diff --git a/mininet/util.py b/mininet/util.py index c4a5819..d724096 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -157,7 +157,7 @@ def errRun( *cmd, **kwargs ): for fd, event in readable: f = fdToFile[ fd ] decoder = fdToDecoder[ fd ] - if event & POLLIN: + if event & ( POLLIN | POLLHUP ): data = decoder.decode( f.read( 1024 ) ) if echo: output( data ) @@ -169,7 +169,7 @@ def errRun( *cmd, **kwargs ): err += data if data == '': errDone = True - else: # POLLHUP or something unexpected + else: # something unexpected if f == popen.stdout: outDone = True elif f == popen.stderr: @@ -451,7 +451,7 @@ def pmonitor(popens, timeoutms=500, readline=True, fd = popen.stdout.fileno() fdToHost[ fd ] = host fdToDecoder[ fd ] = getincrementaldecoder() - poller.register( fd, POLLIN | POLLHUP ) + poller.register( fd, POLLIN ) flags = fcntl( fd, F_GETFL ) fcntl( fd, F_SETFL, flags | O_NONBLOCK ) while popens: @@ -461,7 +461,7 @@ def pmonitor(popens, timeoutms=500, readline=True, host = fdToHost[ fd ] decoder = fdToDecoder[ fd ] popen = popens[ host ] - if event & POLLIN or event & POLLHUP: + if event & ( POLLIN | POLLHUP ): while True: try: f = popen.stdout