Giter Site home page Giter Site logo

waitutil's Introduction

waitutil

Build Status

waitutil provides tools for waiting for various conditions to occur, with a configurable delay time, timeout, and logging.

GitHub: https://github.com/rubytools/waitutil

RubyGems: http://rubygems.org/gems/waitutil

Documentation: http://rubytools.github.io/waitutil/

Examples

Wait methods take a block that returns true or false.

Waiting for conditions

wait_for_condition waits for a condition computed by the given function. It takes two optional parameters, :timeout_sec and :delay_sec, that control how long the function waits before raising a timeout exception, and how frequently it checks for the condition. The time the condition block takes to evaluate is subtracted from the sleep time between successive checks. The timeout is one minute by default, and the delay time is one second.

WaitUtil.wait_for_condition("my_event to happen") do
  check_if_my_event_happened
end
Customized wait time and delay time
WaitUtil.wait_for_condition("my_event to happen", :timeout_sec => 30, :delay_sec => 0.5) do
  check_if_my_event_happened
end
Verbose logging
WaitUtil.wait_for_condition('my event', :verbose => true) { sleep(1) }

Output:

I, [2014-02-16T00:34:31.511915 #15897]  INFO -- : Waiting for my event for up to 60 seconds
I, [2014-02-16T00:34:32.512223 #15897]  INFO -- : Success waiting for my event (1.000153273 seconds)
Returning additional information for the timeout log message
attempt = 1
WaitUtil.wait_for_condition('my event', :verbose => true, :timeout_sec => 3, :delay_sec => 1) do
  sleep(1)
  attempt += 1
  [false, "attempt #{attempt}"]  # the second element goes into the log message
end

Output:

I, [2014-02-16T00:46:53.647936 #17252]  INFO -- : Waiting for my event for up to 3 seconds
WaitUtil::TimeoutError: Timed out waiting for my event (3 seconds elapsed): attempt 3
        from /home/mbautin/.rvm/gems/ruby-2.1.0/gems/waitutil-0.1.0/lib/waitutil.rb:39:in `wait_for_condition'
        from (irb):9
        from /home/mbautin/.rvm/rubies/ruby-2.1.0/bin/irb:11:in `<main>'

Waiting for service availability

Wait for a TCP server to be available using wait_for_service:

WaitUtil.wait_for_service('my service', 'example.com', 80)

License

Apache License, Version 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.