annotate Lib/test/test_threadedtempfile.py @ 65035:87f4b32905df 2.7

Cosmetic fix to use print function.
author Hirokazu Yamamoto <ocean-city@m2.ccsnet.ne.jp>
date Fri, 01 Oct 2010 10:48:47 +0000
parents 3a0484200034
children 614c84670c63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
19 import tempfile
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
20
61046
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 41133
diff changeset
21 from test.test_support import threading_setup, threading_cleanup, run_unittest, import_module
3a0484200034 Issue #7449, last part (11): fix many tests if thread support is disabled
Victor Stinner <victor.stinner@haypocalc.com>
parents: 41133
diff changeset
22 threading = import_module('threading')
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
23 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
24 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
25 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
26
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 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
28
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 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
30 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
31 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
32
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 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45
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
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
47 class ThreadedTempFileTest(unittest.TestCase):
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
48 def test_main(self):
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
49 threads = []
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
50 thread_info = threading_setup()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41132
diff changeset
51
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
52 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
53 t = TempFileGreedy()
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
54 threads.append(t)
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
55 t.start()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41132
diff changeset
56
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
57 startEvent.set()
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41132
diff changeset
58
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
59 ok = 0
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
60 errors = []
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
61 for t in threads:
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
62 t.join()
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
63 ok += t.ok_count
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
64 if t.error_count:
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
65 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
66
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
67 threading_cleanup(*thread_info)
41133
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41132
diff changeset
68
01355124ce40 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 41132
diff changeset
69 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
70 '\n'.join(errors))
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
71 self.assertEquals(errors, [], msg)
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
72 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
73
41132
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
74 def test_main():
1e12bed7a2fe Patch #1670993: Refactor test_threadedtempfile.py to use unittest.
Collin Winter <collinw@gmail.com>
parents: 38867
diff changeset
75 run_unittest(ThreadedTempFileTest)
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
76
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
77 if __name__ == "__main__":
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
78 test_main()