[前][次][番号順一覧][スレッド一覧]

ruby-changes:34034

From: akr <ko1@a...>
Date: Sun, 25 May 2014 22:44:11 +0900 (JST)
Subject: [ruby-changes:34034] akr:r46115 (trunk): * test/lib/minitest/unit.rb: Check tempfile leak for each test class.

akr	2014-05-25 22:44:05 +0900 (Sun, 25 May 2014)

  New Revision: 46115

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=46115

  Log:
    * test/lib/minitest/unit.rb: Check tempfile leak for each test class.

  Modified files:
    trunk/ChangeLog
    trunk/test/lib/minitest/unit.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 46114)
+++ ChangeLog	(revision 46115)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 25 22:42:27 2014  Tanaka Akira  <akr@f...>
+
+	* test/lib/minitest/unit.rb: Check tempfile leak for each test class.
+
 Sun May 25 20:31:49 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* configure.in (ac_cv_func_{getcontext,setcontext}): do not
Index: test/lib/minitest/unit.rb
===================================================================
--- test/lib/minitest/unit.rb	(revision 46114)
+++ test/lib/minitest/unit.rb	(revision 46115)
@@ -923,7 +923,8 @@ module MiniTest https://github.com/ruby/ruby/blob/trunk/test/lib/minitest/unit.rb#L923
         filter === m || filter === "#{suite}##{m}"
       }
 
-      live1 = live_thread_and_tempfile
+      threads = find_threads
+      tempfiles = find_tempfiles
 
       assertions = filtered_test_methods.map { |method|
         inst = suite.new method
@@ -938,47 +939,62 @@ module MiniTest https://github.com/ruby/ruby/blob/trunk/test/lib/minitest/unit.rb#L939
         print result
         puts if @verbose
 
-        live2 = live_thread_and_tempfile
-        check_tempfile_and_thread inst, live1, live2
-        live1 = live2
+        threads = check_thread_leak inst, threads, find_threads
+
+        # find_tempfiles is too slow to run for each test method.
+        #tempfiles = check_tempfile_leak inst, tempfiles, find_tempfiles
 
         inst._assertions
       }
 
+      tempfiles = check_tempfile_leak suite, tempfiles, find_tempfiles
+
       return assertions.size, assertions.inject(0) { |sum, n| sum + n }
     end
 
-    def live_thread_and_tempfile
-      live_threads = Thread.list.find_all {|t|
+    def find_threads
+      Thread.list.find_all {|t|
         t != Thread.current && t.alive?
       }
-      if defined? Tempfile
-        live_tempfiles = ObjectSpace.each_object(Tempfile).find_all {|t|
-          t.path
-        }
-      else
-        live_tempfiles = []
-      end
-      [live_threads, live_tempfiles]
     end
 
-    def check_tempfile_and_thread(inst, live1, live2)
-      thread_finished = live1[0] - live2[0]
+    def check_thread_leak(inst, live1, live2)
+      thread_finished = live1 - live2
       if !thread_finished.empty?
         list = thread_finished.map {|t| ' ' + t.inspect }.sort.join
         puts "Finished threads: #{inst.class}\##{inst.__name__}:#{list}"
       end
-      thread_retained = live2[0] - live1[0]
+      thread_retained = live2 - live1
       if !thread_retained.empty?
         list = thread_retained.map {|t| ' ' + t.inspect }.sort.join
         puts "Leaked threads: #{inst.class}\##{inst.__name__}:#{list}"
       end
-      tempfile_retained = live2[1] - live1[1]
+      live2
+    end
+
+    def find_tempfiles
+      if defined? Tempfile
+        ObjectSpace.each_object(Tempfile).find_all {|t|
+          t.path
+        }
+      else
+        []
+      end
+    end
+
+    def check_tempfile_leak(obj, live1, live2)
+      if obj.respond_to?(:__name__)
+        name = "#{obj.class}\##{obj.__name__}"
+      else
+        name = obj.name
+      end
+      tempfile_retained = live2 - live1
       if !tempfile_retained.empty?
         list = tempfile_retained.map {|t| ' ' + t.inspect }.sort.join
-        puts "Leaked tempfiles: #{inst.class}\##{inst.__name__}:#{list}"
+        puts "Leaked tempfiles: #{name}:#{list}"
         tempfile_retained.each {|t| t.unlink }
       end
+      live2
     end
 
     ##

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]