File: recvfrom_nonblock_spec.rb

package info (click to toggle)
ruby3.3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 153,620 kB
  • sloc: ruby: 1,244,308; ansic: 836,474; yacc: 28,074; pascal: 6,748; sh: 3,913; python: 1,719; cpp: 1,158; makefile: 742; asm: 712; javascript: 394; lisp: 97; perl: 62; awk: 36; sed: 23; xml: 4
file content (102 lines) | stat: -rw-r--r-- 2,795 bytes parent folder | download | duplicates (3)
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
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe 'UDPSocket#recvfrom_nonblock' do
  SocketSpecs.each_ip_protocol do |family, ip_address, family_name|
    before do
      @server = UDPSocket.new(family)
      @client = UDPSocket.new(family)
    end

    after do
      @client.close
      @server.close
    end

    platform_is_not :windows do
      describe 'using an unbound socket' do
        it 'raises IO::WaitReadable' do
          -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable)
        end
      end
    end

    describe 'using a bound socket' do
      before do
        @server.bind(ip_address, 0)

        addr = @server.connect_address

        @client.connect(addr.ip_address, addr.ip_port)
      end

      describe 'without any data available' do
        it 'raises IO::WaitReadable' do
          -> { @server.recvfrom_nonblock(1) }.should raise_error(IO::WaitReadable)
        end

        it 'returns :wait_readable with exception: false' do
          @server.recvfrom_nonblock(1, exception: false).should == :wait_readable
        end
      end

      platform_is_not :windows do
        describe 'with data available' do
          before do
            @client.write('hello')
          end

          it 'returns an Array containing the data and an Array' do
            IO.select([@server])
            @server.recvfrom_nonblock(1).should be_an_instance_of(Array)
          end

          it 'writes the data to the buffer when one is present' do
            buffer = "".b
            IO.select([@server])
            @server.recvfrom_nonblock(1, 0, buffer)
            buffer.should == 'h'
          end

          describe 'the returned Array' do
            before do
              IO.select([@server])
              @array = @server.recvfrom_nonblock(1)
            end

            it 'contains the data at index 0' do
              @array[0].should == 'h'
            end

            it 'contains an Array at index 1' do
              @array[1].should be_an_instance_of(Array)
            end
          end

          describe 'the returned address Array' do
            before do
              IO.select([@server])
              @addr = @server.recvfrom_nonblock(1)[1]
            end

            it 'uses the correct address family' do
              @addr[0].should == family_name
            end

            it 'uses the port of the client' do
              @addr[1].should == @client.local_address.ip_port
            end

            it 'uses the hostname of the client' do
              @addr[2].should == ip_address
            end

            it 'uses the IP address of the client' do
              @addr[3].should == ip_address
            end
          end
        end
      end
    end
  end
end