1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepCreate and delete FILES_PER_THREAD temp files (via tempfile.TemporaryFile) 3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepin each of NUM_THREADS threads, recording the number of successes and 4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfailures. A failure is a bug in tempfile, and may be due to: 5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep+ Trying to create more than one tempfile with the same name. 7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep+ Trying to delete a tempfile that doesn't still exist. 8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep+ Something we've never seen before. 9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepBy default, NUM_THREADS == 20 and FILES_PER_THREAD == 50. This is enough to 11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepcreate about 150 failures per run under Win98SE in 2.0, and runs pretty 12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepquickly. Guido reports needing to boost FILES_PER_THREAD to 500 before 13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepprovoking a 2.0 failure under Linux. 14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep""" 15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepNUM_THREADS = 20 17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepFILES_PER_THREAD = 50 18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport tempfile 20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test.test_support import threading_setup, threading_cleanup, run_unittest, import_module 22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepthreading = import_module('threading') 23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest 24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport StringIO 25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom traceback import print_exc 26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepstartEvent = threading.Event() 28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass TempFileGreedy(threading.Thread): 30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep error_count = 0 31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ok_count = 0 32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def run(self): 34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.errors = StringIO.StringIO() 35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startEvent.wait() 36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in range(FILES_PER_THREAD): 37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep try: 38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f = tempfile.TemporaryFile("w+b") 39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep f.close() 40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep except: 41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.error_count += 1 42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep print_exc(file=self.errors) 43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep else: 44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.ok_count += 1 45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass ThreadedTempFileTest(unittest.TestCase): 48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep def test_main(self): 49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep threads = [] 50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep thread_info = threading_setup() 51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for i in range(NUM_THREADS): 53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t = TempFileGreedy() 54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep threads.append(t) 55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t.start() 56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep startEvent.set() 58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ok = 0 60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errors = [] 61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep for t in threads: 62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep t.join() 63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep ok += t.ok_count 64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep if t.error_count: 65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep errors.append(str(t.getName()) + str(t.errors.getvalue())) 66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep threading_cleanup(*thread_info) 68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep msg = "Errors: errors %d ok %d\n%s" % (len(errors), ok, 70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep '\n'.join(errors)) 71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(errors, [], msg) 72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep self.assertEqual(ok, NUM_THREADS * FILES_PER_THREAD) 73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main(): 75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep run_unittest(ThreadedTempFileTest) 76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep 77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__": 78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep test_main() 79