| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | Create and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile) | 
					
						
							|  |  |  | in each of NUM_THREADS threads, recording the number of successes and | 
					
						
							|  |  |  | failures.  A failure is a bug in tempfile, and may be due to: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | + Trying to create more than one tempfile with the same name. | 
					
						
							|  |  |  | + Trying to delete a tempfile that doesn't still exist. | 
					
						
							|  |  |  | + Something we've never seen before. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | By default, NUM_THREADS == 20 and FILES_PER_THREAD == 50.  This is enough to | 
					
						
							|  |  |  | create about 150 failures per run under Win98SE in 2.0, and runs pretty | 
					
						
							|  |  |  | quickly. Guido reports needing to boost FILES_PER_THREAD to 500 before | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  | provoking a 2.0 failure under Linux. | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  | NUM_THREADS = 20 | 
					
						
							|  |  |  | FILES_PER_THREAD = 50 | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-14 14:35:43 +00:00
										 |  |  | import thread # If this fails, we can't test this module | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | import threading | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  | import tempfile | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from test.test_support import threading_setup, threading_cleanup, run_unittest | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | import StringIO | 
					
						
							|  |  |  | from traceback import print_exc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | startEvent = threading.Event() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TempFileGreedy(threading.Thread): | 
					
						
							|  |  |  |     error_count = 0 | 
					
						
							|  |  |  |     ok_count = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def run(self): | 
					
						
							|  |  |  |         self.errors = StringIO.StringIO() | 
					
						
							|  |  |  |         startEvent.wait() | 
					
						
							|  |  |  |         for i in range(FILES_PER_THREAD): | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 f = tempfile.TemporaryFile("w+b") | 
					
						
							|  |  |  |                 f.close() | 
					
						
							|  |  |  |             except: | 
					
						
							|  |  |  |                 self.error_count += 1 | 
					
						
							|  |  |  |                 print_exc(file=self.errors) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 self.ok_count += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  | class ThreadedTempFileTest(unittest.TestCase): | 
					
						
							|  |  |  |     def test_main(self): | 
					
						
							|  |  |  |         threads = [] | 
					
						
							|  |  |  |         thread_info = threading_setup() | 
					
						
							| 
									
										
										
										
											2007-03-12 18:07:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  |         for i in range(NUM_THREADS): | 
					
						
							|  |  |  |             t = TempFileGreedy() | 
					
						
							|  |  |  |             threads.append(t) | 
					
						
							|  |  |  |             t.start() | 
					
						
							| 
									
										
										
										
											2007-03-12 18:07:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  |         startEvent.set() | 
					
						
							| 
									
										
										
										
											2007-03-12 18:07:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  |         ok = 0 | 
					
						
							|  |  |  |         errors = [] | 
					
						
							|  |  |  |         for t in threads: | 
					
						
							|  |  |  |             t.join() | 
					
						
							|  |  |  |             ok += t.ok_count | 
					
						
							|  |  |  |             if t.error_count: | 
					
						
							|  |  |  |                 errors.append(str(t.getName()) + str(t.errors.getvalue())) | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  |         threading_cleanup(*thread_info) | 
					
						
							| 
									
										
										
										
											2007-03-12 18:07:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok, | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  |             '\n'.join(errors)) | 
					
						
							|  |  |  |         self.assertEquals(errors, [], msg) | 
					
						
							|  |  |  |         self.assertEquals(ok, NUM_THREADS * FILES_PER_THREAD) | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-12 17:24:07 +00:00
										 |  |  | def test_main(): | 
					
						
							|  |  |  |     run_unittest(ThreadedTempFileTest) | 
					
						
							| 
									
										
										
										
											2002-09-25 20:32:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-13 03:04:02 +00:00
										 |  |  | if __name__ == "__main__": | 
					
						
							| 
									
										
										
										
											2002-09-25 20:32:28 +00:00
										 |  |  |     test_main() |