ruby-changes:40135
From: shugo <ko1@a...>
Date: Thu, 22 Oct 2015 18:32:30 +0900 (JST)
Subject: [ruby-changes:40135] shugo:r52216 (trunk): * lib/net/imap (idle): add a new argument timeout for keep-alive.
shugo 2015-10-22 18:32:19 +0900 (Thu, 22 Oct 2015) New Revision: 52216 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52216 Log: * lib/net/imap (idle): add a new argument timeout for keep-alive. [ruby-core:63693] [Bug #10031] Modified files: trunk/ChangeLog trunk/lib/net/imap.rb trunk/test/net/imap/test_imap.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 52215) +++ ChangeLog (revision 52216) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Oct 22 18:25:10 2015 Shugo Maeda <shugo@r...> + + * lib/net/imap (idle): add a new argument timeout for keep-alive. + [ruby-core:63693] [Bug #10031] + Thu Oct 22 15:30:08 2015 Nobuyoshi Nakada <nobu@r...> * compile.c (iseq_peephole_optimize): peephole optimization for Index: lib/net/imap.rb =================================================================== --- lib/net/imap.rb (revision 52215) +++ lib/net/imap.rb (revision 52216) @@ -935,7 +935,17 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L935 # messages. Yields responses from the server during the IDLE. # # Use #idle_done() to leave IDLE. - def idle(&response_handler) + # + # If +timeout+ is given, this method returns after +timeout+ seconds passed. + # +timeout+ can be used for keep-alive. For example, the following code + # checks the connection for each 60 seconds. + # + # loop do + # imap.idle(60) do |res| + # ... + # end + # end + def idle(timeout = nil, &response_handler) raise LocalJumpError, "no block given" unless response_handler response = nil @@ -947,7 +957,7 @@ module Net https://github.com/ruby/ruby/blob/trunk/lib/net/imap.rb#L957 begin add_response_handler(response_handler) @idle_done_cond = new_cond - @idle_done_cond.wait + @idle_done_cond.wait(timeout) @idle_done_cond = nil if @receiver_thread_terminating raise Net::IMAP::Error, "connection closed" Index: test/net/imap/test_imap.rb =================================================================== --- test/net/imap/test_imap.rb (revision 52215) +++ test/net/imap/test_imap.rb (revision 52216) @@ -285,6 +285,50 @@ class IMAPTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/imap/test_imap.rb#L285 end end + def test_idle_timeout + server = create_tcp_server + port = server.addr[1] + requests = [] + @threads << Thread.start do + sock = server.accept + begin + sock.print("* OK test server\r\n") + requests.push(sock.gets) + sock.print("+ idling\r\n") + sock.print("* 3 EXISTS\r\n") + sock.print("* 2 EXPUNGE\r\n") + requests.push(sock.gets) + sock.print("RUBY0001 OK IDLE terminated\r\n") + sock.gets + sock.print("* BYE terminating connection\r\n") + sock.print("RUBY0002 OK LOGOUT completed\r\n") + ensure + sock.close + server.close + end + end + + begin + imap = Net::IMAP.new(SERVER_ADDR, :port => port) + responses = [] + imap.idle(0.1) do |res| + responses.push(res) + end + assert_equal(3, responses.length) + assert_instance_of(Net::IMAP::ContinuationRequest, responses[0]) + assert_equal("EXISTS", responses[1].name) + assert_equal(3, responses[1].data) + assert_equal("EXPUNGE", responses[2].name) + assert_equal(2, responses[2].data) + assert_equal(2, requests.length) + assert_equal("RUBY0001 IDLE\r\n", requests[0]) + assert_equal("DONE\r\n", requests[1]) + imap.logout + ensure + imap.disconnect if imap + end + end + def test_unexpected_bye server = create_tcp_server port = server.addr[1] -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/