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 | |
Ronald Oussoren | 94f2528 | 2010-05-05 19:11:21 +0000 | [diff] [blame] | 7 | @unittest.skipIf(sys.platform[:3] in ('win', 'os2', 'riscos'), |
Benjamin Peterson | d856ab6 | 2010-04-06 21:55:16 +0000 | [diff] [blame] | 8 | "can't easily test on this system") |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 9 | class SelectTestCase(unittest.TestCase): |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 10 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 11 | class Nope: |
| 12 | pass |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 13 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 14 | class Almost: |
| 15 | def fileno(self): |
| 16 | return 'fileno' |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 17 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 18 | 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") |
Antoine Pitrou | 131a641 | 2011-04-09 23:49:58 +0200 | [diff] [blame] | 23 | self.assertRaises(ValueError, select.select, [], [], [], -1) |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 24 | |
Benjamin Peterson | d856ab6 | 2010-04-06 21:55:16 +0000 | [diff] [blame] | 25 | def test_returned_list_identity(self): |
| 26 | # See issue #8329 |
| 27 | r, w, x = select.select([], [], [], 1) |
| 28 | self.assertIsNot(r, w) |
| 29 | self.assertIsNot(r, x) |
| 30 | self.assertIsNot(w, x) |
| 31 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 32 | def test_select(self): |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 33 | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
| 34 | p = os.popen(cmd, 'r') |
| 35 | for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 36 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 37 | print('timeout =', tout) |
| 38 | rfd, wfd, xfd = select.select([p], [], [], tout) |
| 39 | if (rfd, wfd, xfd) == ([], [], []): |
| 40 | continue |
| 41 | if (rfd, wfd, xfd) == ([p], [], []): |
| 42 | line = p.readline() |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 43 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 44 | print(repr(line)) |
| 45 | if not line: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 46 | if support.verbose: |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 47 | print('EOF') |
| 48 | break |
| 49 | continue |
| 50 | self.fail('Unexpected return values from select():', rfd, wfd, xfd) |
| 51 | p.close() |
Barry Warsaw | 792c94a | 1996-12-11 23:58:46 +0000 | [diff] [blame] | 52 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 53 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 54 | support.run_unittest(SelectTestCase) |
| 55 | support.reap_children() |
Neal Norwitz | 1b738e9 | 2002-06-13 22:23:47 +0000 | [diff] [blame] | 56 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 57 | if __name__ == "__main__": |
| 58 | test_main() |