blob: a463868f6278d49eddbacb957d79b74426e65ddb [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001from test import support
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002import unittest
Barry Warsaw792c94a1996-12-11 23:58:46 +00003import select
4import os
Christian Heimesdd15f6c2008-03-16 00:07:10 +00005import sys
Barry Warsaw792c94a1996-12-11 23:58:46 +00006
Christian Heimesdd15f6c2008-03-16 00:07:10 +00007class SelectTestCase(unittest.TestCase):
Barry Warsaw792c94a1996-12-11 23:58:46 +00008
Christian Heimesdd15f6c2008-03-16 00:07:10 +00009 class Nope:
10 pass
Barry Warsaw792c94a1996-12-11 23:58:46 +000011
Christian Heimesdd15f6c2008-03-16 00:07:10 +000012 class Almost:
13 def fileno(self):
14 return 'fileno'
Fred Drake004d5e62000-10-23 17:22:08 +000015
Christian Heimesdd15f6c2008-03-16 00:07:10 +000016 def test_error_conditions(self):
17 self.assertRaises(TypeError, select.select, 1, 2, 3)
18 self.assertRaises(TypeError, select.select, [self.Nope()], [], [])
19 self.assertRaises(TypeError, select.select, [self.Almost()], [], [])
20 self.assertRaises(TypeError, select.select, [], [], [], "not a number")
Barry Warsaw792c94a1996-12-11 23:58:46 +000021
Christian Heimesdd15f6c2008-03-16 00:07:10 +000022 def test_select(self):
23 if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000024 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000025 print("Can't test select easily on", sys.platform)
26 return
27 cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
28 p = os.popen(cmd, 'r')
29 for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000030 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000031 print('timeout =', tout)
32 rfd, wfd, xfd = select.select([p], [], [], tout)
33 if (rfd, wfd, xfd) == ([], [], []):
34 continue
35 if (rfd, wfd, xfd) == ([p], [], []):
36 line = p.readline()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000037 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000038 print(repr(line))
39 if not line:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000040 if support.verbose:
Christian Heimesdd15f6c2008-03-16 00:07:10 +000041 print('EOF')
42 break
43 continue
44 self.fail('Unexpected return values from select():', rfd, wfd, xfd)
45 p.close()
Barry Warsaw792c94a1996-12-11 23:58:46 +000046
Christian Heimesdd15f6c2008-03-16 00:07:10 +000047def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +000048 support.run_unittest(SelectTestCase)
49 support.reap_children()
Neal Norwitz1b738e92002-06-13 22:23:47 +000050
Christian Heimesdd15f6c2008-03-16 00:07:10 +000051if __name__ == "__main__":
52 test_main()