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