Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_devpoll.py @ 73798:6cde416ef03b
Credit Per Øyvind Karlsen for the initial implementation of the lzma module (issue #6715).
| author | Nadeem Vawda <nadeem.vawda@gmail.com> |
|---|---|
| date | Thu, 01 Dec 2011 01:18:27 +0200 |
| parents | 8f7ab4bf7ad9 |
| children | 8f10c9eae183 520adfab957e |
| rev | line source |
|---|---|
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
1 # Test case for the select.devpoll() function |
|
15305
07f24261c6e6
Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents:
14247
diff
changeset
|
2 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
3 # Initial tests are copied as is from "test_poll.py" |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
4 |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
5 import os, select, random, unittest, sys |
|
52389
d4e7470bd660
Merged revisions 70554,70588-70589,70598,70605,70611-70621,70623-70624,70626-70627 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
47201
diff
changeset
|
6 from test.support import TESTFN, run_unittest |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
7 |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
8 try: |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
9 select.devpoll |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
10 except AttributeError: |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
11 raise unittest.SkipTest("select.devpoll not defined -- skipping test_devpoll") |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
12 |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
13 |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
14 def find_ready_matching(ready, flag): |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
15 match = [] |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
16 for fd, mode in ready: |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
17 if mode & flag: |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
18 match.append(fd) |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
19 return match |
|
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
20 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
21 class DevPollTests(unittest.TestCase): |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
22 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
23 def test_devpoll1(self): |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
24 # Basic functional test of poll object |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
25 # Create a bunch of pipe and test that poll works with them. |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
26 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
27 p = select.devpoll() |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
28 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
29 NUM_PIPES = 12 |
|
42205
9b72f0636986
Fix test_poll.py. By Christian Heimes.
Guido van Rossum <guido@python.org>
parents:
40644
diff
changeset
|
30 MSG = b" This is a test." |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
31 MSG_LEN = len(MSG) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
32 readers = [] |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
33 writers = [] |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
34 r2w = {} |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
35 w2r = {} |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
36 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
37 for i in range(NUM_PIPES): |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
38 rd, wr = os.pipe() |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
39 p.register(rd) |
|
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45471
diff
changeset
|
40 p.modify(rd, select.POLLIN) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
41 p.register(wr, select.POLLOUT) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
42 readers.append(rd) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
43 writers.append(wr) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
44 r2w[rd] = wr |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
45 w2r[wr] = rd |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
46 |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
47 bufs = [] |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
48 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
49 while writers: |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
50 ready = p.poll() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
51 ready_writers = find_ready_matching(ready, select.POLLOUT) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
52 if not ready_writers: |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
53 self.fail("no pipes ready for writing") |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
54 wr = random.choice(ready_writers) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
55 os.write(wr, MSG) |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
56 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
57 ready = p.poll() |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
58 ready_readers = find_ready_matching(ready, select.POLLIN) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
59 if not ready_readers: |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
60 self.fail("no pipes ready for reading") |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
61 self.assertEqual([w2r[wr]], ready_readers) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
62 rd = ready_readers[0] |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
63 buf = os.read(rd, MSG_LEN) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
64 self.assertEqual(len(buf), MSG_LEN) |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
65 bufs.append(buf) |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
66 os.close(r2w[rd]) ; os.close(rd) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
67 p.unregister(r2w[rd]) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
68 p.unregister(rd) |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
69 writers.remove(r2w[rd]) |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
70 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
71 self.assertEqual(bufs, [MSG] * NUM_PIPES) |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
72 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
73 def test_timeout_overflow(self): |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
74 pollster = select.devpoll() |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
75 w, r = os.pipe() |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
76 pollster.register(w) |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
77 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
78 pollster.poll(-1) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
79 self.assertRaises(OverflowError, pollster.poll, -2) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
80 self.assertRaises(OverflowError, pollster.poll, -1 << 31) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
81 self.assertRaises(OverflowError, pollster.poll, -1 << 64) |
|
14175
8d1a059ae99d
Test suite for poll() interface (SF patch #100852)
Andrew M. Kuchling <amk@amk.ca>
parents:
diff
changeset
|
82 |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
83 pollster.poll(0) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
84 pollster.poll(1) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
85 pollster.poll(1 << 30) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
86 self.assertRaises(OverflowError, pollster.poll, 1 << 31) |
|
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
87 self.assertRaises(OverflowError, pollster.poll, 1 << 63) |
|
40644
1c26505c1de6
Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
88 self.assertRaises(OverflowError, pollster.poll, 1 << 64) |
|
35834
418a61ead11b
Bug #1346533, select.poll() doesn't raise an error if timeout > sys.maxint
Neal Norwitz <nnorwitz@gmail.com>
parents:
31515
diff
changeset
|
89 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
90 def test_main(): |
|
73549
8f7ab4bf7ad9
Issue #6397: Support '/dev/poll' polling objects in select module, under Solaris & derivatives.
Jesus Cea <jcea@jcea.es>
parents:
52389
diff
changeset
|
91 run_unittest(DevPollTests) |
|
36121
686306a67e9b
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
35834
diff
changeset
|
92 |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
93 if __name__ == '__main__': |
|
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
36121
diff
changeset
|
94 test_main() |
