aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/library/socket/socket/tcp_server_loop_spec.rb
blob: 603e7b3450e37c19e8d992d5515c60c9c32a4f35 (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
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe 'Socket.tcp_server_loop' do
  describe 'when no connections are available' do
    it 'blocks the caller' do
      lambda { Socket.tcp_server_loop('127.0.0.1', 0) }.should block_caller
    end
  end

  describe 'when a connection is available' do
    before do
      @client = Socket.new(:INET, :STREAM)
      @port   = 9998
    end

    after do
      @sock.close if @sock
      @client.close
    end

    # Not working since ruby/ruby a66bc2c01194a9c017c874a30db5b3b6bd95e966
    # https://travis-ci.org/ruby/ruby/jobs/538438184
    platform_is_not :darwin do
      it 'yields a Socket and an Addrinfo' do
        @sock, addr = nil

        thread = Thread.new do
          Socket.tcp_server_loop('127.0.0.1', @port) do |socket, addrinfo|
            @sock = socket
            addr = addrinfo

            break
          end
        end

        SocketSpecs.loop_with_timeout do
          begin
            @client.connect(Socket.sockaddr_in(@port, '127.0.0.1'))
          rescue SystemCallError
            sleep 0.01
            :retry
          end
        end

        # At this point the connection has been set up but the thread may not yet
        # have returned, thus we'll need to wait a little longer for it to
        # complete.
        thread.join(2)

        @sock.should be_an_instance_of(Socket)
        addr.should be_an_instance_of(Addrinfo)
      end
    end
  end
end