From 078254ae6d287a33333ee5c8b8c1b8833fa91ada Mon Sep 17 00:00:00 2001 From: Thibault Jouan Date: Thu, 16 Apr 2015 18:02:13 +0000 Subject: [PATCH] Simplify and refactor uhwm_wait_output helper --- features/support/env.rb | 2 +- lib/uh/wm/testing/acceptance_helpers.rb | 48 ++++++++++++++++--------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/features/support/env.rb b/features/support/env.rb index 20fd06f..3a7fcfc 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -29,5 +29,5 @@ Around '@other_wm_running' do |_, block| end if ENV.key? 'TRAVIS' - ENV['UHWMTEST_OUTPUT_TIMEOUT'] = 8.to_s + ENV['UHWMTEST_TIMEOUT'] = 8.to_s end diff --git a/lib/uh/wm/testing/acceptance_helpers.rb b/lib/uh/wm/testing/acceptance_helpers.rb index 9b38ed7..bf5d05b 100644 --- a/lib/uh/wm/testing/acceptance_helpers.rb +++ b/lib/uh/wm/testing/acceptance_helpers.rb @@ -23,25 +23,17 @@ module Uh end def uhwm_wait_output message - timeout = ENV.key?('UHWMTEST_OUTPUT_TIMEOUT') ? - ENV['UHWMTEST_OUTPUT_TIMEOUT'].to_i : - 1 - Timeout.timeout(timeout) do - loop do - break if case message - when Regexp then @process.stdout + @process.stderr =~ message - when String then assert_partial_output_interactive message - end - sleep 0.1 + output = -> { @process.stdout + @process.stderr } + timeout_until do + case message + when Regexp then output.call =~ message + when String then assert_partial_output_interactive message end end - rescue Timeout::Error - output = (@process.stdout + @process.stderr).lines - .map { |e| " #{e}" } - .join + rescue TimeoutError => e fail [ - "expected `#{message}' not seen after #{timeout} seconds in:", - " ```\n#{output} ```" + "expected `#{message}' not seen after #{e.timeout} seconds in:", + " ```\n#{output.call.lines.map { |e| " #{e}" }.join} ```" ].join "\n" end @@ -93,6 +85,30 @@ module Uh private + def timeout_until + timeout = ENV.key?('UHWMTEST_TIMEOUT') ? + ENV['UHWMTEST_TIMEOUT'].to_i : + 1 + Timeout.timeout(timeout) do + loop do + break if yield + sleep 0.1 + end + end + rescue Timeout::Error + fail TimeoutError.new('execution expired', timeout) + end + + + class TimeoutError < ::StandardError + attr_reader :timeout + + def initialize message, timeout + super message + @timeout = timeout + end + end + class XClient attr_reader :name