summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/udpsocket/send_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/ruby/library/socket/udpsocket/send_spec.rb')
-rw-r--r--spec/ruby/library/socket/udpsocket/send_spec.rb94
1 files changed, 85 insertions, 9 deletions
diff --git a/spec/ruby/library/socket/udpsocket/send_spec.rb b/spec/ruby/library/socket/udpsocket/send_spec.rb
index 1a6f44b26e..63f5b0dcc6 100644
--- a/spec/ruby/library/socket/udpsocket/send_spec.rb
+++ b/spec/ruby/library/socket/udpsocket/send_spec.rb
@@ -1,7 +1,7 @@
-require File.expand_path('../../../../spec_helper', __FILE__)
-require File.expand_path('../../fixtures/classes', __FILE__)
+require_relative '../spec_helper'
+require_relative '../fixtures/classes'
-describe "UDPSocket.send" do
+describe "UDPSocket#send" do
before :each do
@port = nil
@server_thread = Thread.new do
@@ -34,7 +34,7 @@ describe "UDPSocket.send" do
@msg[0].should == "ad hoc"
@msg[1][0].should == "AF_INET"
- @msg[1][1].should be_kind_of(Fixnum)
+ @msg[1][1].should.is_a?(Integer)
@msg[1][3].should == "127.0.0.1"
end
@@ -46,7 +46,7 @@ describe "UDPSocket.send" do
@msg[0].should == "ad hoc"
@msg[1][0].should == "AF_INET"
- @msg[1][1].should be_kind_of(Fixnum)
+ @msg[1][1].should.is_a?(Integer)
@msg[1][3].should == "127.0.0.1"
end
@@ -59,16 +59,16 @@ describe "UDPSocket.send" do
@msg[0].should == "connection-based"
@msg[1][0].should == "AF_INET"
- @msg[1][1].should be_kind_of(Fixnum)
+ @msg[1][1].should.is_a?(Integer)
@msg[1][3].should == "127.0.0.1"
end
- it "raises EMSGSIZE if data is too too big" do
+ it "raises EMSGSIZE if data is too big" do
@socket = UDPSocket.open
begin
- lambda do
+ -> do
@socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s)
- end.should raise_error(Errno::EMSGSIZE)
+ end.should.raise(Errno::EMSGSIZE)
ensure
@socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
@socket.close
@@ -76,3 +76,79 @@ describe "UDPSocket.send" do
end
end
end
+
+describe 'UDPSocket#send' do
+ SocketSpecs.each_ip_protocol do |family, ip_address|
+ before do
+ @server = UDPSocket.new(family)
+ @client = UDPSocket.new(family)
+
+ @server.bind(ip_address, 0)
+
+ @addr = @server.connect_address
+ end
+
+ after do
+ @server.close
+ @client.close
+ end
+
+ describe 'using a disconnected socket' do
+ describe 'without a destination address' do
+ it "raises #{SocketSpecs.dest_addr_req_error}" do
+ -> { @client.send('hello', 0) }.should.raise(SocketSpecs.dest_addr_req_error)
+ end
+ end
+
+ describe 'with a destination address as separate arguments' do
+ it 'returns the amount of sent bytes' do
+ @client.send('hello', 0, @addr.ip_address, @addr.ip_port).should == 5
+ end
+
+ it 'does not persist the connection after sending data' do
+ @client.send('hello', 0, @addr.ip_address, @addr.ip_port)
+
+ -> { @client.send('hello', 0) }.should.raise(SocketSpecs.dest_addr_req_error)
+ end
+ end
+
+ describe 'with a destination address as a single String argument' do
+ it 'returns the amount of sent bytes' do
+ @client.send('hello', 0, @server.getsockname).should == 5
+ end
+ end
+ end
+
+ describe 'using a connected socket' do
+ describe 'without an explicit destination address' do
+ before do
+ @client.connect(@addr.ip_address, @addr.ip_port)
+ end
+
+ it 'returns the amount of bytes written' do
+ @client.send('hello', 0).should == 5
+ end
+ end
+
+ describe 'with an explicit destination address' do
+ before do
+ @alt_server = UDPSocket.new(family)
+
+ @alt_server.bind(ip_address, 0)
+ end
+
+ after do
+ @alt_server.close
+ end
+
+ it 'sends the data to the given address instead' do
+ @client.send('hello', 0, @alt_server.getsockname).should == 5
+
+ -> { @server.recv(5) }.should block_caller
+
+ @alt_server.recv(5).should == 'hello'
+ end
+ end
+ end
+ end
+end