Class: Megatest::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/megatest/config.rb

Constant Summary collapse

NOT_SERIALIZED =
%i(@job_teardown_callbacks @job_setup_callbacks @global_setup_callbacks).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Config

Returns a new instance of Config.



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/megatest/config.rb', line 145

def initialize(env)
  @load_paths = ["test"] # For easier transition from other frameworks
  @retry_tolerance = 0.0
  @max_retries = 0
  @deprecations = true
  @full_backtrace = false
  @queue_url = nil
  @ci = false
  @build_id = nil
  @worker_id = nil
  @workers_count = 1
  @jobs_count = 1
  @colors = nil # auto
  @before_fork_callbacks = []
  @global_setup_callbacks = []
  @job_setup_callbacks = []
  @job_teardown_callbacks = []
  @heartbeat_frequency = 5
  @backtrace = Backtrace.new
  @program_name = nil
  @circuit_breaker = CircuitBreaker.new(Float::INFINITY)
  @seed = Random.rand(0xFFFF)
  @differ = Differ.new(self)
  @pretty_printer = PrettyPrint.new(self)
  @minitest_compatibility = false
  @selectors = nil
  CIService.configure(self, env)
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



141
142
143
# File 'lib/megatest/config.rb', line 141

def backtrace
  @backtrace
end

#before_fork_callbacksObject (readonly)

Returns the value of attribute before_fork_callbacks.



141
142
143
# File 'lib/megatest/config.rb', line 141

def before_fork_callbacks
  @before_fork_callbacks
end

#build_idObject

Returns the value of attribute build_id.



139
140
141
# File 'lib/megatest/config.rb', line 139

def build_id
  @build_id
end

#ciObject

Returns the value of attribute ci.



139
140
141
# File 'lib/megatest/config.rb', line 139

def ci
  @ci
end

#circuit_breakerObject (readonly)

Returns the value of attribute circuit_breaker.



141
142
143
# File 'lib/megatest/config.rb', line 141

def circuit_breaker
  @circuit_breaker
end

#colors(io = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/megatest/config.rb', line 194

def colors(io = nil)
  case @colors
  when true
    Output::ANSIColors
  when false
    Output::NoColors
  else
    if io && !io.tty?
      Output::NoColors
    else
      Output::ANSIColors
    end
  end
end

#deprecationsObject

Returns the value of attribute deprecations.



139
140
141
# File 'lib/megatest/config.rb', line 139

def deprecations
  @deprecations
end

#differ=(value) ⇒ Object (writeonly)

Sets the attribute differ

Parameters:

  • value

    the value to set the attribute differ to.



143
144
145
# File 'lib/megatest/config.rb', line 143

def differ=(value)
  @differ = value
end

#global_setup_callbacksObject (readonly)

Returns the value of attribute global_setup_callbacks.



141
142
143
# File 'lib/megatest/config.rb', line 141

def global_setup_callbacks
  @global_setup_callbacks
end

#heartbeat_frequencyObject

Returns the value of attribute heartbeat_frequency.



139
140
141
# File 'lib/megatest/config.rb', line 139

def heartbeat_frequency
  @heartbeat_frequency
end

#job_indexObject

Returns the value of attribute job_index.



139
140
141
# File 'lib/megatest/config.rb', line 139

def job_index
  @job_index
end

#jobs_countObject

Returns the value of attribute jobs_count.



139
140
141
# File 'lib/megatest/config.rb', line 139

def jobs_count
  @jobs_count
end

#load_pathsObject

Returns the value of attribute load_paths.



139
140
141
# File 'lib/megatest/config.rb', line 139

def load_paths
  @load_paths
end

#max_retriesObject

Returns the value of attribute max_retries.



139
140
141
# File 'lib/megatest/config.rb', line 139

def max_retries
  @max_retries
end

#minitest_compatibilityObject

Returns the value of attribute minitest_compatibility.



139
140
141
# File 'lib/megatest/config.rb', line 139

def minitest_compatibility
  @minitest_compatibility
end

#pretty_printer=(value) ⇒ Object (writeonly)

Sets the attribute pretty_printer

Parameters:

  • value

    the value to set the attribute pretty_printer to.



143
144
145
# File 'lib/megatest/config.rb', line 143

def pretty_printer=(value)
  @pretty_printer = value
end

#program_nameObject



174
175
176
# File 'lib/megatest/config.rb', line 174

def program_name
  @program_name || "megatest"
end

#queue_urlObject

Returns the value of attribute queue_url.



139
140
141
# File 'lib/megatest/config.rb', line 139

def queue_url
  @queue_url
end

#retry_toleranceObject

Returns the value of attribute retry_tolerance.



139
140
141
# File 'lib/megatest/config.rb', line 139

