Merge pull request #938 from JuFil/fix-poll

Fix usage of poll in mininet.utils.errRun
This commit is contained in:
lantz
2020-04-15 18:13:39 -07:00
committed by GitHub
2 changed files with 43 additions and 4 deletions
+39
View File
@@ -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()
+4 -4
View File
@@ -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