Sha256: cad935f1226f347408182c0c03a4ba01b431df715047d3f9fb1b2176db79cc84

Contents?: true

Size: 1.37 KB

Versions: 25

Compression:

Stored size: 1.37 KB

Contents

module WaitWatchers
  class TimeoutError < StandardError; end

  # method to wait until block passed returns truthy (false will not work) or
  # timeout (default is 2 seconds) is reached raises TiemoutError on timeout
  #
  # @returns the truthy value
  #
  # @example 
  #     
  #   @a = nil
  #
  #   th = Thread.new do
  #     sleep(1)
  #     @a = :fudge
  #   end
  #
  #   wait_until(2) { @a }.should == :fudge
  #
  def wait_until(timeout=2)
    if ZK.travis? and timeout and timeout < 5
      logger.debug { "TRAVIS: adjusting wait_until timeout from #{timeout} to 5 sec" }
      timeout = 5
    end

    time_to_stop = Time.now + timeout
    while true
      rval = yield
      return rval if rval
      raise TimeoutError, "timeout of #{timeout}s exceeded" if Time.now > time_to_stop
      Thread.pass
    end
  end

  # inverse of wait_until
  def wait_while(timeout=2)
    if ZK.travis? and timeout and timeout < 5
      logger.debug { "TRAVIS: adjusting wait_while timeout from #{timeout} to 5 sec" }
      timeout = 5
    end

    time_to_stop = Time.now + timeout
    while true
      rval = yield
      return rval unless rval
      raise TimeoutError, "timeout of #{timeout}s exceeded" if Time.now > time_to_stop
      Thread.pass
    end
  end

  def report_realtime(what)
    return yield
    t = Benchmark.realtime { yield }
    $stderr.puts "#{what}: %0.3f" % [t.to_f]
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
zk-1.10.0 spec/support/wait_watchers.rb
zk-1.9.6 spec/support/wait_watchers.rb
zk-1.9.5 spec/support/wait_watchers.rb
zk-1.9.4 spec/support/wait_watchers.rb
zk-1.9.3 spec/support/wait_watchers.rb
zk-1.9.2 spec/support/wait_watchers.rb
zk-1.9.1 spec/support/wait_watchers.rb
zk-1.9.0 spec/support/wait_watchers.rb
zk-1.8.0 spec/support/wait_watchers.rb
zk-1.7.5 spec/support/wait_watchers.rb
zk-1.7.4 spec/support/wait_watchers.rb
zk-1.7.3 spec/support/wait_watchers.rb
zk-1.7.2 spec/support/wait_watchers.rb
zk-1.7.1 spec/support/wait_watchers.rb
zk-1.7.0 spec/support/wait_watchers.rb
zk-1.6.5 spec/support/wait_watchers.rb
zk-1.6.4 spec/support/wait_watchers.rb
zk-1.6.3 spec/support/wait_watchers.rb
zk-1.6.2 spec/support/wait_watchers.rb
zk-1.6.1 spec/support/wait_watchers.rb