Kouhei Sutou 2019-03-28 12:22:17 +0900 (Thu, 28 Mar 2019) Revision: 323559c90522df265c123af876f47981fcb2aa73 https://github.com/ranguba/chupa-text/commit/323559c90522df265c123af876f47981fcb2aa73 Message: external-command: add support for soft timeout Modified files: lib/chupa-text/external-command.rb test/helper.rb test/test-external-command.rb Modified: lib/chupa-text/external-command.rb (+20 -3) =================================================================== --- lib/chupa-text/external-command.rb 2019-03-28 11:27:30 +0900 (c2efeb9) +++ lib/chupa-text/external-command.rb 2019-03-28 12:22:17 +0900 (7d50eb0) @@ -23,6 +23,7 @@ module ChupaText include Loggable @default_timeout = nil + @default_soft_timeout = nil @default_limit_cpu = nil @default_limit_as = nil class << self @@ -34,6 +35,14 @@ module ChupaText @default_timeout = timeout end + def default_soft_timeout + @default_soft_timeout + end + + def default_soft_timeout=(timeout) + @default_soft_timeout = timeout + end + def default_limit_cpu @default_limit_cpu || limit_env("CPU") end @@ -75,7 +84,7 @@ module ChupaText spawn_options(options[:spawn_options])) status = nil begin - status = wait_process(pid, options[:timeout]) + status = wait_process(pid, options[:timeout], options[:soft_timeout]) ensure unless status begin @@ -227,9 +236,17 @@ module ChupaText warn("#{log_tag}#{tag}[invalid] <#{value}>(#{type})") end - def wait_process(pid, timeout) + def wait_process(pid, timeout, soft_timeout) tag = "[timeout]" - timeout = parse_time(tag, timeout || self.class.default_timeout) + timeout = parse_time(tag, + timeout || self.class.default_timeout) + soft_timeout = parse_time(tag, + soft_timeout || self.class.default_soft_timeout) + if timeout + timeout = soft_timeout if soft_timeout and soft_timeout < timeout + else + timeout = soft_timeout + end if timeout info("#{log_tag}#{tag}[use] <#{timeout}s>: <#{pid}>") status = wait_process_timeout(pid, timeout) Modified: test/helper.rb (+5 -0) =================================================================== --- test/helper.rb 2019-03-28 11:27:30 +0900 (7fea6f1) +++ test/helper.rb 2019-03-28 12:22:17 +0900 (0267e8b) @@ -15,6 +15,7 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA require "pathname" +require "rbconfig" require "tempfile" require "uri" require "webrick" @@ -40,4 +41,8 @@ module Helper [level, message] end end + + def ruby + RbConfig.ruby + end end Modified: test/test-external-command.rb (+145 -6) =================================================================== --- test/test-external-command.rb 2019-03-28 11:27:30 +0900 (68b2794) +++ test/test-external-command.rb 2019-03-28 12:22:17 +0900 (a07cd40) @@ -1,4 +1,4 @@ -# Copyright (C) 2014 Kouhei Sutou <kou****@clear*****> +# Copyright (C) 2014-2019 Kouhei Sutou <kou****@clear*****> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -14,12 +14,8 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "rbconfig" - class TestExternalCommand < Test::Unit::TestCase - def ruby - RbConfig.ruby - end + include Helper def create_command(command) ChupaText::ExternalCommand.new(command) @@ -76,4 +72,147 @@ class TestExternalCommand < Test::Unit::TestCase assert_false(exist?("nonexistent")) end end + + class TestTimeout < self + def setup + timeout = ChupaText::ExternalCommand.default_timeout + soft_timeout = ChupaText::ExternalCommand.default_soft_timeout + begin + yield + ensure + ChupaText::ExternalCommand.default_timeout = timeout + ChupaText::ExternalCommand.default_soft_timeout = soft_timeout + end + end + + def run_command(options={}) + IO.pipe do |input, output| + command = create_command(ruby) + command.run("-e", "puts(Process.pid)", + options.merge(spawn_options: {out: output})) + input.gets.chomp + end + end + + def test_option + pid = nil + messages = capture_log do + pid = run_command(timeout: "60s") + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <60.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_option_soft_not_use + pid = nil + messages = capture_log do + pid = run_command(timeout: "60s", + soft_timeout: "90s") + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <60.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_option_soft_use + pid = nil + messages = capture_log do + pid = run_command(timeout: "60s", + soft_timeout: "30s") + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <30.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_option_soft_only + pid = nil + messages = capture_log do + pid = run_command(soft_timeout: "30s") + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <30.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_default + ChupaText::ExternalCommand.default_timeout = "60s" + pid = nil + messages = capture_log do + pid = run_command + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <60.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_default_soft_not_use + ChupaText::ExternalCommand.default_timeout = "60s" + ChupaText::ExternalCommand.default_soft_timeout = "90s" + pid = nil + messages = capture_log do + pid = run_command + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <60.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_default_soft_use + ChupaText::ExternalCommand.default_timeout = "60s" + ChupaText::ExternalCommand.default_soft_timeout = "30s" + pid = nil + messages = capture_log do + pid = run_command + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <30.0s>: <#{pid}>", + ] + ], + messages) + end + + def test_default_soft_only + ChupaText::ExternalCommand.default_timeout = nil + ChupaText::ExternalCommand.default_soft_timeout = "30s" + pid = nil + messages = capture_log do + pid = run_command + end + assert_equal([ + [ + :info, + "[external-command][timeout][use] <30.0s>: <#{pid}>", + ] + ], + messages) + end + end end -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190328/d07facc3/attachment-0001.html>