blob: 81d9687be253c8187d51fa77e5eb3386b01d7cc8 [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
Collin Wintercb637c92007-03-12 17:24:07 +000013provoking a 2.0 failure under Linux.
Tim Peters9fadfb02001-01-13 03:04:02 +000014"""
15
Collin Wintercb637c92007-03-12 17:24:07 +000016NUM_THREADS = 20
17FILES_PER_THREAD = 50
Tim Peters9fadfb02001-01-13 03:04:02 +000018
Collin Wintercb637c92007-03-12 17:24:07 +000019import tempfile
20
Victor Stinner6a102812010-04-27 23:55:59 +000021from test.test_support import threading_setup, threading_cleanup, run_unittest, import_module
22threading = import_module('threading')
Collin Wintercb637c92007-03-12 17:24:07 +000023import unittest
Tim Peters9fadfb02001-01-13 03:04:02 +000024import StringIO
25from traceback import print_exc
26
27startEvent = threading.Event()
28
Tim Peters9fadfb02001-01-13 03:04:02 +000029class TempFileGreedy(threading.Thread):
30 error_count = 0
31 ok_count = 0
32
33 def run(self):
34 self.errors = StringIO.StringIO()
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
Collin Wintercb637c92007-03-12 17:24:07 +000046
47class ThreadedTempFileTest(unittest.TestCase):
48 def test_main(self):
49 threads = []
50 thread_info = threading_setup()
Tim Petersea5962f2007-03-12 18:07:52 +000051
Collin Wintercb637c92007-03-12 17:24:07 +000052 for i in range(NUM_THREADS):
53 t = TempFileGreedy()
54 threads.append(t)
55 t.start()
Tim Petersea5962f2007-03-12 18:07:52 +000056
Collin Wintercb637c92007-03-12 17:24:07 +000057 startEvent.set()
Tim Petersea5962f2007-03-12 18:07:52 +000058
Collin Wintercb637c92007-03-12 17:24:07 +000059 ok = 0
60 errors = []
61 for t in threads:
62 t.join()
63 ok += t.ok_count
64 if t.error_count:
65 errors.append(str(t.getName()) + str(t.errors.getvalue()))
66
67 threading_cleanup(*thread_info)
Tim Petersea5962f2007-03-12 18:07:52 +000068
69 msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok,
Collin Wintercb637c92007-03-12 17:24:07 +000070 '\n'.join(errors))
Ezio Melotti2623a372010-11-21 13:34:58 +000071 self.assertEqual(errors, [], msg)
72 self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD)
Collin Wintercb637c92007-03-12 17:24:07 +000073
Tim Petersd8a9d2a2002-09-25 20:32:28 +000074def test_main():
Collin Wintercb637c92007-03-12 17:24:07 +000075 run_unittest(ThreadedTempFileTest)
Tim Petersd8a9d2a2002-09-25 20:32:28 +000076
Tim Peters9fadfb02001-01-13 03:04:02 +000077if __name__ == "__main__":
Tim Petersd8a9d2a2002-09-25 20:32:28 +000078 test_main()