summaryrefslogtreecommitdiff
path: root/test/socket/test_nonblock.rb
blob: 5a4688bac3421ab764a70298072fe35f8b36b538 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# frozen_string_literal: true

begin
  require "socket"
  require "io/nonblock"
  require "io/wait"
rescue LoadError
end

require "test/unit"
require "tempfile"
require "timeout"

class TestSocketNonblock < Test::Unit::TestCase
  def test_accept_nonblock
    serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    serv.listen(5)
    assert_raise(IO::WaitReadable) { serv.accept_nonblock }
    assert_equal :wait_readable, serv.accept_nonblock(exception: false)
    assert_raise(IO::WaitReadable) { serv.accept_nonblock(exception: true) }
    c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    c.connect(serv.getsockname)
    begin
      s, sockaddr = serv.accept_nonblock
    rescue IO::WaitReadable
      IO.select [serv]
      s, sockaddr = serv.accept_nonblock
    end
    assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
    if s.respond_to?(:nonblock?)
      assert_predicate(s, :nonblock?, 'accepted socket is non-blocking')
    end
  ensure
    serv.close if serv
    c.close if c
    s.close if s
  end

  def test_connect_nonblock
    serv = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    serv.listen(5)
    c = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    servaddr = serv.getsockname
    begin
      c.connect_nonblock(servaddr)
    rescue IO::WaitWritable
      IO.select nil, [c]
      assert_nothing_raised {
        begin
          c.connect_nonblock(servaddr)
        rescue Errno::EISCONN
        end
      }
    end
    s, sockaddr = serv.accept
    assert_equal(Socket.unpack_sockaddr_in(c.getsockname), Socket.unpack_sockaddr_in(sockaddr))
  ensure
    serv.close if serv
    c.close if c
    s.close if s
  end

  def test_connect_nonblock_no_exception
    serv = Socket.new(:INET, :STREAM)
    serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    serv.listen(5)
    c = Socket.new(:INET, :STREAM)
    servaddr = serv.getsockname
    rv = c.connect_nonblock(servaddr, exception: false)
    case rv
    when 0
      # some OSes return immediately on non-blocking local connect()
    else
      assert_equal :wait_writable, rv
    end
    assert_equal([ [], [c], [] ], IO.select(nil, [c], nil, 60))
    assert_equal(0, c.connect_nonblock(servaddr, exception: false),
                 'there should be no EISCONN error')
    s, sockaddr = serv.accept
    assert_equal(Socket.unpack_sockaddr_in(c.getsockname),
                 Socket.unpack_sockaddr_in(sockaddr))
  ensure
    serv.close if serv
    c.close if c
    s.close if s
  end

  def test_udp_recvfrom_nonblock
    u1 = UDPSocket.new
    u2 = UDPSocket.new
    u1.bind("127.0.0.1", 0)
    assert_raise(IO::WaitReadable) { u1.recvfrom_nonblock(100) }
    assert_raise(IO::WaitReadable, Errno::EINVAL) { u2.recvfrom_nonblock(100) }
    u2.send("aaa", 0, u1.getsockname)
    IO.select [u1]
    mesg, inet_addr = u1.recvfrom_nonblock(100)
    assert_equal(4, inet_addr.length)
    assert_equal("aaa", mesg)
    _, port, _, _ = inet_addr
    u2_port, _ = Socket.unpack_sockaddr_in(u2.getsockname)
    assert_equal(u2_port, port)
    assert_raise(IO::WaitReadable) { u1.recvfrom_nonblock(100) }
    u2.send("", 0, u1.getsockname)
    assert_nothing_raised("cygwin 1.5.19 has a problem to send an empty UDP packet. [ruby-dev:28915]") {
      Timeout.timeout(1) { IO.select [u1] }
    }
    mesg, inet_addr = u1.recvfrom_nonblock(100)
    assert_equal("", mesg)
  ensure
    u1.close if u1
    u2.close if u2
  end

  def test_udp_recv_nonblock
    u1 = UDPSocket.new
    u2 = UDPSocket.new
    u1.bind("127.0.0.1", 0)
    assert_raise(IO::WaitReadable) { u1.recv_nonblock(100) }
    assert_raise(IO::WaitReadable, Errno::EINVAL) { u2.recv_nonblock(100) }
    u2.send("aaa", 0, u1.getsockname)
    IO.select [u1]
    mesg = u1.recv_nonblock(100)
    assert_equal("aaa", mesg)
    assert_raise(IO::WaitReadable) { u1.recv_nonblock(100) }
    u2.send("", 0, u1.getsockname)
    assert_nothing_raised("cygwin 1.5.19 has a problem to send an empty UDP packet. [ruby-dev:28915]") {
      Timeout.timeout(1) { IO.select [u1] }
    }
    mesg = u1.recv_nonblock(100)
    assert_equal("", mesg)

    buf = "short".dup
    out = "hello world" * 4
    out.freeze
    u2.send(out, 0, u1.getsockname)
    IO.select [u1]
    rv = u1.recv_nonblock(100, 0, buf)
    assert_equal rv.object_id, buf.object_id
    assert_equal out, rv
    assert_equal out, buf
  ensure
    u1.close if u1
    u2.close if u2
  end

  def test_socket_recvfrom_nonblock
    s1 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
    s1.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    s2 = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM, 0)
    assert_raise(IO::WaitReadable) { s1.recvfrom_nonblock(100) }
    assert_raise(IO::WaitReadable, Errno::EINVAL) { s2.recvfrom_nonblock(100) }
    s2.send("aaa", 0, s1.getsockname)
    IO.select [s1]
    mesg, sockaddr = s1.recvfrom_nonblock(100)
    assert_equal("aaa", mesg)
    port, _ = Socket.unpack_sockaddr_in(sockaddr)
    s2_port, _ = Socket.unpack_sockaddr_in(s2.getsockname)
    assert_equal(s2_port, port)
  ensure
    s1.close if s1
    s2.close if s2
  end

  def tcp_pair
    serv = TCPServer.new("127.0.0.1", 0)
    _, port, _, addr = serv.addr
    c = TCPSocket.new(addr, port)
    s = serv.accept
    if block_given?
      begin
        yield c, s
      ensure
        c.close if !c.closed?
        s.close if !s.closed?
      end
    else
      return c, s
    end
  ensure
    serv.close if serv && !serv.closed?
  end

  def udp_pair
    s1 = UDPSocket.new
    s1.bind('127.0.0.1', 0)
    _, port1, _, addr1 = s1.addr

    s2 = UDPSocket.new
    s2.bind('127.0.0.1', 0)
    _, port2, _, addr2 = s2.addr

    s1.connect(addr2, port2)
    s2.connect(addr1, port1)

    if block_given?
      begin
        yield s1, s2
      ensure
        s1.close if !s1.closed?
        s2.close if !s2.closed?
      end
    else
      return s1, s2
    end
  end

  def test_tcp_recv_nonblock
    c, s = tcp_pair
    assert_raise(IO::WaitReadable) { c.recv_nonblock(100) }
    assert_raise(IO::WaitReadable) { s.recv_nonblock(100) }
    c.write("abc")
    IO.select [s]
    assert_equal("a", s.recv_nonblock(1))
    assert_equal("bc", s.recv_nonblock(100))
    assert_raise(IO::WaitReadable) { s.recv_nonblock(100) }
  ensure
    c.close if c
    s.close if s
  end

  def test_read_nonblock
    c, s = tcp_pair
    assert_raise(IO::WaitReadable) { c.read_nonblock(100) }
    assert_raise(IO::WaitReadable) { s.read_nonblock(100) }
    c.write("abc")
    IO.select [s]
    assert_equal("a", s.read_nonblock(1))
    assert_equal("bc", s.read_nonblock(100))
    assert_raise(IO::WaitReadable) { s.read_nonblock(100) }
  ensure
    c.close if c
    s.close if s
  end

  def test_read_nonblock_no_exception
    c, s = tcp_pair
    assert_equal :wait_readable, c.read_nonblock(100, exception: false)
    assert_equal :wait_readable, s.read_nonblock(100, exception: false)
    c.write("abc")
    IO.select [s]
    assert_equal("a", s.read_nonblock(1, exception: false))
    assert_equal("bc", s.read_nonblock(100, exception: false))
    assert_equal :wait_readable, s.read_nonblock(100, exception: false)
  ensure
    c.close if c
    s.close if s
  end

