Convert test_poll to unittest.

This commit is contained in:
Georg Brandl 2006-10-29 19:20:45 +00:00
parent 3163179f1b
commit eecce795a3
2 changed files with 118 additions and 173 deletions

View File

@ -1,19 +0,0 @@
test_poll
Running poll test 1
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
This is a test.
Poll test 1 complete
Running poll test 2
Poll test 2 complete
Running poll test 3
Poll test 3 complete

View File

@ -1,7 +1,7 @@
# Test case for the os.poll() function # Test case for the os.poll() function
import sys, os, select, random import sys, os, select, random, unittest
from test.test_support import verify, verbose, TestSkipped, TESTFN from test.test_support import TestSkipped, TESTFN, run_unittest
try: try:
select.poll select.poll
@ -16,177 +16,141 @@ def find_ready_matching(ready, flag):
match.append(fd) match.append(fd)
return match return match
def test_poll1(): class PollTests(unittest.TestCase):
"""Basic functional test of poll object
Create a bunch of pipe and test that poll works with them. def test_poll1(self):
""" # Basic functional test of poll object
print 'Running poll test 1' # Create a bunch of pipe and test that poll works with them.
p = select.poll()
p = select.poll()
NUM_PIPES = 12 NUM_PIPES = 12
MSG = " This is a test." MSG = " This is a test."
MSG_LEN = len(MSG) MSG_LEN = len(MSG)
readers = [] readers = []
writers = [] writers = []
r2w = {} r2w = {}
w2r = {} w2r = {}
for i in range(NUM_PIPES): for i in range(NUM_PIPES):
rd, wr = os.pipe() rd, wr = os.pipe()
p.register(rd, select.POLLIN) p.register(rd, select.POLLIN)
p.register(wr, select.POLLOUT) p.register(wr, select.POLLOUT)
readers.append(rd) readers.append(rd)
writers.append(wr) writers.append(wr)
r2w[rd] = wr r2w[rd] = wr
w2r[wr] = rd w2r[wr] = rd
while writers: bufs = []
ready = p.poll()
ready_writers = find_ready_matching(ready, select.POLLOUT)
if not ready_writers:
raise RuntimeError, "no pipes ready for writing"
wr = random.choice(ready_writers)
os.write(wr, MSG)
ready = p.poll() while writers:
ready_readers = find_ready_matching(ready, select.POLLIN) ready = p.poll()
if not ready_readers: ready_writers = find_ready_matching(ready, select.POLLOUT)
raise RuntimeError, "no pipes ready for reading" if not ready_writers:
rd = random.choice(ready_readers) raise RuntimeError, "no pipes ready for writing"
buf = os.read(rd, MSG_LEN) wr = random.choice(ready_writers)
verify(len(buf) == MSG_LEN) os.write(wr, MSG)
print buf
os.close(r2w[rd]) ; os.close( rd )
p.unregister( r2w[rd] )
p.unregister( rd )
writers.remove(r2w[rd])
poll_unit_tests() ready = p.poll()
print 'Poll test 1 complete' ready_readers = find_ready_matching(ready, select.POLLIN)
if not ready_readers:
raise RuntimeError, "no pipes ready for reading"
rd = random.choice(ready_readers)
buf = os.read(rd, MSG_LEN)
self.assertEqual(len(buf), MSG_LEN)
bufs.append(buf)
os.close(r2w[rd]) ; os.close( rd )
p.unregister( r2w[rd] )
p.unregister( rd )
writers.remove(r2w[rd])
def poll_unit_tests(): self.assertEqual(bufs, [MSG] * NUM_PIPES)
# returns NVAL for invalid file descriptor
FD = 42
try:
os.close(FD)
except OSError:
pass
p = select.poll()
p.register(FD)
r = p.poll()
verify(r[0] == (FD, select.POLLNVAL))
f = open(TESTFN, 'w') def poll_unit_tests(self):
fd = f.fileno() # returns NVAL for invalid file descriptor
p = select.poll() FD = 42
p.register(f) try:
r = p.poll() os.close(FD)
verify(r[0][0] == fd) except OSError:
f.close() pass
r = p.poll() p = select.poll()
verify(r[0] == (fd, select.POLLNVAL)) p.register(FD)
os.unlink(TESTFN) r = p.poll()
self.assertEqual(r[0], (FD, select.POLLNVAL))
# type error for invalid arguments f = open(TESTFN, 'w')
p = select.poll() fd = f.fileno()
try: p = select.poll()
p.register(p) p.register(f)
except TypeError: r = p.poll()
pass self.assertEqual(r[0][0], fd)
else: f.close()
print "Bogus register call did not raise TypeError" r = p.poll()
try: self.assertEqual(r[0], (fd, select.POLLNVAL))
p.unregister(p) os.unlink(TESTFN)
except TypeError:
pass
else:
print "Bogus unregister call did not raise TypeError"
# can't unregister non-existent object # type error for invalid arguments
p = select.poll() p = select.poll()
try: self.assertRaises(TypeError, p.register, p)
p.unregister(3) self.assertRaises(TypeError, p.unregister, p)
except KeyError:
pass
else:
print "Bogus unregister call did not raise KeyError"
# Test error cases # can't unregister non-existent object
pollster = select.poll() p = select.poll()
class Nope: self.assertRaises(KeyError, p.unregister, 3)
pass
class Almost: # Test error cases
def fileno(self): pollster = select.poll()
return 'fileno' class Nope:
pass
try: class Almost:
pollster.register( Nope(), 0 ) def fileno(self):
except TypeError: pass return 'fileno'
else: print 'expected TypeError exception, not raised'
try: self.assertRaises(TypeError, pollster.register, Nope(), 0)
pollster.register( Almost(), 0 ) self.assertRaises(TypeError, pollster.register, Almost(), 0)
except TypeError: pass
else: print 'expected TypeError exception, not raised'
# Another test case for poll(). This is copied from the test case for
# select(), modified to use poll() instead.
# Another test case for poll(). This is copied from the test case for def test_poll2(self):
# select(), modified to use poll() instead. cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
p = os.popen(cmd, 'r')
pollster = select.poll()
pollster.register( p, select.POLLIN )
for tout in (0, 1000, 2000, 4000, 8000, 16000) + (-1,)*10:
fdlist = pollster.poll(tout)
if (fdlist == []):
continue
fd, flags = fdlist[0]
if flags & select.POLLHUP:
line = p.readline()
if line != "":
self.fail('error: pipe seems to be closed, but still returns data')
continue
def test_poll2(): elif flags & select.POLLIN:
print 'Running poll test 2' line = p.readline()
cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' if not line:
p = os.popen(cmd, 'r') break
pollster = select.poll() continue
pollster.register( p, select.POLLIN ) else:
for tout in (0, 1000, 2000, 4000, 8000, 16000) + (-1,)*10: self.fail('Unexpected return value from select.poll: %s' % fdlist)
if verbose: p.close()
print 'timeout =', tout
fdlist = pollster.poll(tout)
if (fdlist == []):
continue
fd, flags = fdlist[0]
if flags & select.POLLHUP:
line = p.readline()
if line != "":
print 'error: pipe seems to be closed, but still returns data'
continue
elif flags & select.POLLIN: def test_poll3(self):
line = p.readline() # test int overflow
if verbose: pollster = select.poll()
print repr(line) pollster.register(1)
if not line:
if verbose:
print 'EOF'
break
continue
else:
print 'Unexpected return value from select.poll:', fdlist
p.close()
print 'Poll test 2 complete'
def test_poll3(): self.assertRaises(OverflowError, pollster.poll, 1L << 64)
# test int overflow
print 'Running poll test 3'
pollster = select.poll()
pollster.register(1)
try: x = 2 + 3
pollster.poll(1L << 64) if x != 5:
except OverflowError: self.fail('Overflow must have occurred')
pass
else:
print 'Expected OverflowError with excessive timeout'
x = 2 + 3 def test_main():
if x != 5: run_unittest(PollTests)
print 'Overflow must have occurred'
print 'Poll test 3 complete'
if __name__ == '__main__':
test_poll1() test_main()
test_poll2()
test_poll3()