Skip to content

Commit

Permalink
Add a timeout to async examples
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Jan 22, 2014
1 parent 82fd009 commit c7d6e14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,4 +8,6 @@

* Replaced browser formatter to use html printer from rspec

* Add timeout support to asynchronous specs

## 0.2.1 2013-11-24
39 changes: 32 additions & 7 deletions opal/opal/rspec/async.rb
Expand Up @@ -8,14 +8,18 @@ def async(desc, *args, &block)
end

module AsyncHelpers
def run_async(&block)
::RSpec.current_example.continue_async(block)
def async(&block)
@example.continue_async(block)
end

def set_timeout(duration, &block)
`setTimeout(block, duration)`
alias run_async async

def delay(duration, &block)
`setTimeout(block, duration * 1000)`
self
end

alias set_timeout delay
end

class AsyncRunner
Expand Down Expand Up @@ -70,14 +74,18 @@ def self.examples
@examples ||= []
end

include AsyncHelpers

def run(example_group_instance, reporter, &after_run_block)
@example_group_instance = example_group_instance
@reporter = reporter
@after_run_block = after_run_block
@reporter = reporter
@after_run_block = after_run_block
@finished = false

should_wait = true

::RSpec.current_example = self
example_group_instance.instance_variable_set :@example, self

start(reporter)

Expand All @@ -89,10 +97,21 @@ def run(example_group_instance, reporter, &after_run_block)
should_wait = false
end

async_example_finished unless should_wait
if should_wait
delay options[:timeout] || 10 do
next if finished?

set_exception RuntimeError.new("timeout")
async_example_finished
end
else
async_example_finished
end
end

def continue_async(block)
return if finished?

begin
block.call
rescue Exception => e
Expand All @@ -102,7 +121,13 @@ def continue_async(block)
async_example_finished
end

def finished?
@finished
end

def async_example_finished
@finished = true

begin
run_after_each
rescue Exception => e
Expand Down

0 comments on commit c7d6e14

Please sign in to comment.