blob: fd7c8d0c5e4ea0f3e741d117abe1b80c034dcd43 [file] [log] [blame]
Brett Cannon2e0f9f32008-03-13 20:47:41 +00001from test import test_support
2import unittest
Barry Warsaw792c94a1996-12-11 23:58:46 +00003import select
4import os
Brett Cannon2e0f9f32008-03-13 20:47:41 +00005import sys
Barry Warsaw792c94a1996-12-11 23:58:46 +00006
Benjamin Peterson22c62dd2010-04-06 21:37:06 +00007@unittest.skipIf(sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'),
8 "can't easily test on this system")
Brett Cannon2e0f9f32008-03-13 20:47:41 +00009class SelectTestCase(unittest.TestCase):
Barry Warsaw792c94a1996-12-11 23:58:46 +000010
Brett Cannon2e0f9f32008-03-13 20:47:41 +000011 class Nope:
12 pass
Barry Warsaw792c94a1996-12-11 23:58:46 +000013
Brett Cannon2e0f9f32008-03-13 20:47:41 +000014 class Almost:
15 def fileno(self):
16 return 'fileno'
Fred Drake004d5e62000-10-23 17:22:08 +000017
Brett Cannon2e0f9f32008-03-13 20:47:41 +000018 def test_error_conditions(self):
19 self.assertRaises(TypeError, select.select, 1, 2, 3)
20 self.assertRaises(TypeError, select.select, [self.Nope()], [], [])
21 self.assertRaises(TypeError, select.select, [self.Almost()], [], [])
22 self.assertRaises(TypeError, select.select, [], [], [], "not a number")
Barry Warsaw792c94a1996-12-11 23:58:46 +000023
Brett Cannon2e0f9f32008-03-13 20:47:41 +000024 def test_select(self):
Brett Cannon2e0f9f32008-03-13 20:47:41 +000025 cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
26 p = os.popen(cmd, 'r')
27 for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
28 if test_support.verbose:
29 print 'timeout =', tout
30 rfd, wfd, xfd = select.select([p], [], [], tout)
31 if (rfd, wfd, xfd) == ([], [], []):
32 continue
33 if (rfd, wfd, xfd) == ([p], [], []):
34 line = p.readline()
35 if test_support.verbose:
36 print repr(line)
37 if not line:
38 if test_support.verbose:
39 print 'EOF'
40 break
41 continue
42 self.fail('Unexpected return values from select():', rfd, wfd, xfd)
43 p.close()
Neal Norwitz1b738e92002-06-13 22:23:47 +000044
Guido van Rossumb31c7f71993-11-11 10:31:23 +000045
Brett Cannon2e0f9f32008-03-13 20:47:41 +000046def test_main():
47 test_support.run_unittest(SelectTestCase)
48 test_support.reap_children()
Guido van Rossumb31c7f71993-11-11 10:31:23 +000049
Brett Cannon2e0f9f32008-03-13 20:47:41 +000050if __name__ == "__main__":
51 test_main()