Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test import support |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 2 | import unittest |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 3 | import select |
| 4 | import os |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 5 | import sys |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 6 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 7 | class SelectTestCase(unittest.TestCase): |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 8 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9 | class Nope: |
| 10 | pass |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 11 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 12 | class Almost: |
| 13 | def fileno(self): |
| 14 | return 'fileno' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 16 | 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 Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 21 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 22 | def test_select(self): |
| 23 | if sys.platform[:3] in ('win', 'mac', 'os2', 'riscos'): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 24 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 25 | 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 Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 31 | 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 Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 37 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 38 | print(repr(line)) |
| 39 | if not line: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 40 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 41 | print('EOF') |
| 42 | break |
| 43 | continue |
| 44 | self.fail('Unexpected return values from select():', rfd, wfd, xfd) |
| 45 | p.close() |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 46 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 47 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 48 | support.run_unittest(SelectTestCase) |
| 49 | support.reap_children() |
Neal Norwitz | 1b738e9 | 2002-06-13 22:23:47 +0000 | [diff] [blame] | 50 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 51 | if __name__ == "__main__": |
| 52 | test_main() |