Skip to content

Commit

Permalink
Fix local_file target result unstable.
Browse files Browse the repository at this point in the history
Flush before move to file from tempfile.
Fix #139
  • Loading branch information
hakobera committed Jan 7, 2017
1 parent 2151c07 commit d8dc48d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/tumugi/atomic_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class AtomicFile
def_delegators :@temp_file,
:bin_mode?, :print, :printf, :putc, :puts, :write,
:seek, :set_encoding, :sync, :sync=, :sysseek,
:syswrite, :write, :write_nonblock
:syswrite, :write, :write_nonblock, :flush

def initialize(path)
@path = path
Expand All @@ -30,6 +30,7 @@ def open(&block)

def close
if @temp_file
@temp_file.flush
move_to_final_destination(@temp_file)
@temp_file.close
@temp_file = nil
Expand Down
7 changes: 5 additions & 2 deletions test/plugin/target/local_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ class Tumugi::Plugin::LocalFileTargetTest < Test::Unit::TestCase
sub_test_case 'write' do
test 'with block' do
@target.open('w') do |f|
f.print 'done'
10000.times.each do
f.puts 'done'
end
end
assert_true(@target.exist?)
assert_equal('done', File.read(@target.path))
assert_equal("done\n"*10000, File.read(@target.path))
end

test 'without block' do
f = @target.open('w')
f.print 'done'
f.flush
f.close
assert_equal('done', File.read(@target.path))
end
Expand Down

0 comments on commit d8dc48d

Please sign in to comment.