Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_threadedtempfile.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 01355124ce40 |
| children | 3a0484200034 |
| rev | line source |
|---|---|
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
1 """ |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 Create and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile) |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 in each of NUM_THREADS threads, recording the number of successes and |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
4 failures. A failure is a bug in tempfile, and may be due to: |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
5 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 + Trying to create more than one tempfile with the same name. |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 + Trying to delete a tempfile that doesn't still exist. |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 + Something we've never seen before. |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
9 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
10 By default, NUM_THREADS == 20 and FILES_PER_THREAD == 50. This is enough to |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
11 create about 150 failures per run under Win98SE in 2.0, and runs pretty |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
12 quickly. Guido reports needing to boost FILES_PER_THREAD to 500 before |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
13 provoking a 2.0 failure under Linux. |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
14 """ |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
15 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
16 NUM_THREADS = 20 |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
17 FILES_PER_THREAD = 50 |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
18 |
|
17513
c651cfd0ffd4
Add "import thread" at the top of the module; this prevents us from
Guido van Rossum <guido@python.org>
parents:
15841
diff
changeset
|
19 import thread # If this fails, we can't test this module |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
20 import threading |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
21 import tempfile |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
22 |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
23 from test.test_support import threading_setup, threading_cleanup, run_unittest |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
24 import unittest |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
25 import StringIO |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
26 from traceback import print_exc |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
27 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
28 startEvent = threading.Event() |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
29 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
30 class TempFileGreedy(threading.Thread): |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
31 error_count = 0 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
32 ok_count = 0 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
33 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 def run(self): |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
35 self.errors = StringIO.StringIO() |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
36 startEvent.wait() |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
37 for i in range(FILES_PER_THREAD): |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
38 try: |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
39 f = tempfile.TemporaryFile("w+b") |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
40 f.close() |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
41 except: |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
42 self.error_count += 1 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
43 print_exc(file=self.errors) |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
44 else: |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
45 self.ok_count += 1 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
46 |
|
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
47 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
48 class ThreadedTempFileTest(unittest.TestCase): |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
49 def test_main(self): |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
50 threads = [] |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
51 thread_info = threading_setup() |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41132
diff
changeset
|
52 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
53 for i in range(NUM_THREADS): |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
54 t = TempFileGreedy() |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
55 threads.append(t) |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
56 t.start() |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41132
diff
changeset
|
57 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
58 startEvent.set() |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41132
diff
changeset
|
59 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
60 ok = 0 |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
61 errors = [] |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
62 for t in threads: |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
63 t.join() |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
64 ok += t.ok_count |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
65 if t.error_count: |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
66 errors.append(str(t.getName()) + str(t.errors.getvalue())) |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
67 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
68 threading_cleanup(*thread_info) |
|
41133
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41132
diff
changeset
|
69 |
|
01355124ce40
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
41132
diff
changeset
|
70 msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok, |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
71 '\n'.join(errors)) |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
72 self.assertEquals(errors, [], msg) |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
73 self.assertEquals(ok, NUM_THREADS * FILES_PER_THREAD) |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
74 |
|
41132
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
75 def test_main(): |
|
1e12bed7a2fe
Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents:
38867
diff
changeset
|
76 run_unittest(ThreadedTempFileTest) |
|
25427
29f6160ec0bd
SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
77 |
|
15840
7d5025bba037
Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
78 if __name__ == "__main__": |
|
25427
29f6160ec0bd
SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents:
24364
diff
changeset
|
79 test_main() |
