[前][次][番号順一覧][スレッド一覧]

ruby-changes:29373

From: nobu <ko1@a...>
Date: Wed, 19 Jun 2013 16:48:23 +0900 (JST)
Subject: [ruby-changes:29373] nobu:r41424 (trunk): test_gdbm.rb: open_db_child

nobu	2013-06-19 16:47:33 +0900 (Wed, 19 Jun 2013)

  New Revision: 41424

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=41424

  Log:
    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.

  Modified files:
    trunk/test/gdbm/test_gdbm.rb

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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]