Class: TestRunner::ResourcePool::CapacitySource

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/resource_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_value:, reserve:, detector:, overcommit:, available_detector: nil, monotonic: false) ⇒ CapacitySource

Returns a new instance of CapacitySource.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test-runner/resource_pool.rb', line 38

def initialize(
  max_value:,
  reserve:,
  detector:,
  overcommit:,
  available_detector: nil,
  monotonic: false
)
  @max_value = max_value
  @reserve = reserve
  @detector = detector
  @overcommit = overcommit
  @initial_available = available_detector&.call
  @monotonic = monotonic
  @initialized = false
  @current_value = nil
end

Instance Attribute Details

#current_valueObject (readonly)

Returns the value of attribute current_value.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def current_value
  @current_value
end

#detectedObject (readonly)

Returns the value of attribute detected.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def detected
  @detected
end

#initial_availableObject (readonly)

Returns the value of attribute initial_available.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def initial_available
  @initial_available
end

#max_valueObject (readonly, protected)

Returns the value of attribute max_value.



70
71
72
# File 'lib/test-runner/resource_pool.rb', line 70

def max_value
  @max_value
end

#reserveObject (readonly)

Returns the value of attribute reserve.



36
37
38
# File 'lib/test-runner/resource_pool.rb', line 36

def reserve
  @reserve
end

Instance Method Details

#currentObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test-runner/resource_pool.rb', line 56

def current
  @detected = @detector.call
  value = ResourcePool.capacity(limit(overcommit(usable_detected)), reserve)

  if @monotonic && @initialized
    value = monotonic_value(value)
  end

  @initialized = true
  @current_value = value
end

#limit(detected) ⇒ Object (protected)



93
94
95
96
97
98
99
100
101
# File 'lib/test-runner/resource_pool.rb', line 93

def limit(detected)
  if detected && max_value
    [detected, max_value].min
  elsif detected
    detected
  else
    max_value
  end
end

#monotonic_value(value) ⇒ Object (protected)



80
81
82
83
84
85
# File 'lib/test-runner/resource_pool.rb', line 80

def monotonic_value(value)
  return current_value if value.nil?
  return value if current_value.nil?

  [current_value, value].min
end

#overcommit(detected) ⇒ Object (protected)



87
88
89
90
91
# File 'lib/test-runner/resource_pool.rb', line 87

def overcommit(detected)
  return nil if detected.nil?

  (detected * @overcommit).floor
end

#usable_detectedObject (protected)



72
73
74
75
76
77
78
# File 'lib/test-runner/resource_pool.rb', line 72

def usable_detected
  if detected && initial_available
    [detected, initial_available].min
  else
    detected || initial_available
  end
end