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 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 13 | provoking a 2.0 failure under Linux. |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 14 | """ |
| 15 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | NUM_THREADS = 20 |
| 17 | FILES_PER_THREAD = 50 |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | import tempfile |
| 20 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 21 | from test.support import threading_setup, threading_cleanup, run_unittest, import_module |
| 22 | threading = import_module('threading') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | import unittest |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 24 | import io |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 25 | from traceback import print_exc |
| 26 | |
| 27 | startEvent = threading.Event() |
| 28 | |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 29 | class TempFileGreedy(threading.Thread): |
| 30 | error_count = 0 |
| 31 | ok_count = 0 |
| 32 | |
| 33 | def run(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 34 | self.errors = io.StringIO() |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 35 | startEvent.wait() |
| 36 | for i in range(FILES_PER_THREAD): |
| 37 | try: |
| 38 | f = tempfile.TemporaryFile("w+b") |
| 39 | f.close() |
| 40 | except: |
| 41 | self.error_count += 1 |
| 42 | print_exc(file=self.errors) |
| 43 | else: |
| 44 | self.ok_count += 1 |
| 45 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 46 | |
| 47 | class ThreadedTempFileTest(unittest.TestCase): |
| 48 | def test_main(self): |
| 49 | threads = [] |
| 50 | thread_info = threading_setup() |
| 51 | |
| 52 | for i in range(NUM_THREADS): |
| 53 | t = TempFileGreedy() |
| 54 | threads.append(t) |
| 55 | t.start() |
| 56 | |
| 57 | startEvent.set() |
| 58 | |
| 59 | ok = 0 |
| 60 | errors = [] |
| 61 | for t in threads: |
| 62 | t.join() |
| 63 | ok += t.ok_count |
| 64 | if t.error_count: |
Benjamin Peterson | 88d9498 | 2008-09-05 00:43:33 +0000 | [diff] [blame] | 65 | errors.append(str(t.name) + str(t.errors.getvalue())) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 66 | |
| 67 | threading_cleanup(*thread_info) |
| 68 | |
| 69 | msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok, |
| 70 | '\n'.join(errors)) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 71 | self.assertEqual(errors, [], msg) |
| 72 | self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 73 | |
Tim Peters | d8a9d2a | 2002-09-25 20:32:28 +0000 | [diff] [blame] | 74 | def test_main(): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 75 | run_unittest(ThreadedTempFileTest) |
Tim Peters | d8a9d2a | 2002-09-25 20:32:28 +0000 | [diff] [blame] | 76 | |
Tim Peters | 9fadfb0 | 2001-01-13 03:04:02 +0000 | [diff] [blame] | 77 | if __name__ == "__main__": |
Tim Peters | d8a9d2a | 2002-09-25 20:32:28 +0000 | [diff] [blame] | 78 | test_main() |