Class: Moto::Runner::ThreadContext

Inherits:
Object
  • Object
show all
Defined in:
lib/runner/thread_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, test_reporter) ⇒ ThreadContext

Returns a new instance of ThreadContext.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/runner/thread_context.rb', line 12

def initialize(test, test_reporter)
  @test = test
  @test_reporter = test_reporter

  log_directory = File.dirname(@test.log_path)
  if !File.directory?(log_directory)
    FileUtils.mkdir_p(log_directory)
  end

  Thread.current['logger'] = Logger.new(File.open(@test.log_path, File::WRONLY | File::TRUNC | File::CREAT))
  Thread.current['logger'].level = config[:test_log_level] || Logger::DEBUG
end

Instance Attribute Details

#testObject (readonly)

Returns the value of attribute test.



10
11
12
# File 'lib/runner/thread_context.rb', line 10

def test
  @test
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/runner/thread_context.rb', line 25

def run
  max_attempts = config[:test_attempt_max]   || 1
  sleep_time   = config[:test_attempt_sleep] || 0

  # Reporting: start_test
  @test_reporter.report_start_test(@test.status)

  (1..max_attempts).each do |attempt|

    Thread.current['clients_manager'].clients.each_value { |c| c.start_test(@test) }
    @test.before
    Thread.current['logger'].info("Start: #{@test.name} attempt #{attempt}/#{max_attempts}")

    begin
      @test.run_test
    rescue Exceptions::TestForcedPassed, Exceptions::TestForcedFailure, Exceptions::TestSkipped => e
      Thread.current['logger'].info(e.message)
    rescue Exception => e
      Thread.current['logger'].error("#{e.class.name}: #{e.message}")
      Thread.current['logger'].error(e.backtrace.join("\n"))
      Thread.current['clients_manager'].clients.each_value { |c| c.handle_test_exception(@test, e) }
    end

    @test.after
    Thread.current['clients_manager'].clients.each_value { |c| c.end_test(@test) }

    Thread.current['logger'].info("Result: #{@test.status.results.last.code}")

    # test should have another attempt in case of an error / failure / none at all
    if (@test.status.results.last.code == Moto::Test::Result::ERROR   && !Moto::Lib::Config.moto[:test_reattempt_on_error]) ||
       (@test.status.results.last.code == Moto::Test::Result::FAILURE && !Moto::Lib::Config.moto[:test_reattempt_on_fail] )
      break
    end

    # don't go to sleep in the last attempt
    if attempt < max_attempts
      sleep sleep_time
    end

  end # Make another attempt

  # Close and flush stream to file
  Thread.current['logger'].close

  # Reporting: end_test
  @test_reporter.report_end_test(@test.status)

  Thread.current['clients_manager'].clients.each_value { |c| c.end_run }

end