Class: Moto::Modes::Run::ThreadContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(test, test_reporter) ⇒ ThreadContext

Returns a new instance of ThreadContext.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/modes/run/thread_context.rb', line 11

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

  setup_logger
end

Instance Attribute Details

#testObject (readonly)

Returns the value of attribute test.



9
10
11
# File 'lib/modes/run/thread_context.rb', line 9

def test
  @test
end

Instance Method Details

#loggerObject



87
88
89
# File 'lib/modes/run/thread_context.rb', line 87

def logger
  Thread.current['logger']
end

#runObject



23
24
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
# File 'lib/modes/run/thread_context.rb', line 23

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, @test.)

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

    @test.before
    logger.info("Start: #{@test.name} attempt #{attempt}/#{max_attempts}")

    begin
      @test.run_test
    rescue Exceptions::TestForcedPassed, Exceptions::TestForcedFailure, Exceptions::TestSkipped => e
      logger.info(e.message)
    rescue Exception => e
      logger.error("#{e.class.name}: #{e.message}")
      logger.error(e.backtrace.join("\n"))

      if config[:explicit_errors]
        raise e
      end

    end

    @test.after

    logger.info("Result: #{@test.status.results.last.code}")

    # test should have another attempt in case of an error / failure / none at all
    unless (@test.status.results.last.code == Moto::Test::Result::ERROR && config[:test_reattempt_on_error]) ||
        (@test.status.results.last.code == Moto::Test::Result::FAILURE && config[: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
  logger.close

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

#setup_loggerObject



79
80
81
82
83
84
85
# File 'lib/modes/run/thread_context.rb', line 79

def setup_logger
  file = File.open(@test.log_path, File::WRONLY | File::TRUNC | File::CREAT)
  file.chmod(0o666)

  Thread.current['logger'] = Logger.new(file)
  logger.level = config[:log_level]
end