Permalink
Browse files

Added encoding support to FileTail when running in 1.9

  • Loading branch information...
iconara committed Feb 16, 2011
1 parent 9d6733b commit e11f162e3303bd44d9224b609de82adbfce987cb
Showing with 23 additions and 0 deletions.
  1. +3 −0 lib/em/filetail.rb
  2. +20 −0 test/test_filetail.rb
View
@@ -38,6 +38,8 @@ class EventMachine::FileTail
CHUNKSIZE = 65536
#MAXSLEEP = 2
+
+ FORCE_ENCODING = !! (defined? Encoding)
# The path of the file being tailed
attr_reader :path
@@ -254,6 +256,7 @@ def read
@logger.debug "#{self}: Reading..."
begin
data = @file.sysread(CHUNKSIZE)
+ data.force_encoding(@file.external_encoding) if FORCE_ENCODING
# Won't get here if sysread throws EOF
@position += data.length
View
@@ -208,5 +208,25 @@ def test_filetail_tracks_symlink_changes
File.delete(f.path)
end
end # def test_filetail_tracks_renames
+
+ def test_encoding
+ return if RUBY_VERSION < '1.9.0'
+ tmp = Tempfile.new("testfiletail")
+ data = DATA.clone
+ EM.run do
+ abort_after_timeout(1)
+
+ EM::file_tail(tmp.path) do |filetail, line|
+ assert_equal(Encoding.default_external, line.encoding,
+ "Expected the read data to have the encoding specified in Encoding.default_external (#{Encoding.default_external}, but was #{line.encoding})")
+ finish
+ end
+
+ EM.next_tick do
+ tmp.puts(data.shift)
+ tmp.flush
+ end
+ end # EM.run
+ end # def test_encoding
end # class TestFileTail

0 comments on commit e11f162

Please sign in to comment.