From d7bac4a190c49474101be1628dba17ad4e5e68e9 Mon Sep 17 00:00:00 2001 From: Brian Palmer Date: Mon, 4 Feb 2013 14:58:38 -0700 Subject: [PATCH] fix for a ruby 1.9 warning message Backported from ruby 2.0 (and possibly later 1.9 patchlevels) because it was filling our logs with many millions of warnings. The code was backported directly, the test was backported with conversion from minitest -> rspec. test plan: Delayed jobs that use ruby's net libraries should still work, such as sending messages, taking web snapshots, etc. You shouldn't see any further lines like this in the log: net/protocol.rb:313: warning: regexp match /.../n against to UTF-8 string Change-Id: Ic29daa4ee9f0587fb2a1ac2968cd08a2f68365a1 Reviewed-on: https://gerrit.instructure.com/17431 Tested-by: Jenkins Reviewed-by: Brian Palmer QA-Review: Brian Palmer --- config/initializers/ruby_version_compat.rb | 14 +++++++++++ spec/initializers/ruby_version_compat_spec.rb | 23 +++++++++++++++++++ spec/spec_helper.rb | 10 ++++++++ 3 files changed, 47 insertions(+) create mode 100644 spec/initializers/ruby_version_compat_spec.rb diff --git a/config/initializers/ruby_version_compat.rb b/config/initializers/ruby_version_compat.rb index f7086ef76a6..bea3418e9eb 100644 --- a/config/initializers/ruby_version_compat.rb +++ b/config/initializers/ruby_version_compat.rb @@ -144,4 +144,18 @@ else super.force_encoding('utf-8') end end + + # Fix for https://bugs.ruby-lang.org/issues/7278 , which was filling up our logs with these warnings + if RUBY_VERSION < "2." + require 'net/protocol' + class Net::InternetMessageIO + def each_crlf_line(src) + buffer_filling(@wbuf, src) do + while line = @wbuf.slice!(/\A[^\r\n]*(?:\n|\r(?:\n|(?!\z)))/) + yield line.chomp("\n") + "\r\n" + end + end + end + end + end end diff --git a/spec/initializers/ruby_version_compat_spec.rb b/spec/initializers/ruby_version_compat_spec.rb new file mode 100644 index 00000000000..dfa43cd79a4 --- /dev/null +++ b/spec/initializers/ruby_version_compat_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../spec_helper', File.dirname( __FILE__ )) + +describe 'ruby_version_compat' do + describe 'backport of ruby #7278' do + it "should not output to stdout/stderr" do + pending("ruby 1.9+ only") if RUBY_VERSION < "1.9." + + output = capture_io do + sio = StringIO.new("") + imio = Net::InternetMessageIO.new(sio) + imio.write_message("\u3042\r\u3044\n\u3046\r\n\u3048").should == 23 + sio.string.should == "\u3042\r\n\u3044\r\n\u3046\r\n\u3048\r\n.\r\n".force_encoding('us-ascii') + + sio = StringIO.new("") + imio = Net::InternetMessageIO.new(sio) + imio.write_message("\u3042\r").should == 8 + sio.string.should == "\u3042\r\n.\r\n".force_encoding('us-ascii') + end + + output.should == ['', ''] + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4ff2870704e..78a3bac9ec3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -864,6 +864,16 @@ Spec::Runner.configure do |config| str end + # from minitest, MIT licensed + def capture_io + orig_stdout, orig_stderr = $stdout, $stderr + $stdout, $stderr = StringIO.new, StringIO.new + yield + return $stdout.string, $stderr.string + ensure + $stdout, $stderr = orig_stdout, orig_stderr + end + def verify_post_matches(post_lines, expected_post_lines) # first lines should match post_lines[0].should == expected_post_lines[0]