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
173
# 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 = nil
  @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
  @test_globs = [DEFAULT_TEST_GLOB]
  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



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/megatest/config.rb', line 224

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



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/megatest/config.rb', line 189

def jobs_count
  if @jobs_count == :number_of_processors
    if Megatest.fork?
      require "etc"
      nprocessors = Etc.nprocessors
      jobs = [nprocessors, cgroups_cpu_quota&.to_i || nprocessors].min
      @jobs_count = [jobs, 1].max
    else
      @jobs_count = 1
    end
  end
  @jobs_count ||= 1
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



185
186
187
# File 'lib/megatest/config.rb', line 185

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

#test_globsObject

Returns the value of attribute test_globs.



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

def test_globs
  @test_globs
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

#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



283
284
285
# File 'lib/megatest/config.rb', line 283

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

#build_queueObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/megatest/config.rb', line 263

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

#diff(expected, actual) ⇒ Object



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

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

#global_setup(&block) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



179
180
181
182
183
# File 'lib/megatest/config.rb', line 179

def initialize_dup(_)
  super
  @circuit_breaker = @circuit_breaker.dup
  @test_globs = @test_globs.dup
end

#job_setup(&block) ⇒ Object



299
300
301
# File 'lib/megatest/config.rb', line 299

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

#job_teardown(&block) ⇒ Object



307
308
309
# File 'lib/megatest/config.rb', line 307

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

#marshal_dumpObject



324
325
326
327
328
# File 'lib/megatest/config.rb', line 324

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



330
331
332
333
334
# File 'lib/megatest/config.rb', line 330

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

#max_consecutive_failures=(max) ⇒ Object



239
240
241
# File 'lib/megatest/config.rb', line 239

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

#parallelize_maybeObject



203
204
205
206
# File 'lib/megatest/config.rb', line 203

def parallelize_maybe
  @jobs_count ||= :number_of_processors if Megatest.fork?
  self
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.



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

def random
  Random.new(@seed)
end

#render_object(object) ⇒ Object



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

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

#retries?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/megatest/config.rb', line 311

def retries?
  @max_retries.positive?
end

#run_before_fork_callbackObject



279
280
281
# File 'lib/megatest/config.rb', line 279

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

#run_global_setup_callbacksObject



287
288
289
# File 'lib/megatest/config.rb', line 287

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

#run_job_setup_callbacks(job_index) ⇒ Object



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

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



303
304
305
# File 'lib/megatest/config.rb', line 303

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

#total_max_retries(size) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/megatest/config.rb', line 315

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)


220
221
222
# File 'lib/megatest/config.rb', line 220

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