Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Commit

Permalink
UNIXSocket: don't delegate #readline and #puts
Browse files Browse the repository at this point in the history
Delegating these two methods prevents them from using the internal
buffers and therefore may lead to blocking IO and disordered bytes.
  • Loading branch information
Hannes Georg committed Nov 7, 2015
1 parent 1f44168 commit 19a45dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/celluloid/io/unix_socket.rb
Expand Up @@ -6,7 +6,7 @@ module IO
class UNIXSocket < Stream
extend Forwardable

def_delegators :@socket, :read_nonblock, :write_nonblock, :closed?, :readline, :puts, :addr
def_delegators :@socket, :read_nonblock, :write_nonblock, :closed?, :addr

# Open a UNIX connection.
def self.open(socket_path, &block)
Expand Down
29 changes: 29 additions & 0 deletions spec/celluloid/io/unix_socket_spec.rb
Expand Up @@ -171,4 +171,33 @@
end
end
end

context 'puts' do
it 'uses the write buffer' do
with_connected_unix_sockets do |subject, peer|
subject.sync = false
subject << "a"
subject.puts "b"
subject << "c"
subject.flush
subject.close
expect(peer.read).to eq "ab\nc"
end
end
end

context 'readline' do
it 'uses the read buffer' do
with_connected_unix_sockets do |subject, peer|
peer << "xline one\nline two\n"
subject.getc # read one character to fill buffer
Timeout::timeout(1){
# this will block if the buffer is not used
expect(subject.readline).to eq "line one\n"
expect(subject.readline).to eq "line two\n"
}
end
end
end

end

0 comments on commit 19a45dc

Please sign in to comment.