home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_threadedtempfile.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  2.3 KB  |  87 lines

  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.  Run the test alone to boost either
  14. via cmdline switches:
  15.  
  16. -f  FILES_PER_THREAD (int)
  17. -t  NUM_THREADS (int)
  18. """
  19.  
  20. NUM_THREADS = 20        # change w/ -t option
  21. FILES_PER_THREAD = 50   # change w/ -f option
  22.  
  23. import thread # If this fails, we can't test this module
  24. import threading
  25. from test_support import TestFailed
  26. import StringIO
  27. from traceback import print_exc
  28.  
  29. startEvent = threading.Event()
  30.  
  31. import tempfile
  32. tempfile.gettempdir() # Do this now, to avoid spurious races later
  33.  
  34. class TempFileGreedy(threading.Thread):
  35.     error_count = 0
  36.     ok_count = 0
  37.  
  38.     def run(self):
  39.         self.errors = StringIO.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. def _test():
  52.     threads = []
  53.  
  54.     print "Creating"
  55.     for i in range(NUM_THREADS):
  56.         t = TempFileGreedy()
  57.         threads.append(t)
  58.         t.start()
  59.  
  60.     print "Starting"
  61.     startEvent.set()
  62.  
  63.     print "Reaping"
  64.     ok = errors = 0
  65.     for t in threads:
  66.         t.join()
  67.         ok += t.ok_count
  68.         errors += t.error_count
  69.         if t.error_count:
  70.             print '%s errors:\n%s' % (t.getName(), t.errors.getvalue())
  71.  
  72.     msg = "Done: errors %d ok %d" % (errors, ok)
  73.     print msg
  74.     if errors:
  75.         raise TestFailed(msg)
  76.  
  77. if __name__ == "__main__":
  78.     import sys, getopt
  79.     opts, args = getopt.getopt(sys.argv[1:], "t:f:")
  80.     for o, v in opts:
  81.         if o == "-f":
  82.             FILES_PER_THREAD = int(v)
  83.         elif o == "-t":
  84.             NUM_THREADS = int(v)
  85.  
  86. _test()
  87.