6 require 'sleepy_penguin'
8 class TestEpoll < Test::Unit::TestCase
10 RBX = defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'rbx')
18 Epoll.constants.each do |const|
19 next if const.to_sym == :IO
20 nr = Epoll.const_get(const)
21 assert nr <= 0xffffffff, "#{const}=#{nr}"
27 Thread.new { sleep 0.100; @ep.add(@wr, Epoll::OUT) }
29 @ep.wait { |flags,obj| tmp << [ flags, obj ] }
30 elapsed = Time.now - t0
31 assert elapsed >= 0.100
32 assert_equal [[Epoll::OUT, @wr]], tmp
37 @ep.add @rd, Epoll::IN
39 @ep.wait(nil, 100) { |flags,obj| tmp << [ flags, obj ] }
43 _, status = Process.waitpid2(pid)
44 assert status.success?
45 @ep.wait(nil, 0) { |flags,obj| tmp << [ flags, obj ] }
46 assert_equal [[Epoll::IN, @rd]], tmp
49 def test_after_fork_usability
50 fork { @ep.add(@rd, Epoll::IN); exit!(0) }
51 fork { @ep.set(@rd, Epoll::IN); exit!(0) }
52 fork { @ep.to_io; exit!(0) }
53 fork { @ep.dup; exit!(0) }
54 fork { @ep.clone; exit!(0) }
55 fork { @ep.close; exit!(0) }
56 fork { @ep.closed?; exit!(0) }
66 res.each { |(pid,status)| assert status.success? }
69 def test_tcp_connect_nonblock_edge
70 epflags = Epoll::OUT | Epoll::ET
72 srv = TCPServer.new(host, 0)
74 addr = Socket.pack_sockaddr_in(port, host)
75 sock = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
76 assert_raises(Errno::EINPROGRESS) { sock.connect_nonblock(addr) }
77 IO.select(nil, [ sock ], [sock ])
78 @ep.add(sock, epflags)
80 @ep.wait(1) { |flags, obj| tmp << [ flags, obj ] }
81 assert_equal [ [Epoll::OUT, sock] ], tmp
84 def test_tcp_connect_edge
85 epflags = Epoll::OUT | Epoll::ET
87 srv = TCPServer.new(host, 0)
89 sock = TCPSocket.new(host, port)
90 @ep.add(sock, epflags)
92 @ep.wait(1) { |flags, obj| tmp << [ flags, obj ] }
93 assert_equal [ [Epoll::OUT, sock] ], tmp
97 assert_nothing_raised do
98 @rd.close unless @rd.closed?
99 @wr.close unless @wr.closed?
100 @ep.close unless @ep.closed?
104 def test_max_events_big
105 @ep.add @rd, Epoll::IN
107 thr = Thread.new { @ep.wait(1024) { |flags, obj| tmp << [ flags, obj ] } }
112 assert_equal([[Epoll::IN, @rd]], tmp)
114 thr = Thread.new { @ep.wait { |flags, obj| tmp << [ flags, obj ] } }
116 assert_equal([[Epoll::IN, @rd]], tmp)
119 def test_max_events_small
120 @ep.add @rd, Epoll::IN | Epoll::ET
121 @ep.add @wr, Epoll::OUT | Epoll::ET
124 @ep.wait(1) { |flags, obj| tmp << [ flags, obj ] }
125 assert_equal 1, tmp.size
126 @ep.wait(1) { |flags, obj| tmp << [ flags, obj ] }
127 assert_equal 2, tmp.size
132 trap(:USR1) { time[:USR1] = Time.now; sleep 0.1; @wr.write '.' }
133 @ep.add @rd, Epoll::IN
136 sleep 0.1 # slightly racy :<
137 Process.kill(:USR1, Process.ppid)
139 time[:START_WAIT] = Time.now
141 @ep.wait { |flags, obj| tmp << [ flags, obj ]; time[:EP] = Time.now }
145 assert_equal([[Epoll::IN, @rd]], tmp)
146 _, status = Process.waitpid2(pid)
147 assert status.success?
148 assert((time[:USR1] - time[:START_WAIT]) >= 0.1)
149 assert((time[:USR1] - time[:START_WAIT]) < 0.15)
150 assert((time[:EP] - time[:USR1]) >= 0.1)
151 assert((time[:EP] - time[:USR1]) < 0.15)
153 trap(:USR1, 'DEFAULT')
157 @ep.add @rd, Epoll::IN
159 thr = Thread.new { @ep.wait { |flags, obj| tmp << [ flags, obj ] } }
162 assert_nil thr.join(0.01)
169 defined?(Epoll::RDHUP) or
170 return warn "skipping test, EPOLLRDHUP not available"
171 rd, wr = UNIXSocket.pair
172 @ep.add rd, Epoll::RDHUP
174 thr = Thread.new { @ep.wait { |flags, obj| tmp << [ flags, obj ] } }
175 wr.shutdown Socket::SHUT_WR
177 assert_equal([[ Epoll::RDHUP, rd ]], tmp)
181 @ep.add @rd, Epoll::IN
183 thr = Thread.new { @ep.wait { |flags, obj| tmp << [ flags, obj ] } }
186 assert_equal([[ Epoll::HUP, @rd ]], tmp)
191 assert_nothing_raised do
193 @ep.add @rd, Epoll::IN
194 @ep.add w, Epoll::OUT
195 @ep.add @wr, Epoll::OUT
198 @ep.wait { |flags, obj| tmp << [ flags, obj ] }
199 assert_equal 2, tmp.size
200 assert_equal [ Epoll::OUT ], tmp.map { |flags, obj| flags }.uniq
201 ios = tmp.map { |flags, obj| obj }
202 assert ios.include?(@wr)
203 assert ios.include?(w)
207 assert_nothing_raised { 4096.times { Epoll.new } }
212 assert_nothing_raised do
224 assert @ep.to_io.fileno != clone.to_io.fileno
225 clone.add @wr, Epoll::OUT
226 @ep.wait(nil, 0) { |flags, obj| tmp << [ flags, obj ] }
227 assert_equal([[Epoll::OUT, @wr]], tmp)
228 assert_nothing_raised { clone.close }
234 assert @ep.to_io.fileno != clone.to_io.fileno
235 clone.add @wr, Epoll::OUT
236 @ep.wait(nil, 0) { |flags, obj| tmp << [ flags, obj ] }
237 assert_equal([[Epoll::OUT, @wr]], tmp)
238 assert_nothing_raised { clone.close }
241 def test_set_idempotency
242 assert_nothing_raised do
243 @ep.set @rd, Epoll::IN
244 @ep.set @rd, Epoll::IN
245 @ep.set @wr, Epoll::OUT
246 @ep.set @wr, Epoll::OUT
250 def test_wait_timeout
252 assert_equal 0, @ep.wait(nil, 100) { |flags,obj| assert false }
254 assert(diff >= 0.075, "#{diff} < 0.100s")
258 assert_raises(Errno::ENOENT) { @ep.del(@rd) }
259 assert_nothing_raised do
260 @ep.add(@rd, Epoll::IN)
266 @ep.add(@rd, Epoll::IN)
267 assert_equal 0, @ep.wait(nil, 0) { |flags,obj| assert false }
270 nr = @ep.wait(nil, 0) do |flags,obj|
271 assert_equal Epoll::IN, flags
272 assert_equal obj, @rd
280 @ep.add(@wr, Epoll::OUT | Epoll::IN)
282 nr = @ep.wait(nil, 0) do |flags, obj|
283 assert_equal Epoll::OUT, flags
284 assert_equal obj, @wr
291 def test_wait_write_blocked
293 @wr.write_nonblock('.' * 65536)
297 @ep.add(@wr, Epoll::OUT | Epoll::IN)
299 assert_equal 0, @ep.wait(nil, 0) { |flags,event| assert false }
304 @ep.add @rd, Epoll::IN
305 thr = Thread.new { tmp = IO.select([ @ep ]) }
310 assert_equal([[@ep],[],[]], tmp)
313 def test_new_no_cloexec
315 io = Epoll.new(0).to_io
316 assert((io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC) == 0)
321 io = Epoll.new(Epoll::CLOEXEC).to_io
322 assert((io.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC) == Fcntl::FD_CLOEXEC)
328 assert_equal 0, io.fcntl(Fcntl::F_GETFD)
332 assert_nil @ep.delete(@rd)
333 assert_nil @ep.delete(@wr)
334 assert_nothing_raised { @ep.add @rd, Epoll::IN }
335 assert_equal @rd, @ep.delete(@rd)
336 assert_nil @ep.delete(@rd)
340 @ep.add @rd, Epoll::IN
341 assert_equal @rd, @ep.io_for(@rd.fileno)
342 assert_equal @rd, @ep.io_for(@rd)
344 assert_nil @ep.io_for(@rd.fileno)
345 assert_nil @ep.io_for(@rd)
349 @ep.add @rd, Epoll::IN
350 assert_equal Epoll::IN, @ep.flags_for(@rd.fileno)
351 assert_equal Epoll::IN, @ep.flags_for(@rd)
354 assert_nil @ep.flags_for(@rd.fileno)
355 assert_nil @ep.flags_for(@rd)
358 def test_flags_for_sym
360 assert_equal Epoll::IN, @ep.flags_for(@rd.fileno)
361 assert_equal Epoll::IN, @ep.flags_for(@rd)
364 assert_nil @ep.flags_for(@rd.fileno)
365 assert_nil @ep.flags_for(@rd)
368 def test_flags_for_sym_ary
369 @ep.add @rd, [:IN, :ET]
370 expect = Epoll::IN | Epoll::ET
371 assert_equal expect, @ep.flags_for(@rd.fileno)
372 assert_equal expect, @ep.flags_for(@rd)
375 assert_nil @ep.flags_for(@rd.fileno)
376 assert_nil @ep.flags_for(@rd)
380 assert ! @ep.include?(@rd)
381 @ep.add @rd, Epoll::IN
382 assert @ep.include?(@rd)
383 assert @ep.include?(@rd.fileno)
384 assert ! @ep.include?(@wr)
385 assert ! @ep.include?(@wr.fileno)