Class: Dill::Checkpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/dill/checkpoint.rb

Overview

A point in time where some condition, or some set of conditions, should be verified.

Direct Known Subclasses

WidgetCheckpoint

Defined Under Namespace

Classes: ConditionNotMet, TimeFrozen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wait_time = Capybara.default_wait_time) ⇒ Checkpoint

Initializes a new Checkpoint.

Parameters:

  • wait_time (defaults to: Capybara.default_wait_time)

    how long this checkpoint will wait for its conditions to be met, in seconds.



23
24
25
# File 'lib/dill/checkpoint.rb', line 23

def initialize(wait_time = Capybara.default_wait_time)
  @wait_time = wait_time
end

Instance Attribute Details

#wait_timeObject (readonly)

Returns the configured wait time, in seconds.

Returns:

  • the configured wait time, in seconds.



12
13
14
# File 'lib/dill/checkpoint.rb', line 12

def wait_time
  @wait_time
end

Class Method Details

.wait_for(wait_time = Capybara.default_wait_time, &block) ⇒ Object

Shortcut for instance level wait_for.



15
16
17
# File 'lib/dill/checkpoint.rb', line 15

def self.wait_for(wait_time = Capybara.default_wait_time, &block)
  new(wait_time).wait_for(&block)
end

Instance Method Details

#rescuable_errorsObject



54
55
56
# File 'lib/dill/checkpoint.rb', line 54

def rescuable_errors
  StandardError
end

#wait_for(&condition) ⇒ Object

Executes block repeatedly until it returns a “truthy” value or wait_time expires.

Swallows any StandardError or StandardError descendent until wait_time expires. If an exception is raised and the time has expired, that exception will be raised again.

If the block does not return a “truthy” value until wait_time expires, raises a Dill::Checkpoint::ConditionNotMet error.

Returns whatever value is returned by the block.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dill/checkpoint.rb', line 38

def wait_for(&condition)
  start

  begin
    yield or raise ConditionNotMet
  rescue *rescuable_errors
    raise if expired?

    wait

    raise TimeFrozen, 'time appears to be frozen' if time_frozen?

    retry
  end
end