Class: RSpec::Rewind::RetryBudget

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rewind/retry_budget.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit) ⇒ RetryBudget

Returns a new instance of RetryBudget.



22
23
24
25
26
# File 'lib/rspec/rewind/retry_budget.rb', line 22

def initialize(limit)
  @limit = normalize_limit(limit)
  @used = 0
  @mutex = Mutex.new
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



20
21
22
# File 'lib/rspec/rewind/retry_budget.rb', line 20

def limit
  @limit
end

#usedObject (readonly)

Returns the value of attribute used.



20
21
22
# File 'lib/rspec/rewind/retry_budget.rb', line 20

def used
  @used
end

Instance Method Details

#consumeObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/rspec/rewind/retry_budget.rb', line 32

def consume
  return decision(true) if unlimited?

  @mutex.synchronize do
    return decision(false) if @used >= @limit

    @used += 1
    decision(true)
  end
end

#consume!Object



28
29
30
# File 'lib/rspec/rewind/retry_budget.rb', line 28

def consume!
  consume.allowed?
end

#remainingObject



43
44
45
46
47
# File 'lib/rspec/rewind/retry_budget.rb', line 43

def remaining
  return Float::INFINITY if unlimited?

  [@limit - @used, 0].max
end

#reset!Object



53
54
55
# File 'lib/rspec/rewind/retry_budget.rb', line 53

def reset!
  @mutex.synchronize { @used = 0 }
end

#unlimited?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/rspec/rewind/retry_budget.rb', line 49

def unlimited?
  @limit.nil?
end