From 1704541cc9c68589a043429dfd9221dec8cae35e Mon Sep 17 00:00:00 2001 From: "julian.filter" Date: Thu, 9 Apr 2020 17:05:06 +0200 Subject: [PATCH 1/3] fix util.errRun --- mininet/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mininet/util.py b/mininet/util.py index c4a5819..3f6c31c 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: From d5b4aa829b1ec92c57f60d1eec01e4d42bd279d4 Mon Sep 17 00:00:00 2001 From: "julian.filter" Date: Thu, 9 Apr 2020 17:10:52 +0200 Subject: [PATCH 2/3] Improve util.pmonitor --- mininet/util.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mininet/util.py b/mininet/util.py index 3f6c31c..d724096 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -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 From 5f69bf0adeee796ce2d66b605f1e65c67bc791bb Mon Sep 17 00:00:00 2001 From: "julian.filter" Date: Thu, 9 Apr 2020 18:16:13 +0200 Subject: [PATCH 3/3] Add unit tests for util --- mininet/test/test_util.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 mininet/test/test_util.py 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()