FirePoll – Simple Polling in Ruby

FirePoll is a gem for simple polling in Ruby. Ever wanted an easy way of knowing when your test can continue executing? Here’s an example. Assume uploading a file takes a little while.

def wait_for_file(filename)
  FirePoll.poll do
    FileStore.file_ready?(filename)
  end
end

def test_files_are_saved
  upload_file "blue.txt"
  assert_nothing_raised { wait_for_file "blue.txt" }
  assert_equal "blue is the best color", read_saved_file("blue.txt")
end

FirePoll.poll continues to execute your block until it sees a true result. When your block returns true, execution continues. If your block does not return true within the expected timeframe, then an exception is raised—the condition wasn’t met in the expected timeframe.

Please see the README for more information about using FirePoll.

Get FirePoll: Gem | Source on GitHub

 
Conversation
  • Is’t that just a while loop?

  • Micah Alles says:

    @Danny: not quite; This is just a while loop: https://gist.github.com/842272 and this is firepoll: https://gist.github.com/842276. While simple, we still felt it worthwhile to release as a gem as it’s something we’ve copied countless times between projects over the years to ease pain in testing asynchronous logic. Given the advent of tools like Bundler, it’s faster to add a singe gem dependency than go dig up or re-implement even simple tools such as this.

  • OK fair point

  • Comments are closed.