Bug #13644 » test-logger-test_logdevice.rb.patch
test/logger/test_logdevice.rb Sat Jun 10 10:10:01 2017 → test/logger/test_logdevice.rb Sat Jun 10 22:36:29 2017 | ||
---|---|---|
end
|
||
end if env_tz_works
|
||
# Some windows configurations do not allow software to change the mtime of
|
||
# a file. The LogDevice uses on mtime for time interval based log files.
|
||
# In use this isn't an issue, but this test need to mock it. Hence, for MinGW
|
||
# builds, it prepends LogDevice.initialize (and uses atime). It may need
|
||
# revision if the actual method is updated.
|
||
#
|
||
def test_shifting_midnight_exist_file
|
||
Dir.mktmpdir do |tmpdir|
|
||
dir = Dir.mktmpdir
|
||
Dir.chdir(dir) do |tmpdir|
|
||
log_ext = Time.now.strftime("log.%Y%m%d")
|
||
assert_in_out_err([*%W"--disable=gems -rlogger -C#{tmpdir} -"], "#{<<-"begin;"}\n#{<<-'end;'}")
|
||
begin;
|
||
real_time = Time.now
|
||
begin
|
||
module FakeTime
|
||
attr_accessor :now
|
||
Time.singleton_class.prepend(Module.new { attr_accessor :now } )
|
||
if /mingw/ =~ RUBY_PLATFORM
|
||
Logger::LogDevice.prepend( Module.new do
|
||
def initialize(log = nil, shift_age: nil, shift_size: nil, shift_period_suffix: nil)
|
||
@dev = @filename = @shift_age = @shift_size = @shift_period_suffix = nil
|
||
mon_initialize
|
||
set_dev(log)
|
||
if @filename
|
||
@shift_age = shift_age || 7
|
||
@shift_size = shift_size || 1048576
|
||
@shift_period_suffix = shift_period_suffix || '%Y%m%d'
|
||
unless @shift_age.is_a?(Integer)
|
||
base_time = @dev.respond_to?(:stat) ? @dev.stat.atime : Time.now
|
||
@next_rotate_time = next_rotate_time(base_time, @shift_age)
|
||
end
|
||
end
|
||
end
|
||
end)
|
||
end
|
||
class << Time
|
||
prepend FakeTime
|
||
# check for DST match at next midnight, else use previous
|
||
if real_time.dst? == (real_time + 24 * 60 * 60).dst?
|
||
ta = (real_time + 24 * 60 * 60).to_a
|
||
else
|
||
ta = real_time.to_a
|
||
end
|
||
tomorrow = [ ta[5], ta[4], ta[3] ]
|
||
log = "log"
|
||
File.open(log, "w") {}
|
||
File.utime(*[Time.mktime(2014, 1, 2, 0, 0, 0)]*2, log)
|
||
delta = 0.500 # +/- midnight delta seconds
|
||
Time.now = Time.mktime(2014, 1, 2, 23, 59, 59, 999000)
|
||
Time.now = Time.local(*tomorrow, 0, 0, 0) - delta # midnight - delta
|
||
dev = Logger::LogDevice.new(log, shift_age: 'daily')
|
||
dev.write("#{Time.now} hello-1\n")
|
||
dev.close
|
||
File.utime(Time.now, Time.now, log)
|
||
Time.now = Time.mktime(2014, 1, 3, 1, 1, 1)
|
||
dev = Logger::LogDevice.new(log, shift_age: 'daily')
|
||
Time.now = Time.local(*tomorrow, 0, 0, delta) # midnight + delta
|
||
dev.write("#{Time.now} hello-2\n")
|
||
File.utime(Time.now, Time.now, log)
|
||
ensure
|
||
dev.close
|
||
dev.close if dev
|
||
end
|
||
end;
|
||
... | ... | |
cont = File.read(log)
|
||
assert_match(/hello-2/, cont)
|
||
assert_not_match(/hello-1/, cont)
|
||
assert_file.for(bug).exist?(log+".20140102")
|
||
assert_match(/hello-1/, File.read(log+".20140102"), bug)
|
||
assert_file.for(bug).exist?(log_ext)
|
||
assert_match(/hello-1/, File.read(log_ext), bug)
|
||
end
|
||
FileUtils.remove_entry dir # comment to view files
|
||
end
|
||
env_tz_works = /linux|darwin|freebsd/ =~ RUBY_PLATFORM # borrow from test/ruby/test_time_tz.rb
|