Skip to content

Commit

Permalink
Merge pull request #151 from gaffneyc/master
Browse files Browse the repository at this point in the history
Improvements to LineProtocol and have it autoload.
  • Loading branch information
tmm1 committed Feb 28, 2013
2 parents 11ef5f9 + 36b9fa7 commit 2a3ab8e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/em/protocols.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ module Protocols
autoload :Postgres3, 'em/protocols/postgres3'
autoload :ObjectProtocol, 'em/protocols/object_protocol'
autoload :Socks4, 'em/protocols/socks4'
autoload :LineProtocol, 'em/protocols/line_protocol'
end
end
4 changes: 2 additions & 2 deletions lib/em/protocols/line_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ module LineProtocol
def receive_data data
(@buf ||= '') << data

while line = @buf.slice!(/(.*)\r?\n/)
receive_line(line)
while @buf.slice!(/(.*?)\r?\n/)
receive_line($1)
end
end

Expand Down
33 changes: 33 additions & 0 deletions tests/test_line_protocol.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'em_test_helper'

class TestLineProtocol < Test::Unit::TestCase
class LineProtocolTestClass
include EM::Protocols::LineProtocol

def lines
@lines ||= []
end

def receive_line(line)
lines << line
end
end

def setup
@proto = LineProtocolTestClass.new
end

def test_simple_split_line
@proto.receive_data("this is")
assert_equal([], @proto.lines)

@proto.receive_data(" a test\n")
assert_equal(["this is a test"], @proto.lines)
end

def test_simple_lines
@proto.receive_data("aaa\nbbb\r\nccc\nddd")
assert_equal(%w(aaa bbb ccc), @proto.lines)
end

end

0 comments on commit 2a3ab8e

Please sign in to comment.