def retry_tolerance
  @retry_tolerance
end

#seedObject

Returns the value of attribute seed.



141
142
143
# File 'lib/megatest/config.rb', line 141

def seed
  @seed
end

#selectorsObject

Returns the value of attribute selectors.



139
140
141
# File 'lib/megatest/config.rb', line 139

def selectors
  @selectors
end

#worker_idObject

Returns the value of attribute worker_id.



141
142
143
# File 'lib/megatest/config.rb', line 141

def worker_id
  @worker_id
end

#worker_setup_callbacksObject (readonly)

Returns the value of attribute worker_setup_callbacks.



141
142
143
# File 'lib/megatest/config.rb', line 141

def worker_setup_callbacks
  @worker_setup_callbacks
end

#workers_countObject

Returns the value of attribute workers_count.



141
142
143
# File 'lib/megatest/config.rb', line 141

def workers_count
  @workers_count
end

Instance Method Details

#before_fork(&block) ⇒ Object



248
249
250
# File 'lib/megatest/config.rb', line 248

def before_fork(&block)
  @before_fork_callbacks << block
end

#build_queueObject



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/megatest/config.rb', line 232

def build_queue
  case @queue_url
  when nil
    Queue.build(self)
  when /\Arediss?:/
    require "megatest/redis_queue"
    RedisQueue.build(self)
  else
    raise ArgumentError, "Unsupported queue type: #{@queue_url.inspect}"
  end
end

#diff(expected, actual) ⇒ Object



213
214
215
# File 'lib/megatest/config.rb', line 213

def diff(expected, actual)
  @differ&.call(expected, actual)
end

#global_setup(&block) ⇒ Object



256
257
258
# File 'lib/megatest/config.rb', line 256

def global_setup(&block)
  @global_setup_callbacks << block
end

#job_setup(&block) ⇒ Object



264
265
266
# File 'lib/megatest/config.rb', line 264

def job_setup(&block)
  @job_setup_callbacks << block
end

#job_teardown(&block) ⇒ Object



272
273
274
# File 'lib/megatest/config.rb', line 272

def job_teardown(&block)
  @job_teardown_callbacks << block
end

#marshal_dumpObject



289
290
291
292
293
# File 'lib/megatest/config.rb', line 289

def marshal_dump
  instance_variables.reject { |k| NOT_SERIALIZED.include?(k) }.to_h do |name|
    [name, instance_variable_get(name)]
  end
end

#marshal_load(hash) ⇒ Object



295
296
297
298
299
# File 'lib/megatest/config.rb', line 295

def marshal_load(hash)
  hash.each do |name, value|
    instance_variable_set(name, value)
  end
end

#max_consecutive_failures=(max) ⇒ Object



209
210
211
# File 'lib/megatest/config.rb', line 209

def max_consecutive_failures=(max)
  @circuit_breaker = CircuitBreaker.new(max)
end

#pretty_print(object) ⇒ Object Also known as: pp



217
218
219
# File 'lib/megatest/config.rb', line 217

def pretty_print(object)
  @pretty_printer.pretty_print(object)
end

#randomObject

We always return a new generator with the same seed as to best reproduce remote builds locally if the same seed is given.



224
225
226
# File 'lib/megatest/config.rb', line 224

def random
  Random.new(@seed)
end

#retries?Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/megatest/config.rb', line 276

def retries?
  @max_retries.positive?
end

#run_before_fork_callbackObject



244
245
246
# File 'lib/megatest/config.rb', line 244

def run_before_fork_callback
  @before_fork_callback.each { |c| c.call(self) }
end

#run_global_setup_callbacksObject



252
253
254
# File 'lib/megatest/config.rb', line 252

def run_global_setup_callbacks
  @global_setup_callbacks.each { |c| c.call(self) }
end

#run_job_setup_callbacks(job_index) ⇒ Object



260
261
262
# File 'lib/megatest/config.rb', line 260

def run_job_setup_callbacks(job_index)
  @job_setup_callbacks.each { |c| c.call(self, job_index) }
end

#run_job_teardown_callbacks(job_index) ⇒ Object



268
269
270
# File 'lib/megatest/config.rb', line 268

def run_job_teardown_callbacks(job_index)
  @job_teardown_callbacks.each { |c| c.call(self, job_index) }
end

#total_max_retries(size) ⇒ Object



280
281
282
283
284
285
286
# File 'lib/megatest/config.rb', line 280

def total_max_retries(size)
  if @retry_tolerance.positive?
    (size * @retry_tolerance).ceil
  else
    @max_retries * size
  end
end

#valid_worker_index?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/megatest/config.rb', line 190

def valid_worker_index?
  worker_id.is_a?(Integer) && worker_id.positive? && worker_id < workers_count
end