File: tcp_socket_spec.rb

package info (click to toggle)
ruby-nio4r 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 528 kB
  • ctags: 1,100
  • sloc: ansic: 5,635; ruby: 679; java: 348; makefile: 5
file content (88 lines) | stat: -rw-r--r-- 2,393 bytes parent folder | download
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
require 'spec_helper'

describe TCPSocket do
  port_offset = 0
  let(:tcp_port) { 12345 + (port_offset += 1) }

  let :readable_subject do
    server = TCPServer.new("localhost", tcp_port)
    sock = TCPSocket.open("localhost", tcp_port)
    peer = server.accept
    peer << "data"
    sock
  end

  let :unreadable_subject do
    TCPServer.new("localhost", tcp_port)
    sock = TCPSocket.new("localhost", tcp_port)

    # Sanity check to make sure we actually produced an unreadable socket
    if select([sock], [], [], 0)
      pending "Failed to produce an unreadable socket"
    end

    sock
  end

  let :writable_subject do
    TCPServer.new("localhost", tcp_port)
    TCPSocket.new("localhost", tcp_port)
  end

  let :unwritable_subject do
    server = TCPServer.new("localhost", tcp_port)
    sock = TCPSocket.open("localhost", tcp_port)
    peer = server.accept

    begin
      sock.write_nonblock "X" * 1024
      _, writers = select [], [sock], [], 0
    end while writers and writers.include? sock

    # I think the kernel might manage to drain its buffer a bit even after
    # the socket first goes unwritable. Attempt to sleep past this and then
    # attempt to write again
    sleep 0.1

    # Once more for good measure!
    begin
      sock.write_nonblock "X" * 1024
    rescue Errno::EWOULDBLOCK
    end

    # Sanity check to make sure we actually produced an unwritable socket
    if select([], [sock], [], 0)
      pending "Failed to produce an unwritable socket"
    end

    sock
  end

  let :pair do
    server = TCPServer.new("localhost", tcp_port)
    client = TCPSocket.open("localhost", tcp_port)
    [client, server.accept]
  end

  it_behaves_like "an NIO selectable"
  it_behaves_like "an NIO selectable stream"
  it_behaves_like "an NIO bidirectional stream"

  context :connect do
    it "selects writable when connected" do
      selector = NIO::Selector.new
      server = TCPServer.new('127.0.0.1', tcp_port)

      client = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
      monitor = selector.register(client, :w)

      expect do
        client.connect_nonblock Socket.sockaddr_in(tcp_port, '127.0.0.1')
      end.to raise_exception Errno::EINPROGRESS

      selector.select(0).should include monitor
      result = client.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_ERROR)
      result.unpack('i').first.should be_zero
    end
  end
end