Debian Codegraph @

Lib/test/test_threadedtempfile.py

raw
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.
14 """
15
16 import tempfile
17
18 from test import support
19 from test.support import threading_helper
20 import unittest
21 import io
22 import threading
23 from traceback import print_exc
24
25 threading_helper.requires_working_threading(module=True)
26
27 NUM_THREADS = 20
28 FILES_PER_THREAD = 50
29
30
31 startEvent = threading.Event()
32
33
34 class TempFileGreedy(threading.Thread):
35 error_count = 0
36 ok_count = 0
37
38 def run(self):
39 self.errors = io.StringIO()
40 startEvent.wait()
41 for i in range(FILES_PER_THREAD):
42 try:
43 f = tempfile.TemporaryFile("w+b")
44 f.close()
45 except:
46 self.error_count += 1
47 print_exc(file=self.errors)
48 else:
49 self.ok_count += 1
50
51
52 class ThreadedTempFileTest(unittest.TestCase):
53 @support.bigmemtest(size=NUM_THREADS, memuse=60*2**20, dry_run=False)
54 def test_main(self, size):
55 threads = [TempFileGreedy() for i in range(NUM_THREADS)]
56 with threading_helper.start_threads(threads, startEvent.set):
57 pass
58 ok = sum(t.ok_count for t in threads)
59 errors = [str(t.name) + str(t.errors.getvalue())
60 for t in threads if t.error_count]
61
62 msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok,
63 '\n'.join(errors))
64 self.assertEqual(errors, [], msg)
65 self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD)
66
67 if __name__ == "__main__":
68 unittest.main()