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

ruby-changes:67037

From: aycabta <ko1@a...>
Date: Wed, 4 Aug 2021 18:30:07 +0900 (JST)
Subject: [ruby-changes:67037] 5ca0a51ffd (master): Add a test for handling SIGINT in other thread

https://git.ruby-lang.org/ruby.git/commit/?id=5ca0a51ffd

From 5ca0a51ffd218131f5d396f0250781cdf972779f Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Mon, 2 Aug 2021 23:43:05 +0900
Subject: Add a test for handling SIGINT in other thread

---
 test/readline/test_readline.rb | 76 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)

diff --git a/test/readline/test_readline.rb b/test/readline/test_readline.rb
index 59408c2..297bc30 100644
--- a/test/readline/test_readline.rb
+++ b/test/readline/test_readline.rb
@@ -475,6 +475,82 @@ module BasetestReadline https://github.com/ruby/ruby/blob/trunk/test/readline/test_readline.rb#L475
     end
   end
 
+  def test_interrupt_in_other_thread
+    omit unless respond_to?(:assert_ruby_status)
+    code = <<-"end;"
+      require 'readline'
+      require 'test/readline/helper'
+      #{
+        if self.class == TestReadline
+          "use_ext_readline"
+        elsif self.class == TestRelineAsReadline
+          "use_lib_reline"
+        end
+      }
+      Readline.input = STDIN
+      begin
+        Thread.new{
+          trap(:INT) {
+            p :INT
+          }
+          Readline.readline('input> ')
+        }.value
+      rescue Interrupt => e
+        puts 'FAILED'
+        raise
+      end
+      puts 'SUCCEEDED'
+    end;
+    f = Tempfile.new("interrupt_in_other_thread")
+    path = f.path
+    f.write code
+    f.close
+    f.open
+    asserted = false
+    log, status = EnvUtil.invoke_ruby([path], "", true, :merge_to_stdout) do |_in, _out, _, pid|
+      Timeout.timeout(4) do
+        log = String.new
+        while c = _out.read(1)
+          log << c if c
+          break if log.include?('input>')
+        end
+        Process.kill(:INT, pid)
+        sleep 0.1
+        while c = _out.read(1)
+          log << c if c
+          break if log.include?('INT')
+        end
+        begin
+          _in.write "\n"
+        rescue Errno::EPIPE
+          # The "write" will fail if Reline crashed by SIGINT.
+        end
+        while c = _out.read(1)
+          log << c if c
+          if log.include?('FAILED')
+            assert false, "Should handle SIGINT correctly but failed."
+            asserted = true
+          end
+          if log.include?('SUCCEEDED')
+            assert true, "Should handle SIGINT correctly but failed."
+            asserted = true
+          end
+        end
+      rescue Timeout::Error
+        assert true, "Timed out to handle SIGINT."
+        asserted = true
+      end
+      [log, Process.wait2(pid)[1]]
+    end
+    unless asserted
+      assert false, "Unknown failure with exit status #{status}"
+    end
+  ensure
+    f.close! if f
+  end
+
+
+
   def test_setting_quoting_detection_proc
     return unless Readline.respond_to?(:quoting_detection_proc=)
 
-- 
cgit v1.1


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

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