blob: 9aa93763e374565fe1699ef0a26bd5e176864b5a [file] [log] [blame]
Tim Peters9fadfb02001-01-13 03:04:02 +00001"""
2Create and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile)
3in each of NUM_THREADS threads, recording the number of successes and
4failures. A failure is a bug in tempfile, and may be due to:
5
6+ Trying to create more than one tempfile with the same name.
7+ Trying to delete a tempfile that doesn't still exist.
8+ Something we've never seen before.
9
10By default, NUM_THREADS == 20 and FILES_PER_THREAD == 50. This is enough to
11create about 150 failures per run under Win98SE in 2.0, and runs pretty
12quickly. Guido reports needing to boost FILES_PER_THREAD to 500 before
13provoking a 2.0 failure under Linux. Run the test alone to boost either
14via cmdline switches:
15
16-f FILES_PER_THREAD (int)
17-t NUM_THREADS (int)
18"""
19
20NUM_THREADS = 20 # change w/ -t option
21FILES_PER_THREAD = 50 # change w/ -f option
22
Guido van Rossum9df3eab2001-04-14 14:35:43 +000023import thread # If this fails, we can't test this module
Tim Peters9fadfb02001-01-13 03:04:02 +000024import threading
Tim Peters73cbc5e2001-01-13 03:45:59 +000025from test_support import TestFailed
Tim Peters9fadfb02001-01-13 03:04:02 +000026import StringIO
27from traceback import print_exc
28
29startEvent = threading.Event()
30
31import tempfile
32tempfile.gettempdir() # Do this now, to avoid spurious races later
33
34class TempFileGreedy(threading.Thread):
35 error_count = 0
36 ok_count = 0
37
38 def run(self):
39 self.errors = StringIO.StringIO()
40 startEvent.wait()
41 for i in range(FILES_PER_THREAD):
42 try:
43 f = tempfile.TemporaryFile("w+b")
44 f.close()
45 except:
46 self.error_count += 1
47 print_exc(file=self.errors)
48 else:
49 self.ok_count += 1
50
51def _test():
52 threads = []
53
54 print "Creating"
55 for i in range(NUM_THREADS):
56 t = TempFileGreedy()
57 threads.append(t)
58 t.start()
59
60 print "Starting"
61 startEvent.set()
62
63 print "Reaping"
64 ok = errors = 0
65 for t in threads:
66 t.join()
67 ok += t.ok_count
68 errors += t.error_count
69 if t.error_count:
70 print '%s errors:\n%s' % (t.getName(), t.errors.getvalue())
71
72 msg = "Done: errors %d ok %d" % (errors, ok)
73 print msg
74 if errors:
75 raise TestFailed(msg)
76
77if __name__ == "__main__":
78 import sys, getopt
79 opts, args = getopt.getopt(sys.argv[1:], "t:f:")
80 for o, v in opts:
81 if o == "-f":
82 FILES_PER_THREAD = int(v)
83 elif o == "-t":
84 NUM_THREADS = int(v)
85
86_test()