blob: f3d4ba36377dacdc89281cd40d8f76044031b8be [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
Guido van Rossumd8faa362007-04-27 19:54:29 +000013provoking a 2.0 failure under Linux.
Tim Peters9fadfb02001-01-13 03:04:02 +000014"""
15
Guido van Rossumd8faa362007-04-27 19:54:29 +000016NUM_THREADS = 20
17FILES_PER_THREAD = 50
Tim Peters9fadfb02001-01-13 03:04:02 +000018
Guido van Rossumd8faa362007-04-27 19:54:29 +000019import tempfile
20
Serhiy Storchaka263dcd22015-04-01 13:01:14 +030021from test.support import start_threads, import_module
Guido van Rossumd8faa362007-04-27 19:54:29 +000022import unittest
Guido van Rossum34d19282007-08-09 01:03:29 +000023import io
Antoine Pitroua6a4dc82017-09-07 18:56:24 +020024import threading
Tim Peters9fadfb02001-01-13 03:04:02 +000025from 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):
Guido van Rossum34d19282007-08-09 01:03:29 +000034 self.errors = io.StringIO()
Tim Peters9fadfb02001-01-13 03:04:02 +000035 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 Rossumd8faa362007-04-27 19:54:29 +000046
47class ThreadedTempFileTest(unittest.TestCase):
48 def test_main(self):
Serhiy Storchaka263dcd22015-04-01 13:01:14 +030049 threads = [TempFileGreedy() for i in range(NUM_THREADS)]
50 with start_threads(threads, startEvent.set):
51 pass
52 ok = sum(t.ok_count for t in threads)
53 errors = [str(t.name) + str(t.errors.getvalue())
54 for t in threads if t.error_count]
Guido van Rossumd8faa362007-04-27 19:54:29 +000055
56 msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok,
57 '\n'.join(errors))
Ezio Melottib3aedd42010-11-20 19:04:17 +000058 self.assertEqual(errors, [], msg)
59 self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD)
Guido van Rossumd8faa362007-04-27 19:54:29 +000060
Tim Peters9fadfb02001-01-13 03:04:02 +000061if __name__ == "__main__":
Serhiy Storchaka263dcd22015-04-01 13:01:14 +030062 unittest.main()