summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/basicsocket/sendmsg_spec.rb
blob: 7ff336c0b707773336d726c53e6a6b94bbdbbc79 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe 'BasicSocket#sendmsg' do
  SocketSpecs.each_ip_protocol do |family, ip_address|
    describe 'using a disconnected socket' do
      before do
        @client = Socket.new(family, :DGRAM)
        @server = Socket.new(family, :DGRAM)

        @server.bind(Socket.sockaddr_in(0, ip_address))
      end

      after do
        @client.close
        @server.close
      end

      platform_is_not :windows do
        describe 'without a destination address' do
          it "raises #{SocketSpecs.dest_addr_req_error}" do
            -> { @client.sendmsg('hello') }.should raise_error(SocketSpecs.dest_addr_req_error)
          end
        end
      end

      describe 'with a destination address as a String' do
        it 'returns the amount of sent bytes' do
          @client.sendmsg('hello', 0, @server.getsockname).should == 5
        end
      end

      describe 'with a destination address as an Addrinfo' do
        it 'returns the amount of sent bytes' do
          @client.sendmsg('hello', 0, @server.connect_address).should == 5
        end
      end
    end

    describe 'using a connected UDP socket' do
      before do
        @client = Socket.new(family, :DGRAM)
        @server = Socket.new(family, :DGRAM)

        @server.bind(Socket.sockaddr_in(0, ip_address))
      end

      after do
        @client.close
        @server.close
      end

      describe 'without a destination address argument' do
        before do
          @client.connect(@server.getsockname)
        end

        it 'returns the amount of bytes written' do
          @client.sendmsg('hello').should == 5
        end
      end

      describe 'with a destination address argument' do
        before do
          @alt_server = Socket.new(family, :DGRAM)

          @alt_server.bind(Socket.sockaddr_in(0, ip_address))
        end

        after do
          @alt_server.close
        end

        it 'sends the message to the given address instead' do
          @client.sendmsg('hello', 0, @alt_server.getsockname).should == 5

          -> { @server.recv(5) }.should block_caller

          @alt_server.recv(5).should == 'hello'
        end
      end
    end

    platform_is_not :windows do # spurious
      describe 'using a connected TCP socket' do
        before do
          @client = Socket.new(family, :STREAM)
          @server = Socket.new(family, :STREAM)

          @server.bind(Socket.sockaddr_in(0, ip_address))
          @server.listen(1)

          @client.connect(@server.getsockname)
        end

        after do
          @client.close
          @server.close
        end

        it 'blocks when the underlying buffer is full' do
          # Buffer sizes may differ per platform, so sadly this is the only
          # reliable way of testing blocking behaviour.
          -> do
            10.times { @client.sendmsg('hello' * 1_000_000) }
          end.should block_caller
        end
      end
    end
  end
end