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.



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
174
175
176
# File 'lib/megatest/config.rb', line 147

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]
  @output_profile = nil
  CIService.configure(self, env)
end

Instance Attribute Details

#backtraceObject (readonly)

Returns the value of attribute backtrace.



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

def backtrace
  @backtrace
end

#before_fork_callbacksObject (readonly)

Returns the value of attribute before_fork_callbacks.



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

def before_fork_callbacks
  @before_fork_callbacks
end

#build_idObject

Returns the value of attribute build_id.



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

def build_id
  @build_id
end

#ciObject

Returns the value of attribute ci.



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

def ci
  @ci
end

#circuit_breakerObject (readonly)

Returns the value of attribute circuit_breaker.



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

def circuit_breaker
  @circuit_breaker
end

#colors(io = nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/megatest/config.rb', line 227

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.



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

def deprecations
  @deprecations
end

#differ=(value) ⇒ Object (writeonly)

Sets the attribute differ

Parameters:

  • value

    the value to set the attribute differ to.



145
146
147
# File 'lib/megatest/config.rb', line 145

def differ=(value)
  @differ = value
end

#global_setup_callbacksObject (readonly)

Returns the value of attribute global_setup_callbacks.



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

def global_setup_callbacks
  @global_setup_callbacks
end

#heartbeat_frequencyObject

Returns the value of attribute heartbeat_frequency.



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

def heartbeat_frequency
  @heartbeat_frequency
end

#job_indexObject

Returns the value of attribute job_index.



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

def job_index
  @job_index
end

#jobs_countObject



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

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.



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

def load_paths
  @load_paths
end

#max_retriesObject

Returns the value of attribute max_retries.



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

def max_retries
  @max_retries
end

#minitest_compatibilityObject

Returns the value of attribute minitest_compatibility.



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

def minitest_compatibility
  @minitest_compatibility
end

#output_profileObject

Returns the value of attribute output_profile.



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

def output_profile
  @output_profile
end

#pretty_printer=(value) ⇒ Object (writeonly)

Sets the attribute pretty_printer

Parameters:

  • value

    the value to set the attribute pretty_printer to.



145
146
147
# File 'lib/megatest/config.rb', line 145

def pretty_printer=(value)
  @pretty_printer = value
end

#program_nameObject



188
189
190
# File 'lib/megatest/config.rb', line 188

def program_name
  @program_name || "megatest"
end

#queue_urlObject

Returns the value of attribute queue_url.



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

def queue_url
  @queue_url
end

#retry_toleranceObject

Returns the value of attribute retry_tolerance.



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

def retry_tolerance
  @retry_tolerance
end

#seedObject

Returns the value of attribute seed.



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

def seed
  @seed
end

#selectorsObject

Returns the value of attribute selectors.



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

def selectors
  @selectors
end

#test_globsObject

Returns the value of attribute test_globs.



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

def test_globs
  @test_globs
end

#worker_idObject

Returns the value of attribute worker_id.



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

def worker_id
  @worker_id
end

#workers_countObject

Returns the value of attribute workers_count.



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

def workers_count
  @workers_count
end

Instance Method Details

#before_fork(&block) ⇒ Object



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

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

#build_queueObject



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/megatest/config.rb', line 266

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



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

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

#global_setup(&block) ⇒ Object



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

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

#initialize_dup(_) ⇒ Object



182
183
184
185
186
# File 'lib/megatest/config.rb', line 182

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

#job_setup(&block) ⇒ Object



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

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

#job_teardown(&block) ⇒ Object



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

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

#marshal_dumpObject



327
328
329
330
331
# File 'lib/megatest/config.rb', line 327

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



333
334
335
336
337
# File 'lib/megatest/config.rb', line 333

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

#max_consecutive_failures=(max) ⇒ Object



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

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

#parallelize_maybeObject



206
207
208
209
# File 'lib/megatest/config.rb', line 206

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.



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

def random
  Random.new(@seed)
end

#render_object(object) ⇒ Object



250
251
252
# File 'lib/megatest/config.rb', line 250

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

#retries?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/megatest/config.rb', line 314

def retries?
  @max_retries.positive?
end

#run_before_fork_callbackObject



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

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

#run_global_setup_callbacksObject



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

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

#run_job_setup_callbacks(job_index) ⇒ Object



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

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



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

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

#total_max_retries(size) ⇒ Object



318
319
320
321
322
323
324
# File 'lib/megatest/config.rb', line 318

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)


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

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