Class: ChaoticJob::Simulation

Inherits:
Object
  • Object
show all
Defined in:
lib/chaotic_job/simulation.rb

Instance Method Summary collapse

Constructor Details

#initialize(job, callstack: nil, variations: nil, test: nil, seed: nil, perform_only_jobs_within: nil, capture: nil) ⇒ Simulation

Returns a new instance of Simulation.

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/chaotic_job/simulation.rb', line 6

def initialize(job, callstack: nil, variations: nil, test: nil, seed: nil, perform_only_jobs_within: nil, capture: nil)
  @template = job
  @callstack = callstack || capture_callstack
  @variations = variations
  @test = test
  @seed = seed || Random.new_seed
  @random = Random.new(@seed)
  @perform_only_jobs_within = perform_only_jobs_within
  @capture = capture

  @template.class.retry_on RetryableError, attempts: 3, wait: 1, jitter: 0
  raise Error.new("callstack must be a generated via ChaoticJob::Tracer") unless @callstack.is_a?(Stack)
end

Instance Method Details

#define(&assertions) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chaotic_job/simulation.rb', line 20

def define(&assertions)
  debug "👾 Defining #{@variations || "all"} simulated scenarios of the total #{variants.size} possibilities..."

  scenarios.each do |scenario|
    test_method_name = "test_simulation_scenario_before_#{scenario.glitch.event}_#{scenario.glitch.key}"
    perform_only_jobs_within = @perform_only_jobs_within

    @test.define_method(test_method_name) do
      if perform_only_jobs_within
        scenario.run do
          Performer.perform_all_before(perform_only_jobs_within)
          instance_exec(scenario, &assertions)
        end
      else
        scenario.run
        instance_exec(scenario, &assertions)
      end

      assert scenario.glitched?, "Scenario did not execute glitch: #{scenario.glitch}"
    end
  end

  # Since the callstack capture likely touches the database and this code runs during test class definition,
  # we need to disconnect the database connection before possible parallel test forking
  ActiveRecord::Base.connection_pool.disconnect! if ActiveRecord::Base.connected?
end