ref: refs/heads/master commit e83a642e2631e79b6a42dfb39f7f1c77b90a7c13 Author: Ladislav Slezak <lslezak@novell.com> Date: Mon Dec 21 19:12:47 2009 +0100 patch model - accept exceptions from the subprocess --- plugins/patches/app/models/patch.rb | 38 ++++++++++++++++++++++++++---- plugins/patches/test/unit/patch_test.rb | 23 ++++++++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/plugins/patches/app/models/patch.rb b/plugins/patches/app/models/patch.rb index adf9a3b..edb12cf 100644 --- a/plugins/patches/app/models/patch.rb +++ b/plugins/patches/app/models/patch.rb @@ -42,13 +42,13 @@ class Patch < Resolvable def self.subprocess_find(what) # open subprocess - subproc = IO.popen(subprocess_command what) + subproc = open_subprocess what result = nil - while !subproc.eof? do + while !eof_subprocess?(subproc) do begin - line = subproc.readline + line = read_subprocess subproc unless line.blank? received = Hash.from_xml(line) @@ -103,7 +103,14 @@ class Patch < Resolvable proc_id = id(what) if bm.process_finished? proc_id Rails.logger.debug "Request #{proc_id} is done" - return bm.get_value proc_id + ret = bm.get_value proc_id + + # check for exception + if ret.is_a? StandardError + raise ret + end + + ret end running = bm.get_progress proc_id @@ -119,7 +126,13 @@ class Patch < Resolvable # run the patch query in a separate thread Thread.new do res = subprocess_find what - Rails.logger.info "*** Patches thread: Found #{res.size} applicable patches" + + # check for exception + unless res.is_a? StandardError + Rails.logger.info "*** Patches thread: Found #{res.size} applicable patches" + else + Rails.logger.debug "*** Exception raised: #{res.inspect}" + end bm.finish_process(proc_id, res) end @@ -174,4 +187,19 @@ class Patch < Resolvable ret += " #{what}" if what != :available ret end + + # IO functions moved to separate methods for easy mocking/testing + + def self.open_subprocess(what) + IO.popen(subprocess_command what) + end + + def self.read_subprocess(subproc) + subproc.readline + end + + def self.eof_subprocess?(subproc) + subproc.eof? + end + end diff --git a/plugins/patches/test/unit/patch_test.rb b/plugins/patches/test/unit/patch_test.rb index b603dd1..6a0173f 100644 --- a/plugins/patches/test/unit/patch_test.rb +++ b/plugins/patches/test/unit/patch_test.rb @@ -48,8 +48,29 @@ class PatchTest < ActiveSupport::TestCase assert_equal 2, patches.size patch = patches.first assert_equal "847", patch.resolvable_id + end - + SCRIPT_OUTPUT_ERROR = [ + '<?xml version="1.0" encoding="UTF-8"?><background_status><status>running</status><progress type="integer">0</progress><subprogress type="integer">-1</subprogress></background_status>', + '<?xml version="1.0" encoding="UTF-8"?><background_status><status>setup</status><progress type="integer">0</progress><subprogress type="integer">-1</subprogress></background_status>', + '<?xml version="1.0" encoding="UTF-8"?><background_status><status>query</status><progress type="integer">0</progress><subprogress type="integer">-1</subprogress></background_status>', + '<?xml version="1.0" encoding="UTF-8"?><background_status><status>refresh-cache</status><progress type="integer">0</progress><subprogress type="integer">-1</subprogress></background_status>', + '<?xml version="1.0" encoding="UTF-8"?><background_status><status>refresh-cache</status><progress type="integer">9</progress><subprogress type="integer">-1</subprogress></background_status>', + '<?xml version="1.0" encoding="UTF-8"?><error><type>PACKAGEKIT_ERROR</type><description>gpg-failure: Signature verification for Repository Factory_(Non-OSS) failed</description></error>' + ] + + def test_available_patches_background_mode + Patch.stubs(:open_subprocess).returns(nil) + Patch.stubs(:read_subprocess).returns(*SCRIPT_OUTPUT_ERROR) + + # return EOF when all lines are read + Patch.stubs(:eof_subprocess?).returns(*(Array.new(SCRIPT_OUTPUT_ERROR.size, false) << true)) + + # note: Patch.find(:available, {:background => true}) + # cannot be used here, Threading support in test mode doesn't work :-( + patches = Patch.subprocess_find(:available) + + assert_equal PackageKitError, patches.class end end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org