ยปCore Development>Code coverage>Lib/test/test_threadedtempfile.py

Python code coverage for Lib/test/test_threadedtempfile.py

#countcontent
1n/a"""
2n/aCreate and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile)
3n/ain each of NUM_THREADS threads, recording the number of successes and
4n/afailures. A failure is a bug in tempfile, and may be due to:
5n/a
6n/a+ Trying to create more than one tempfile with the same name.
7n/a+ Trying to delete a tempfile that doesn't still exist.
8n/a+ Something we've never seen before.
9n/a
10n/aBy default, NUM_THREADS == 20 and FILES_PER_THREAD == 50. This is enough to
11n/acreate about 150 failures per run under Win98SE in 2.0, and runs pretty
12n/aquickly. Guido reports needing to boost FILES_PER_THREAD to 500 before
13n/aprovoking a 2.0 failure under Linux.
14n/a"""
15n/a
16n/aNUM_THREADS = 20
17n/aFILES_PER_THREAD = 50
18n/a
19n/aimport tempfile
20n/a
21n/afrom test.support import start_threads, import_module
22n/athreading = import_module('threading')
23n/aimport unittest
24n/aimport io
25n/afrom traceback import print_exc
26n/a
27n/astartEvent = threading.Event()
28n/a
29n/aclass TempFileGreedy(threading.Thread):
30n/a error_count = 0
31n/a ok_count = 0
32n/a
33n/a def run(self):
34n/a self.errors = io.StringIO()
35n/a startEvent.wait()
36n/a for i in range(FILES_PER_THREAD):
37n/a try:
38n/a f = tempfile.TemporaryFile("w+b")
39n/a f.close()
40n/a except:
41n/a self.error_count += 1
42n/a print_exc(file=self.errors)
43n/a else:
44n/a self.ok_count += 1
45n/a
46n/a
47n/aclass ThreadedTempFileTest(unittest.TestCase):
48n/a def test_main(self):
49n/a threads = [TempFileGreedy() for i in range(NUM_THREADS)]
50n/a with start_threads(threads, startEvent.set):
51n/a pass
52n/a ok = sum(t.ok_count for t in threads)
53n/a errors = [str(t.name) + str(t.errors.getvalue())
54n/a for t in threads if t.error_count]
55n/a
56n/a msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok,
57n/a '\n'.join(errors))
58n/a self.assertEqual(errors, [], msg)
59n/a self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD)
60n/a
61n/aif __name__ == "__main__":
62n/a unittest.main()