Created
February 15, 2012 04:32
-
-
Save justinko/1833212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oh, that's beautiful.