annotate Lib/test/test_threadedtempfile.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 29f6160ec0bd
children 30d2333852d5
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
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
13 provoking a 2.0 failure under Linux. Run the test alone to boost either
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 via cmdline switches:
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
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
16 -f FILES_PER_THREAD (int)
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
17 -t NUM_THREADS (int)
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 """
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
19
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 NUM_THREADS = 20 # change w/ -t option
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
21 FILES_PER_THREAD = 50 # change w/ -f option
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
22
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
23 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
24 import threading
24364
aa0d2d51fbde Get rid of relative imports in all unittests. Now anything that
Barry Warsaw <barry@python.org>
parents: 17513
diff changeset
25 from test.test_support import TestFailed
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
26 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
27 from traceback import print_exc
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
28 import tempfile
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
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 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
31
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 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
33 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
34 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
35
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 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
49 def test_main():
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
50 threads = []
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
51
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
52 print "Creating"
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
53 for i in range(NUM_THREADS):
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
54 t = TempFileGreedy()
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
55 threads.append(t)
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
56 t.start()
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
57
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
58 print "Starting"
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
59 startEvent.set()
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
60
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
61 print "Reaping"
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
62 ok = errors = 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
63 for t in threads:
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
64 t.join()
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
65 ok += t.ok_count
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 errors += t.error_count
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 if t.error_count:
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
68 print '%s errors:\n%s' % (t.getName(), t.errors.getvalue())
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
69
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
70 msg = "Done: errors %d ok %d" % (errors, ok)
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
71 print msg
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
72 if 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
73 raise TestFailed(msg)
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
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
75
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
76 if __name__ == "__main__":
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 import sys, getopt
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 opts, args = getopt.getopt(sys.argv[1:], "t:f:")
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
79 for o, v in opts:
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
80 if o == "-f":
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
81 FILES_PER_THREAD = int(v)
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
82 elif o == "-t":
7d5025bba037 Guido found a brand new race in tempfile on Linux, due to Linux changing
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
83 NUM_THREADS = int(v)
25427
29f6160ec0bd SF bug 613233: test_threadedtempfile hangs
Tim Peters <tim.peters@gmail.com>
parents: 24364
diff changeset
84 test_main()