Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Minor fix for LineAndTextProtocol #32

Open
chuckremes opened this Issue · 1 comment

1 participant

@chuckremes
From 5a77af54a27cfbf8e8354aa2c8e7c35153130e77 Mon Sep 17 00:00:00 2001
From: Chuck Remes <cremes.devlist@mac.com>
Date: Sat, 30 May 2009 14:20:33 -0500
Subject: [PATCH] Minor fix for custom delimiters in the LineAndText protocol; it was
 still "chomping" off newlines when it should have preserved them.
 Fixed it plus added a new test to verify this behavior.

---
 lib/em/protocols/line_and_text.rb |    6 +++---
 tests/test_ltp.rb                 |   20 ++++++++++++++++++--
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/lib/em/protocols/line_and_text.rb b/lib/em/protocols/line_and_text.rb
index 4b3d797..2979642 100644
--- a/lib/em/protocols/line_and_text.rb
+++ b/lib/em/protocols/line_and_text.rb
@@ -39,16 +39,16 @@ module EventMachine
       def initialize *args
         super
         hsh = args.select { |element| element if element.respond_to?(:has_key?) && element.has_key?(:delimiter) }.first
-        delimiter = hsh ? hsh[:delimiter] : "\n"
+        @delimiter = hsh ? hsh[:delimiter] : "\n"

-        lbp_init_line_state delimiter
+        lbp_init_line_state @delimiter
       end

       def receive_data data
         if @lbp_mode == :lines
           begin
             @lpb_buffer.extract(data).each do |line| 
-              receive_line(line.chomp) if respond_to?(:receive_line)
+              receive_line(line.chomp(@delimiter)) if respond_to?(:receive_line)
             end
           rescue Exception
             receive_error('overlength line') if respond_to?(:receive_error)
diff --git a/tests/test_ltp.rb b/tests/test_ltp.rb
index 59eb9f8..dbdf0d6 100644
--- a/tests/test_ltp.rb
+++ b/tests/test_ltp.rb
@@ -114,11 +114,27 @@ class TestLineAndTextProtocol < Test::Unit::TestCase
       EventMachine.add_timer(4) {assert(false, "test timed out")}

       EventMachine.connect TestHost, TestPort, StopClient do |c|
-        c.send_data "aaa|bbb|ccc|"
+        c.send_data "aaa|bbb|ccc"
         c.close_connection_after_writing
       end
     }
-    assert_equal( %w(aaa bbb ccc), lines_received )
+    assert_equal( ["aaa", "bbb"], lines_received )
+  end
+
+  def test_custom_delimiter_preserves_newlines
+    lines_received = []
+    EventMachine.run {
+      EventMachine.start_server( TestHost, TestPort, SimpleLineTest, {:delimiter => "|"} ) do |conn|
+        conn.instance_eval "@line_buffer = lines_received"
+      end
+      EventMachine.add_timer(4) {assert(false, "test timed out")}
+
+      EventMachine.connect TestHost, TestPort, StopClient do |c|
+        c.send_data "aaa|bbb|ccc|\n|"
+        c.close_connection_after_writing
+      end
+    }
+    assert_equal( ["aaa", "bbb", "ccc", "\n"], lines_received )
   end


-- 
1.6.2.4
@chuckremes

Alternately, this can be pulled from my fork in the "ltp" branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.