Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 1 | """ |
| 2 | Create and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile) |
| 3 | in each of NUM_THREADS threads, recording the number of successes and |
| 4 | failures. 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 | |
| 10 | By default, NUM_THREADS == 20 and FILES_PER_THREAD == 50. This is enough to |
| 11 | create about 150 failures per run under Win98SE in 2.0, and runs pretty |
| 12 | quickly. Guido reports needing to boost FILES_PER_THREAD to 500 before |
| 13 | provoking a 2.0 failure under Linux. Run the test alone to boost either |
| 14 | via cmdline switches: |
| 15 | |
| 16 | -f FILES_PER_THREAD (int) |
| 17 | -t NUM_THREADS (int) |
| 18 | """ |
| 19 | |
| 20 | NUM_THREADS = 20 # change w/ -t option |
| 21 | FILES_PER_THREAD = 50 # change w/ -f option |
| 22 | |
| 23 | import threading |
Tim Peters | 73cbc5e | 2001-01-13 03:45:59 +0000 | [diff] [blame] | 24 | from test_support import TestFailed |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 25 | import StringIO |
| 26 | from traceback import print_exc |
| 27 | |
| 28 | startEvent = threading.Event() |
| 29 | |
| 30 | import tempfile |
| 31 | tempfile.gettempdir() # Do this now, to avoid spurious races later |
| 32 | |
| 33 | class TempFileGreedy(threading.Thread): |
| 34 | error_count = 0 |
| 35 | ok_count = 0 |
| 36 | |
| 37 | def run(self): |
| 38 | self.errors = StringIO.StringIO() |
| 39 | startEvent.wait() |
| 40 | for i in range(FILES_PER_THREAD): |
| 41 | try: |
| 42 | f = tempfile.TemporaryFile("w+b") |
| 43 | f.close() |
| 44 | except: |
| 45 | self.error_count += 1 |
| 46 | print_exc(file=self.errors) |
| 47 | else: |
| 48 | self.ok_count += 1 |
| 49 | |
| 50 | def _test(): |
| 51 | threads = [] |
| 52 | |
| 53 | print "Creating" |
| 54 | for i in range(NUM_THREADS): |
| 55 | t = TempFileGreedy() |
| 56 | threads.append(t) |
| 57 | t.start() |
| 58 | |
| 59 | print "Starting" |
| 60 | startEvent.set() |
| 61 | |
| 62 | print "Reaping" |
| 63 | ok = errors = 0 |
| 64 | for t in threads: |
| 65 | t.join() |
| 66 | ok += t.ok_count |
| 67 | errors += t.error_count |
| 68 | if t.error_count: |
| 69 | print '%s errors:\n%s' % (t.getName(), t.errors.getvalue()) |
| 70 | |
| 71 | msg = "Done: errors %d ok %d" % (errors, ok) |
| 72 | print msg |
| 73 | if errors: |
| 74 | raise TestFailed(msg) |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | import sys, getopt |
| 78 | opts, args = getopt.getopt(sys.argv[1:], "t:f:") |
| 79 | for o, v in opts: |
| 80 | if o == "-f": |
| 81 | FILES_PER_THREAD = int(v) |
| 82 | elif o == "-t": |
| 83 | NUM_THREADS = int(v) |
| 84 | |
| 85 | _test() |