=begin
  def test_write_nonblock
    c, s = tcp_pair
    str = "a" * 10000
    _, ws, _ = IO.select(nil, [c], nil)
    assert_equal([c], ws)
    ret = c.write_nonblock(str)
    assert_operator(ret, :>, 0)
    loop {
      assert_raise(IO::WaitWritable) {
        loop {
          ret = c.write_nonblock(str)
          assert_operator(ret, :>, 0)
        }
      }
      _, ws, _ = IO.select(nil, [c], nil, 0)
      break if !ws
    }
  ensure
    c.close if c
    s.close if s
  end
=end

  def test_sendmsg_nonblock_error
    udp_pair {|s1, s2|
      begin
        loop {
          s1.sendmsg_nonblock("a" * 100000)
        }
      rescue NotImplementedError, Errno::ENOSYS
        omit "sendmsg not implemented on this platform: #{$!}"
      rescue Errno::EMSGSIZE
        # UDP has 64K limit (if no Jumbograms).  No problem.
      rescue Errno::EWOULDBLOCK
        assert_kind_of(IO::WaitWritable, $!)
      end
    }
  end

  def test_recvfrom_nonblock_no_exception
    udp_pair do |s1, s2|
      assert_equal :wait_readable, s1.recvfrom_nonblock(100, exception: false)
      s2.send("aaa", 0)
      assert_predicate s1, :wait_readable
      mesg, inet_addr = s1.recvfrom_nonblock(100, exception: false)
      assert_equal(4, inet_addr.length)
      assert_equal("aaa", mesg)
    end
  end

  if defined?(UNIXSocket) && defined?(Socket::SOCK_SEQPACKET)
    def test_sendmsg_nonblock_seqpacket
      buf = '*' * 4096
      UNIXSocket.pair(:SEQPACKET) do |s1, s2|
        assert_raise(IO::WaitWritable) do
          loop { s1.sendmsg_nonblock(buf) }
        end
      end
    rescue NotImplementedError, Errno::ENOSYS, Errno::EPROTONOSUPPORT, Errno::EPROTOTYPE
      omit "UNIXSocket.pair(:SEQPACKET) not implemented on this platform: #{$!}"
    end

    def test_sendmsg_nonblock_no_exception
      omit "AF_UNIX + SEQPACKET is not supported on windows" if /mswin|mingw/ =~ RUBY_PLATFORM

      buf = '*' * 4096
      UNIXSocket.pair(:SEQPACKET) do |s1, s2|
        n = 0
        Timeout.timeout(60) do
          case rv = s1.sendmsg_nonblock(buf, exception: false)
          when Integer
            n += rv
          when :wait_writable
            break
          else
            flunk "unexpected return value: #{rv.inspect}"
          end while true
          assert_equal :wait_writable, rv
          assert_operator n, :>, 0
        end
      end
    rescue NotImplementedError, Errno::ENOSYS, Errno::EPROTONOSUPPORT
      omit "UNIXSocket.pair(:SEQPACKET) not implemented on this platform: #{$!}"
    end
  end

  def test_recvmsg_nonblock_error
    udp_pair {|s1, s2|
      begin
        s1.recvmsg_nonblock(4096)
      rescue NotImplementedError
        omit "recvmsg not implemented on this platform."
      rescue Errno::EWOULDBLOCK
        assert_kind_of(IO::WaitReadable, $!)
      end
      assert_equal :wait_readable, s1.recvmsg_nonblock(11, exception: false)
    }
  end

  def test_recv_nonblock_error
    tcp_pair {|c, s|
      begin
        c.recv_nonblock(4096)
      rescue Errno::EWOULDBLOCK
        assert_kind_of(IO::WaitReadable, $!)
      end
    }
  end

  def test_recv_nonblock_no_exception
    tcp_pair {|c, s|
      assert_equal :wait_readable, c.recv_nonblock(11, exception: false)
      s.write('HI')
      assert_predicate c, :wait_readable
      assert_equal 'HI', c.recv_nonblock(11, exception: false)
      assert_equal :wait_readable, c.recv_nonblock(11, exception: false)
    }
  end

  def test_connect_nonblock_error
    serv = TCPServer.new("127.0.0.1", 0)
    _, port, _, _ = serv.addr
    c = Socket.new(:INET, :STREAM)
    begin
      c.connect_nonblock(Socket.sockaddr_in(port, "127.0.0.1"))
    rescue Errno::EINPROGRESS
      assert_kind_of(IO::WaitWritable, $!)
    end
  ensure
    serv.close if serv && !serv.closed?
    c.close if c && !c.closed?
  end

  def test_accept_nonblock_error
    serv = Socket.new(:INET, :STREAM)
    serv.bind(Socket.sockaddr_in(0, "127.0.0.1"))
    serv.listen(5)
    begin
      s, _ = serv.accept_nonblock
    rescue Errno::EWOULDBLOCK
      assert_kind_of(IO::WaitReadable, $!)
    end
  ensure
    serv.close if serv && !serv.closed?
    s.close if s && !s.closed?
  end

end if defined?(Socket)