File: test_epoll.rb

package info (click to toggle)
ruby-eventmachine 1.0.3-6%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,000 kB
  • ctags: 3,178
  • sloc: ruby: 8,641; cpp: 5,217; java: 827; makefile: 5
file content (130 lines) | stat: -rw-r--r-- 3,344 bytes parent folder | download | duplicates (2)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require 'em_test_helper'


class TestEpoll < Test::Unit::TestCase

  module TestEchoServer
    def receive_data data
      send_data data
      close_connection_after_writing
    end
  end

  module TestEchoClient
    def connection_completed
      send_data "ABCDE"
      $max += 1
    end
    def receive_data data
      raise "bad response" unless data == "ABCDE"
    end
    def unbind
      $n -= 1
      EM.stop if $n == 0
    end
  end


  if windows? || jruby?
    warn "EM.set_descriptor_table_size not implemented, skipping test in #{__FILE__}"
  else
    # We can set the rlimit/nofile of a process but we can only set it
    # higher if we're running as root.
    # On most systems, the default value is 1024.
    def test_rlimit
      unless EM.set_descriptor_table_size >= 1024
        a = EM.set_descriptor_table_size
        assert( a <= 1024 )
        a = EM.set_descriptor_table_size( 1024 )
        assert( a == 1024 )
      end
    end
  end

  # Run a high-volume version of this test by kicking the number of connections
  # up past 512. (Each connection uses two sockets, a client and a server.)
  # (Will require running the test as root)
  # This test exercises TCP clients and servers.
  #
  # XXX this test causes all sort of weird issues on OSX (when run as part of the suite)
  def _test_descriptors
    EM.epoll
    EM.set_descriptor_table_size 60000
    EM.run {
      EM.start_server "127.0.0.1", 9800, TestEchoServer
      $n = 0
      $max = 0
      100.times {
        EM.connect("127.0.0.1", 9800, TestEchoClient) {$n += 1}
      }
    }
    assert_equal(0, $n)
    assert_equal(100, $max)
  end

  def setup
    @port = next_port
  end

  module TestDatagramServer
    def receive_data dgm
      $in = dgm
      send_data "abcdefghij"
    end
  end
  module TestDatagramClient
    def initialize port
      @port = port
    end

    def post_init
      send_datagram "1234567890", "127.0.0.1", @port
    end

    def receive_data dgm
      $out = dgm
      EM.stop
    end
  end

  def test_datagrams
    $in = $out = ""
    EM.run {
      EM.open_datagram_socket "127.0.0.1", @port, TestDatagramServer
      EM.open_datagram_socket "127.0.0.1", 0, TestDatagramClient, @port
    }
    assert_equal( "1234567890", $in )
    assert_equal( "abcdefghij", $out )
  end

  # XXX this test fails randomly..
  def _test_unix_domain
    fn = "/tmp/xxx.chain"
    EM.epoll
    EM.set_descriptor_table_size 60000
    EM.run {
      # The pure-Ruby version won't let us open the socket if the node already exists.
      # Not sure, that actually may be correct and the compiled version is wrong.
      # Pure Ruby also oddly won't let us make that many connections. This test used
      # to run 100 times. Not sure where that lower connection-limit is coming from in
      # pure Ruby.
      # Let's not sweat the Unix-ness of the filename, since this test can't possibly
      # work on Windows anyway.
      #
      File.unlink(fn) if File.exist?(fn)
      EM.start_unix_domain_server fn, TestEchoServer
      $n = 0
      $max = 0
      50.times {
        EM.connect_unix_domain(fn, TestEchoClient) {$n += 1}
      }
      EM::add_timer(1) { $stderr.puts("test_unix_domain timed out!"); EM::stop }
    }
    assert_equal(0, $n)
    assert_equal(50, $max)
  ensure
    File.unlink(fn) if File.exist?(fn)
  end

end