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