summaryrefslogtreecommitdiff
path: root/spec/ruby/library/socket/socket/udp_server_loop_spec.rb
blob: 66563bc7423c8501f4575efcfb696ed8fb285b1b (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
require_relative '../spec_helper'
require_relative '../fixtures/classes'

describe 'Socket.udp_server_loop' do
  describe 'when no connections are available' do
    it 'blocks the caller' do
      -> { Socket.udp_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, :DGRAM)
      SocketSpecs::ServerLoopPortFinder.cleanup
    end

    after do
      @client.close
    end

    it 'yields the message and a Socket::UDPSource' do
      msg, src = nil

      Thread.new do
        SocketSpecs::ServerLoopPortFinder.udp_server_loop('127.0.0.1', 0) do |message, source|
          msg = message
          src = source

          break
        end
      end

      port = SocketSpecs::ServerLoopPortFinder.port

      # Because this will return even if the server is up and running (it's UDP
      # after all) we'll have to write and wait until "msg" is set.
      @client.connect(Socket.sockaddr_in(port, '127.0.0.1'))

      SocketSpecs.loop_with_timeout do
        begin
          @client.write('hello')
        rescue SystemCallError
          sleep 0.01
          :retry
        else
          unless msg
            sleep 0.001
            :retry
          end
        end
      end

      msg.should == 'hello'
      src.should be_an_instance_of(Socket::UDPSource)
    end
  end
end