Project

General

Profile

Bug #13644 » test-logger-test_logdevice.rb.patch

test/logger/test_logdevice.rb patch file - MSP-Greg (Greg L), 06/11/2017 03:43 AM

View differences:

test/logger/test_logdevice.rb Sat Jun 10 10:10:01 2017 → test/logger/test_logdevice.rb Sat Jun 10 22:36:29 2017
684 684
    end
685 685
  end if env_tz_works
686
  # Some windows configurations do not allow software to change the mtime of
687
  # a file.  The LogDevice uses on mtime for time interval based log files.
688
  # In use this isn't an issue, but this test need to mock it.  Hence, for MinGW
689
  # builds, it prepends LogDevice.initialize (and uses atime). It may need
690
  # revision if the actual method is updated.
691
  #
686 692
  def test_shifting_midnight_exist_file
687
    Dir.mktmpdir do |tmpdir|
693
    dir = Dir.mktmpdir
694
    Dir.chdir(dir) do |tmpdir|
695
      log_ext = Time.now.strftime("log.%Y%m%d")
688 696
      assert_in_out_err([*%W"--disable=gems -rlogger -C#{tmpdir} -"], "#{<<-"begin;"}\n#{<<-'end;'}")
689 697
      begin;
698
        real_time = Time.now
690 699
        begin
691
          module FakeTime
692
            attr_accessor :now
700
          Time.singleton_class.prepend(Module.new { attr_accessor :now } )
701
          if /mingw/ =~ RUBY_PLATFORM
702
            Logger::LogDevice.prepend( Module.new do
703
              def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
704
                @dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
705
                mon_initialize
706
                set_dev(log)
707
                if @filename
708
                  @shift_age = shift_age || 7
709
                  @shift_size = shift_size || 1048576
710
                  @shift_period_suffix = shift_period_suffix || '%Y%m%d'
711
                  unless @shift_age.is_a?(Integer)
712
                    base_time = @dev.respond_to?(:stat) ? @dev.stat.atime : Time.now
713
                    @next_rotate_time = next_rotate_time(base_time, @shift_age)
714
                  end
715
                end
716
              end
717
            end)
693 718
          end
694
          class << Time
695
            prepend FakeTime
719
          # check for DST match at next midnight, else use previous
720
          if real_time.dst? == (real_time + 24 * 60 * 60).dst?
721
            ta = (real_time + 24 * 60 * 60).to_a
722
          else
723
            ta = real_time.to_a
696 724
          end
725
          tomorrow = [ ta[5], ta[4], ta[3] ]
697 726
          log = "log"
698
          File.open(log, "w") {}
699
          File.utime(*[Time.mktime(2014, 1, 2, 0, 0, 0)]*2, log)
727
          delta =  0.500                            # +/- midnight delta seconds
700
          Time.now = Time.mktime(2014, 1, 2, 23, 59, 59, 999000)
728
          Time.now = Time.local(*tomorrow, 0, 0, 0) - delta   # midnight - delta
701 729
          dev = Logger::LogDevice.new(log, shift_age: 'daily')
702 730
          dev.write("#{Time.now} hello-1\n")
703
          dev.close
704 731
          File.utime(Time.now, Time.now, log)
705
          Time.now = Time.mktime(2014, 1, 3, 1, 1, 1)
706
          dev = Logger::LogDevice.new(log, shift_age: 'daily')
732
          Time.now = Time.local(*tomorrow, 0, 0, delta)       # midnight + delta
707 733
          dev.write("#{Time.now} hello-2\n")
708
          File.utime(Time.now, Time.now, log)
709 734
        ensure
710
          dev.close
735
          dev.close if dev
711 736
        end
712 737
      end;
......
721 746
      cont = File.read(log)
722 747
      assert_match(/hello-2/, cont)
723 748
      assert_not_match(/hello-1/, cont)
724
      assert_file.for(bug).exist?(log+".20140102")
725
      assert_match(/hello-1/, File.read(log+".20140102"), bug)
749
      assert_file.for(bug).exist?(log_ext)
750
      assert_match(/hello-1/, File.read(log_ext), bug)
726 751
    end
752
    FileUtils.remove_entry dir                           # comment to view files
727 753
  end
728 754
  env_tz_works = /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb