Skip to content

Instantly share code, notes, and snippets.

@justinko
Created February 15, 2012 04:32
Show Gist options
  • Save justinko/1833212 to your computer and use it in GitHub Desktop.
Save justinko/1833212 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe TCPSocket do
describe '#write_nonblock' do
let(:payload) { 'JUNK IN THE TUBES' }
it "writes if the operation won't block" do
setup_tcp do |client, peer|
client.write_nonblock(payload).should eq payload.length
peer.read(payload.length).should eq payload
end
end
it "raises Errno::EWOULDBLOCK if the operation would block" do
setup_tcp do |client, peer|
expect do
loop { client.write_nonblock(payload) }
end.to raise_exception(Errno::EWOULDBLOCK)
end
end
def setup_tcp(&block)
addr, tcp_port = '127.0.0.1', 12321
server = TCPServer.new(addr, tcp_port)
client = TCPSocket.new(addr, tcp_port)
peer = server.accept
block.call(client, peer)
client.close
server.close
peer.close
end
end
end
@dkubb
Copy link

dkubb commented Feb 15, 2012

Oh, that's beautiful.

@justinko
Copy link
Author

@dkubb thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment