1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* test/net/imap/test_imap.rb: Don't ignore exceptions on server

threads.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48366 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2014-11-10 15:22:29 +00:00
parent 9bf5457481
commit 976574413a
2 changed files with 181 additions and 223 deletions

View file

@ -1,3 +1,8 @@
Tue Nov 11 00:21:50 2014 Tanaka Akira <akr@fsij.org>
* test/net/imap/test_imap.rb: Don't ignore exceptions on server
threads.
Mon Nov 10 23:34:13 2014 Tanaka Akira <akr@fsij.org> Mon Nov 10 23:34:13 2014 Tanaka Akira <akr@fsij.org>
* test/net/ftp/test_ftp.rb (create_ftp_server): Don't ignore * test/net/ftp/test_ftp.rb (create_ftp_server): Don't ignore

View file

@ -11,9 +11,14 @@ class IMAPTest < Test::Unit::TestCase
def setup def setup
@do_not_reverse_lookup = Socket.do_not_reverse_lookup @do_not_reverse_lookup = Socket.do_not_reverse_lookup
Socket.do_not_reverse_lookup = true Socket.do_not_reverse_lookup = true
@threads = []
end end
def teardown def teardown
if !@threads.empty?
assert_join_threads(@threads)
end
ensure
Socket.do_not_reverse_lookup = @do_not_reverse_lookup Socket.do_not_reverse_lookup = @do_not_reverse_lookup
end end
@ -127,32 +132,25 @@ class IMAPTest < Test::Unit::TestCase
def test_unexpected_eof def test_unexpected_eof
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin sock.gets
sock.print("* OK test server\r\n") # sock.print("* BYE terminating connection\r\n")
sock.gets # sock.print("RUBY0001 OK LOGOUT completed\r\n")
# sock.print("* BYE terminating connection\r\n") ensure
# sock.print("RUBY0001 OK LOGOUT completed\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = Net::IMAP.new(SERVER_ADDR, :port => port)
imap = Net::IMAP.new(SERVER_ADDR, :port => port) assert_raise(EOFError) do
assert_raise(EOFError) do imap.logout
imap.logout
end
ensure
imap.disconnect if imap
end end
ensure ensure
server.close imap.disconnect if imap
ths.join
end end
end end
@ -160,52 +158,46 @@ class IMAPTest < Test::Unit::TestCase
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
requests = [] requests = []
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin requests.push(sock.gets)
sock.print("* OK test server\r\n") sock.print("+ idling\r\n")
requests.push(sock.gets) sock.print("* 3 EXISTS\r\n")
sock.print("+ idling\r\n") sock.print("* 2 EXPUNGE\r\n")
sock.print("* 3 EXISTS\r\n") requests.push(sock.gets)
sock.print("* 2 EXPUNGE\r\n") sock.print("RUBY0001 OK IDLE terminated\r\n")
requests.push(sock.gets) sock.gets
sock.print("RUBY0001 OK IDLE terminated\r\n") sock.print("* BYE terminating connection\r\n")
sock.gets sock.print("RUBY0002 OK LOGOUT completed\r\n")
sock.print("* BYE terminating connection\r\n") ensure
sock.print("RUBY0002 OK LOGOUT completed\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = Net::IMAP.new(SERVER_ADDR, :port => port)
imap = Net::IMAP.new(SERVER_ADDR, :port => port) responses = []
responses = [] imap.idle do |res|
imap.idle do |res| responses.push(res)
responses.push(res) if res.name == "EXPUNGE"
if res.name == "EXPUNGE" imap.idle_done
imap.idle_done
end
end 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
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 ensure
server.close imap.disconnect if imap
ths.join
end end
end end
@ -213,163 +205,134 @@ class IMAPTest < Test::Unit::TestCase
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
requests = [] requests = []
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin requests.push(sock.gets)
sock.print("* OK test server\r\n") sock.print("+ idling\r\n")
requests.push(sock.gets) sock.print("* 3 EXISTS\r\n")
sock.print("+ idling\r\n") sock.print("* 2 EXPUNGE\r\n")
sock.print("* 3 EXISTS\r\n") requests.push(sock.gets)
sock.print("* 2 EXPUNGE\r\n") sock.print("RUBY0001 OK IDLE terminated\r\n")
requests.push(sock.gets) sock.gets
sock.print("RUBY0001 OK IDLE terminated\r\n") sock.print("* BYE terminating connection\r\n")
sock.gets sock.print("RUBY0002 OK LOGOUT completed\r\n")
sock.print("* BYE terminating connection\r\n") ensure
sock.print("RUBY0002 OK LOGOUT completed\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
imap = Net::IMAP.new(SERVER_ADDR, :port => port)
begin begin
imap = Net::IMAP.new(SERVER_ADDR, :port => port) th = Thread.current
begin m = Monitor.new
th = Thread.current in_idle = false
m = Monitor.new exception_raised = false
in_idle = false c = m.new_cond
exception_raised = false @threads << Thread.start do
c = m.new_cond m.synchronize do
thw = Thread.start do until in_idle
m.synchronize do c.wait(0.1)
until in_idle
c.wait(0.1)
end
end
th.raise(Interrupt)
exception_raised = true
end
imap.idle do |res|
m.synchronize do
in_idle = true
c.signal
until exception_raised
c.wait(0.1)
end
end end
end end
rescue Interrupt th.raise(Interrupt)
exception_raised = true
end end
assert_equal(2, requests.length) imap.idle do |res|
assert_equal("RUBY0001 IDLE\r\n", requests[0]) m.synchronize do
assert_equal("DONE\r\n", requests[1]) in_idle = true
imap.logout c.signal
ensure until exception_raised
imap.disconnect if imap c.wait(0.1)
thw.join end
end
end
rescue Interrupt
end end
assert_equal(2, requests.length)
assert_equal("RUBY0001 IDLE\r\n", requests[0])
assert_equal("DONE\r\n", requests[1])
imap.logout
ensure ensure
server.close imap.disconnect if imap
ths.join
end end
end end
def test_idle_done_not_during_idle def test_idle_done_not_during_idle
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin ensure
sock.print("* OK test server\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = Net::IMAP.new(SERVER_ADDR, :port => port)
imap = Net::IMAP.new(SERVER_ADDR, :port => port) assert_raise(Net::IMAP::Error) do
assert_raise(Net::IMAP::Error) do imap.idle_done
imap.idle_done
end
ensure
imap.disconnect if imap
end end
ensure ensure
server.close imap.disconnect if imap
ths.join
end end
end end
def test_unexpected_bye def test_unexpected_bye
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK Gimap ready for requests from 75.101.246.151 33if2752585qyk.26\r\n")
begin sock.gets
sock.print("* OK Gimap ready for requests from 75.101.246.151 33if2752585qyk.26\r\n") sock.print("* BYE System Error 33if2752585qyk.26\r\n")
sock.gets ensure
sock.print("* BYE System Error 33if2752585qyk.26\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = Net::IMAP.new(SERVER_ADDR, :port => port)
imap = Net::IMAP.new(SERVER_ADDR, :port => port) assert_raise(Net::IMAP::ByeResponseError) do
assert_raise(Net::IMAP::ByeResponseError) do imap.login("user", "password")
imap.login("user", "password")
end
end end
ensure
server.close
ths.join
end end
end end
def test_exception_during_shutdown def test_exception_during_shutdown
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin sock.gets
sock.print("* OK test server\r\n") sock.print("* BYE terminating connection\r\n")
sock.gets sock.print("RUBY0001 OK LOGOUT completed\r\n")
sock.print("* BYE terminating connection\r\n") ensure
sock.print("RUBY0001 OK LOGOUT completed\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = Net::IMAP.new(SERVER_ADDR, :port => port)
imap = Net::IMAP.new(SERVER_ADDR, :port => port) imap.instance_eval do
imap.instance_eval do def @sock.shutdown(*args)
def @sock.shutdown(*args) super
super ensure
ensure raise "error"
raise "error"
end
end
imap.logout
ensure
assert_raise(RuntimeError) do
imap.disconnect
end end
end end
imap.logout
ensure ensure
server.close assert_raise(RuntimeError) do
ths.join imap.disconnect
end
end end
end end
@ -378,23 +341,26 @@ class IMAPTest < Test::Unit::TestCase
port = server.addr[1] port = server.addr[1]
requests = [] requests = []
sock = nil sock = nil
ths = Thread.start do threads = []
threads << Thread.start do
begin begin
sock = server.accept sock = server.accept
sock.print("* OK test server\r\n") sock.print("* OK test server\r\n")
requests.push(sock.gets) requests.push(sock.gets)
sock.print("+ idling\r\n") sock.print("+ idling\r\n")
rescue rescue IOError # sock is closed by another thread
ensure
server.close
end end
end end
begin threads << Thread.start do
imap = Net::IMAP.new(SERVER_ADDR, :port => port) imap = Net::IMAP.new(SERVER_ADDR, :port => port)
begin begin
m = Monitor.new m = Monitor.new
in_idle = false in_idle = false
exception_raised = false exception_raised = false
c = m.new_cond c = m.new_cond
thw = Thread.start do threads << Thread.start do
m.synchronize do m.synchronize do
until in_idle until in_idle
c.wait(0.1) c.wait(0.1)
@ -418,34 +384,28 @@ class IMAPTest < Test::Unit::TestCase
assert_equal("RUBY0001 IDLE\r\n", requests[0]) assert_equal("RUBY0001 IDLE\r\n", requests[0])
ensure ensure
imap.disconnect if imap imap.disconnect if imap
thw.join
end end
ensure end
server.close assert_join_threads(threads)
if sock && !sock.closed? ensure
sock.close if sock && !sock.closed?
end sock.close
ths.join
end end
end end
def test_connection_closed_without_greeting def test_connection_closed_without_greeting
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
begin begin
sock = server.accept sock = server.accept
sock.close sock.close
rescue ensure
server.close
end end
end end
begin assert_raise(Net::IMAP::Error) do
assert_raise(Net::IMAP::Error) do Net::IMAP.new(SERVER_ADDR, :port => port)
Net::IMAP.new(SERVER_ADDR, :port => port)
end
ensure
server.close
ths.join
end end
end end
@ -482,7 +442,7 @@ class IMAPTest < Test::Unit::TestCase
ensure ensure
sock.close sock.close
end end
rescue rescue Errno::EPIPE
end end
end end
begin begin
@ -501,43 +461,36 @@ class IMAPTest < Test::Unit::TestCase
def starttls_test def starttls_test
server = create_tcp_server server = create_tcp_server
port = server.addr[1] port = server.addr[1]
ths = Thread.start do @threads << Thread.start do
sock = server.accept
begin begin
sock = server.accept sock.print("* OK test server\r\n")
begin sock.gets
sock.print("* OK test server\r\n") sock.print("RUBY0001 OK completed\r\n")
sock.gets ctx = OpenSSL::SSL::SSLContext.new
sock.print("RUBY0001 OK completed\r\n") ctx.ca_file = CA_FILE
ctx = OpenSSL::SSL::SSLContext.new ctx.key = File.open(SERVER_KEY) { |f|
ctx.ca_file = CA_FILE OpenSSL::PKey::RSA.new(f)
ctx.key = File.open(SERVER_KEY) { |f| }
OpenSSL::PKey::RSA.new(f) ctx.cert = File.open(SERVER_CERT) { |f|
} OpenSSL::X509::Certificate.new(f)
ctx.cert = File.open(SERVER_CERT) { |f| }
OpenSSL::X509::Certificate.new(f) sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
} sock.sync_close = true
sock = OpenSSL::SSL::SSLSocket.new(sock, ctx) sock.accept
sock.sync_close = true sock.gets
sock.accept sock.print("* BYE terminating connection\r\n")
sock.gets sock.print("RUBY0002 OK LOGOUT completed\r\n")
sock.print("* BYE terminating connection\r\n") ensure
sock.print("RUBY0002 OK LOGOUT completed\r\n") sock.close
ensure server.close
sock.close
end
rescue
end end
end end
begin begin
begin imap = yield(port)
imap = yield(port) imap.logout if !imap.disconnected?
imap.logout if !imap.disconnected?
ensure
imap.disconnect if imap && !imap.disconnected?
end
ensure ensure
server.close imap.disconnect if imap && !imap.disconnected?
ths.join
end end
end end