summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/udpsocket/send_spec.rb
blob: 0159e25214bb165dc768a51f5c37a007a165755c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require_relative '../../../spec_helper'
require_relative '../fixtures/classes'

describe "UDPSocket.send" do
  before :each do
    @port = nil
    @server_thread = Thread.new do
      @server = UDPSocket.open
      begin
        @server.bind(nil, 0)
        @port = @server.addr[1]
        begin
          @msg = @server.recvfrom_nonblock(64)
        rescue IO::WaitReadable
          IO.select([@server])
          retry
        end
      ensure
        @server.close if !@server.closed?
      end
    end
    Thread.pass while @server_thread.status and !@port
  end

  after :each do
    @server_thread.join
  end

  it "sends data in ad hoc mode" do
    @socket = UDPSocket.open
    @socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
    @socket.close
    @server_thread.join

    @msg[0].should == "ad hoc"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Fixnum)
    @msg[1][3].should == "127.0.0.1"
  end

  it "sends data in ad hoc mode (with port given as a String)" do
    @socket = UDPSocket.open
    @socket.send("ad hoc", 0, SocketSpecs.hostname, @port.to_s)
    @socket.close
    @server_thread.join

    @msg[0].should == "ad hoc"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Fixnum)
    @msg[1][3].should == "127.0.0.1"
  end

  it "sends data in connection mode" do
    @socket = UDPSocket.open
    @socket.connect(SocketSpecs.hostname, @port)
    @socket.send("connection-based", 0)
    @socket.close
    @server_thread.join

    @msg[0].should == "connection-based"
    @msg[1][0].should == "AF_INET"
    @msg[1][1].should be_kind_of(Fixnum)
    @msg[1][3].should == "127.0.0.1"
  end

  it "raises EMSGSIZE if data is too too big" do
    @socket = UDPSocket.open
    begin
      lambda do
        @socket.send('1' * 100_000, 0, SocketSpecs.hostname, @port.to_s)
      end.should raise_error(Errno::EMSGSIZE)
    ensure
      @socket.send("ad hoc", 0, SocketSpecs.hostname, @port)
      @socket.close
      @server_thread.join
    end
  end
end