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

ruby-changes:31434

From: nobu <ko1@a...>
Date: Sat, 2 Nov 2013 15:15:09 +0900 (JST)
Subject: [ruby-changes:31434] nobu:r43513 (trunk): logger.rb: fix r43511 for Windows

nobu	2013-11-02 15:15:02 +0900 (Sat, 02 Nov 2013)

  New Revision: 43513

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

  Log:
    logger.rb: fix r43511 for Windows
    
    * lib/logger.rb (Logger::LogDevice::LogDeviceMutex#lock_shift_log):
      open file can't be removed or renamed on Windows.  [ruby-dev:47790]
      [Bug #9046]
    * test/logger/test_logger.rb (TestLogDevice#run_children): don't use
      fork.

  Modified files:
    trunk/ChangeLog
    trunk/lib/logger.rb
    trunk/test/logger/test_logger.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43512)
+++ ChangeLog	(revision 43513)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Nov  2 15:14:33 2013  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/logger.rb (Logger::LogDevice::LogDeviceMutex#lock_shift_log):
+	  open file can't be removed or renamed on Windows.  [ruby-dev:47790]
+	  [Bug #9046]
+
+	* test/logger/test_logger.rb (TestLogDevice#run_children): don't use
+	  fork.
+
 Sat Nov  2 07:08:43 2013  NARUSE, Yui  <naruse@r...>
 
 	* lib/logger.rb: Inter-process locking for log rotation
Index: lib/logger.rb
===================================================================
--- lib/logger.rb	(revision 43512)
+++ lib/logger.rb	(revision 43513)
@@ -633,8 +633,12 @@ private https://github.com/ruby/ruby/blob/trunk/lib/logger.rb#L633
       end
     end
 
-    def lock_shift_log
-      begin
+    if /mswin|mingw/ =~ RUBY_PLATFORM
+      def lock_shift_log
+        yield
+      end
+    else
+      def lock_shift_log
         retry_limit = 8
         retry_sleep = 0.1
         begin
Index: test/logger/test_logger.rb
===================================================================
--- test/logger/test_logger.rb	(revision 43512)
+++ test/logger/test_logger.rb	(revision 43513)
@@ -2,6 +2,7 @@ https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L2
 require 'test/unit'
 require 'logger'
 require 'tempfile'
+require_relative '../ruby/envutil'
 
 
 class TestLoggerSeverity < Test::Unit::TestCase
@@ -486,30 +487,16 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L487
     File.unlink(logfile1) if File.exist?(logfile1)
     File.unlink(logfile2) if File.exist?(logfile2)
     begin
-      logger = Logger.new(logfile, 4, 10)
-      r, w = IO.pipe
-      $stderr = w # To capture #warn output in Logger
-      pid1 = Process.fork do
+      stderr = run_children(2, [logfile], <<-'END')
+        logger = Logger.new(ARGV[0], 4, 10)
         10.times do
           logger.info '0' * 15
         end
-      end
-      pid2 = Process.fork do
-        10.times do
-          logger.info '0' * 15
-        end
-      end
-      Process.waitpid pid1
-      Process.waitpid pid2
-      w.close
-      stderr = r.read
-      r.close
+      END
       assert_no_match(/log shifting failed/, stderr)
       assert_no_match(/log writing failed/, stderr)
       assert_no_match(/log rotation inter-process lock failed/, stderr)
     ensure
-      $stderr = STDERR # restore
-      logger.close if logger
       File.unlink(logfile) if File.exist?(logfile)
       File.unlink(logfile0) if File.exist?(logfile0)
       File.unlink(logfile1) if File.exist?(logfile1)
@@ -523,30 +510,16 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L510
     filename2 = @filename + ".#{yyyymmdd}.1"
     filename3 = @filename + ".#{yyyymmdd}.2"
     begin
-      logger = Logger.new(@filename, 'now')
-      r, w = IO.pipe
-      $stderr = w # To capture #warn output in Logger
-      pid1 = Process.fork do
-        10.times do
-          logger.info '0' * 15
-        end
-      end
-      pid2 = Process.fork do
+      stderr = run_children(2, [@filename], <<-'END')
+        logger = Logger.new(ARGV[0], 'now')
         10.times do
           logger.info '0' * 15
         end
-      end
-      Process.waitpid pid1
-      Process.waitpid pid2
-      w.close
-      stderr = r.read
-      r.close
+      END
       assert_no_match(/log shifting failed/, stderr)
       assert_no_match(/log writing failed/, stderr)
       assert_no_match(/log rotation inter-process lock failed/, stderr)
     ensure
-      $stderr = STDERR # restore
-      logger.close if logger
       [filename1, filename2, filename3].each do |filename|
         File.unlink(filename) if File.exist?(filename)
       end
@@ -557,26 +530,34 @@ class TestLogDevice < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L530
     tmpfile = Tempfile.new([File.basename(__FILE__, '.*'), '_1.log'])
     logfile = tmpfile.path
     tmpfile.close(true)
-    logdev = Logger::LogDevice.new(logfile)
-    File.unlink(logfile) if File.exist?(logfile)
     begin
       20.times do
-        pid1 = Process.fork do
+        run_children(2, [logfile], <<-'END')
+          logfile = ARGV[0]
+          logdev = Logger::LogDevice.new(logfile)
           logdev.send(:open_logfile, logfile)
-        end
-        pid2 = Process.fork do
-          logdev.send(:open_logfile, logfile)
-        end
-        Process.waitpid pid1
-        Process.waitpid pid2
-        assert_not_equal(2, File.readlines(logfile).grep(/# Logfile created on/).size)
+        END
+        assert_equal(1, File.readlines(logfile).grep(/# Logfile created on/).size)
         File.unlink(logfile)
       end
     ensure
-      logdev.close if logdev
       File.unlink(logfile) if File.exist?(logfile)
     end
   end
+
+  private
+
+  def run_children(n, args, src)
+    r, w = IO.pipe
+    [w, *(1..n).map do
+       f = IO.popen([EnvUtil.rubybin, *%w[--disable=gems -rlogger -], *args], "w", err: w)
+       f.puts(src)
+       f
+     end].each(&:close)
+    stderr = r.read
+    r.close
+    stderr
+  end
 end
 
 

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

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