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

test_gdbm.rb: open_db_child

* test/gdbm/test_gdbm.rb (TestGDBM#open_db_child): open the db in a
  child process and handshake using popen.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41424 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2013-06-19 07:47:33 +00:00
parent 56fae61460
commit 7b253adc68

View file

@ -1,14 +1,13 @@
require 'test/unit'
require 'tmpdir'
begin begin
require 'gdbm' require 'gdbm'
rescue LoadError rescue LoadError
end end
if defined? GDBM if defined? GDBM
require 'test/unit'
require 'tmpdir' require 'tmpdir'
require 'fileutils' require 'fileutils'
require_relative '../ruby/envutil'
class TestGDBM_RDONLY < Test::Unit::TestCase class TestGDBM_RDONLY < Test::Unit::TestCase
def TestGDBM_RDONLY.uname_s def TestGDBM_RDONLY.uname_s
@ -86,15 +85,6 @@ if defined? GDBM
end end
end end
def have_fork?
begin
Process.wait(fork{})
true
rescue NotImplementedError
false
end
end
def test_s_new_has_no_block def test_s_new_has_no_block
# GDBM.new ignore the block # GDBM.new ignore the block
foo = true foo = true
@ -144,23 +134,31 @@ if defined? GDBM
def test_s_open_with_block def test_s_open_with_block
assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo) assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
end end
def test_s_open_lock
return unless have_fork? # snip this test def open_db_child(dbname, *opts)
pid = fork() { opts = [0644, *opts].map(&:inspect).join(', ')
assert_instance_of(GDBM, GDBM.open("#{@tmpdir}/#{@prefix}", 0644)) args = [EnvUtil.rubybin, "-rgdbm", "-e", <<-SRC, dbname]
sleep 2 STDOUT.sync = true
} gdbm = GDBM.open(ARGV.shift, #{opts})
begin puts gdbm.class
sleep 1 gets
assert_raise(Errno::EWOULDBLOCK) { SRC
begin IO.popen(args, "r+") do |f|
assert_instance_of(GDBM, GDBM.open("#{@tmpdir}/#{@prefix}", 0644)) dbclass = f.gets
rescue Errno::EAGAIN, Errno::EACCES assert_equal("GDBM", dbclass.chomp)
raise Errno::EWOULDBLOCK yield
end end
end
def test_s_open_lock
dbname = "#{@tmpdir}/#{@prefix}"
open_db_child(dbname) do
assert_raise(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
GDBM.open(dbname, 0644) {|gdbm|
assert_instance_of(GDBM, gdbm)
}
} }
ensure
Process.wait pid
end end
end end
@ -180,47 +178,27 @@ if defined? GDBM
=end =end
def test_s_open_nolock def test_s_open_nolock
# gdbm 1.8.0 specific dbname = "#{@tmpdir}/#{@prefix}"
if not defined? GDBM::NOLOCK
return
end
return unless have_fork? # snip this test
pid = fork() { open_db_child(dbname, GDBM::NOLOCK) do
assert_instance_of(GDBM, GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::NOLOCK))
sleep 2
}
sleep 1
begin
gdbm2 = nil
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644)) GDBM.open(dbname, 0644) {|gdbm2|
assert_instance_of(GDBM, gdbm2)
}
} }
ensure
Process.wait pid
gdbm2.close if gdbm2
end end
STDERR.puts Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG STDERR.puts Dir.glob("#{dbname}*") if $DEBUG
pid = fork() { open_db_child(dbname) do
assert_instance_of(GDBM, GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
sleep 2
}
begin
sleep 1
gdbm2 = nil
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
# this test is failed on Cygwin98 (???) # this test is failed on Cygwin98 (???)
assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644, GDBM.open(dbname, 0644, GDBM::NOLOCK) {|gdbm2|
GDBM::NOLOCK)) assert_instance_of(GDBM, gdbm2)
}
} }
ensure
Process.wait pid
gdbm2.close if gdbm2
end
end end
end if defined? GDBM::NOLOCK # gdbm 1.8.0 specific
def test_s_open_error def test_s_open_error
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0))