Merge pull request #938 from JuFil/fix-poll
Fix usage of poll in mininet.utils.errRun
This commit is contained in:
Executable
+39
@@ -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
@@ -157,7 +157,7 @@ def errRun( *cmd, **kwargs ):
|
|||||||
for fd, event in readable:
|
for fd, event in readable:
|
||||||
f = fdToFile[ fd ]
|
f = fdToFile[ fd ]
|
||||||
decoder = fdToDecoder[ fd ]
|
decoder = fdToDecoder[ fd ]
|
||||||
if event & POLLIN:
|
if event & ( POLLIN | POLLHUP ):
|
||||||
data = decoder.decode( f.read( 1024 ) )
|
data = decoder.decode( f.read( 1024 ) )
|
||||||
if echo:
|
if echo:
|
||||||
output( data )
|
output( data )
|
||||||
@@ -169,7 +169,7 @@ def errRun( *cmd, **kwargs ):
|
|||||||
err += data
|
err += data
|
||||||
if data == '':
|
if data == '':
|
||||||
errDone = True
|
errDone = True
|
||||||
else: # POLLHUP or something unexpected
|
else: # something unexpected
|
||||||
if f == popen.stdout:
|
if f == popen.stdout:
|
||||||
outDone = True
|
outDone = True
|
||||||
elif f == popen.stderr:
|
elif f == popen.stderr:
|
||||||
@@ -451,7 +451,7 @@ def pmonitor(popens, timeoutms=500, readline=True,
|
|||||||
fd = popen.stdout.fileno()
|
fd = popen.stdout.fileno()
|
||||||
fdToHost[ fd ] = host
|
fdToHost[ fd ] = host
|
||||||
fdToDecoder[ fd ] = getincrementaldecoder()
|
fdToDecoder[ fd ] = getincrementaldecoder()
|
||||||
poller.register( fd, POLLIN | POLLHUP )
|
poller.register( fd, POLLIN )
|
||||||
flags = fcntl( fd, F_GETFL )
|
flags = fcntl( fd, F_GETFL )
|
||||||
fcntl( fd, F_SETFL, flags | O_NONBLOCK )
|
fcntl( fd, F_SETFL, flags | O_NONBLOCK )
|
||||||
while popens:
|
while popens:
|
||||||
@@ -461,7 +461,7 @@ def pmonitor(popens, timeoutms=500, readline=True,
|
|||||||
host = fdToHost[ fd ]
|
host = fdToHost[ fd ]
|
||||||
decoder = fdToDecoder[ fd ]
|
decoder = fdToDecoder[ fd ]
|
||||||
popen = popens[ host ]
|
popen = popens[ host ]
|
||||||
if event & POLLIN or event & POLLHUP:
|
if event & ( POLLIN | POLLHUP ):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
f = popen.stdout
|
f = popen.stdout
|
||||||
|
|||||||
Reference in New Issue
Block a user